向上滚动不适用于 Listview 中的 SwipeRefreshLayout

2024-01-04

我想用 listView 实现滚动刷新功能。此外,同一布局文件中还有其他视图元素,如果列表为空,则会显示这些元素。这是我的布局文件。问题是,当我向下滚动然后尝试向上滚动时,而不是一直滚动到顶部然后刷新它只是在那里刷新并且向上滚动不起作用。

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="64dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/inbox_empty" />

        <TextView
            android:id="@+id/noEventsText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="16dp"
            android:layout_toRightOf="@+id/noEventsIcon" />

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:background="@color/dividerOnBlack" />
    </RelativeLayout>

    <ListView
        android:id="@+id/list_items"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="@android:color/transparent"
        android:choiceMode="multipleChoice"
        android:descendantFocusability="afterDescendants"
        android:divider="@android:color/transparent"
        android:dividerHeight="0px"
        android:scrollbars="none" />
</LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>

这是我的 onScroll 方法。

@Override
public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount) {
    // If the total item count is zero and the previous isn't, assume the
    // list is invalidated and should be reset back to initial state
    if (totalItemCount < previousTotalItemCount) {
        this.currentPage = this.startingPageIndex;
        this.previousTotalItemCount = totalItemCount;
        if (totalItemCount == 0) { this.loading = true; } 
    }

    // If it's still loading, we check to see if the dataset count has
    // changed, if so we conclude it has finished loading and update the current page
    // number and total item count.
    if (loading && (totalItemCount > previousTotalItemCount)) {
        loading = false;
        previousTotalItemCount = totalItemCount;
        currentPage++;
    }

    // If reverse then the firstVisibleItem is calculated wrong
    if (reverse) {
        firstVisibleItem = totalItemCount - firstVisibleItem;
    }
    // If it isn't currently loading, we check to see if we have breached
    // the visibleThreshold and need to reload more data.
    // If we do need to reload some more data, we execute onLoadMore to fetch the data.
    if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
        onLoadMore(currentPage + 1, totalItemCount);
        loading = true;
    }
}

为了使 SwipeRefreshLayout 工作,它需要是 ListView 的直接父级,并且 ListView 应该是 SwipeRefreshLayout 的第一个活动子视图。

SwipeRefreshLayout 的文档说 ListView 应该是唯一的子视图,但是如果它有多个子视图也没关系,只要 ListView 是第一个即可。这意味着,例如,如果您使用具有“空”视图的适配器,SwipeRefreshLayout 将正常工作。例如:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NearbyJobsActivity$PlaceholderFragment">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />

</android.support.v4.widget.SwipeRefreshLayout>

如果您可以管理这种布局,那么 SwipeRefreshLayout 将正常工作,并且您不需要其他答案中列出的任何解决方法。

我自己的问题是我将 ListView 作为片段加载,所以我实际上有:

<SwipeRefreshLayout>

    <FrameLayout>           )
         <ListView/>        \ fragment
         <TextView/>        /
    </FrameLayout>          )

</SwipeRefreshLayout>

因此 SwipeRefreshLayout 选择 FrameLayout 作为“目标”及其默认值canChildScrollUp()实现总是返回 false。一旦我将 SwipeRefreshLayout 移动到 Fragment 内,一切就开始正常工作。

<FrameLayout>

    <SwipeRefreshLayout>    )
         <ListView/>        \ fragment
         <TextView/>        /
    </SwipeRefreshLayout>   )

</FrameLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

向上滚动不适用于 Listview 中的 SwipeRefreshLayout 的相关文章

随机推荐

  • 如何停止 Microsoft 认知 TTS 音频播放?

    我正在使用 Microsoft 认知服务语音 SDK 的 JavaScript 版本https github com Azure Samples cognitive services speech sdk https github com
  • PyQt 对齐复选框并将其放在每一行中

    我正在尝试做this http falsinsoft blogspot ro 2013 11 qtablewidget center checkbox inside cell html与复选框 遗憾的是 它是为 C 编写的 并且对 Pyth
  • 为什么我收到未读块数据 - 非法状态异常?

    我只有以下内容 JavaPairRDD
  • java单例模式,所有变量都应该是类变量吗?

    如果一个类实现了单例模式 是否应该将所有变量声明为静态 有什么理由不应该将它们声明为静态吗 这有什么不同吗 不 单例模式只是意味着单个实例是唯一的实例 它并不意味着 使所有内容都可以静态访问 单例模式为您提供 单实例 的所有好处 而不牺牲测
  • 使用 new 创建时命名 ValueTuple 属性

    我知道当我隐式创建元组时可以命名参数 例如 var me age 21 favoriteFood Custard 显式创建元组时是否可以命名参数 IE var me new ValueTuple
  • ruby 中的块作用域

    我的理解是 ruby 块具有块作用域 并且在块内创建的所有变量将仅存在于该块内 案例示例 food toast cheese wine food each food puts food capitalize puts food Output
  • Woocommerce 按属性搜索

    我在默认的 woocommerce 搜索系统中遇到了一个小问题 我需要开设一家基于 WooCommerce 的书店 所有这些书籍都包含独特的属性 例如标识号和 ODN 或 IBN 现在我需要一个搜索栏 如果我在搜索栏中输入任何独特的属性 例
  • 什么控制 git checkout 反馈?

    有时一个git checkout命令给出进度反馈 git checkout develop Checking out files 100 10779 10779 done Switched to branch develop 有时它不会 下
  • Python中获取总物理内存

    如何以与分布无关的方式获取 Python 中的总物理内存 我不需要已用内存 只需要总物理内存 跨平台解决方案的最佳选择是使用psutil https github com giampaolo psutil包 可在PyPI https pyp
  • Helvetica 在 Windows 操作系统上呈现为 Arial

    在我的网站上 http helvetitee com http helvetitee com 我有以下字体堆栈 font family helvetica neue helvetica nimbus sans Nimbus Sans 一种网
  • 用 Coq 重写假设,保留蕴涵

    我正在做 Coq 证明 我有P gt Q作为假设 并且 P gt Q gt Q gt P 作为引理 如何将假设转化为 Q gt P 当我尝试apply它 我只是产生新的子目标 这没有帮助 换句话说 我想从以下开始 P Prop Q Prop
  • 清除 woocommerce 中的结账字段

    我正在尝试删除各个结账字段中自动加载的用户信息 但似乎找不到任何方法来访问字段值 我已经尝试了以下清除格式 删除 字段等的操作 但我找不到任何内容显示如何仅删除该值 有谁知道如何访问这个 add filter woocommerce che
  • 如何使用 ADB 生成 Android 中的捏合等多点触控事件? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想通过 ADB 命令行在 Android 中生成多点触控 捏合 的输入事件 现在我可以使用以下命令生成触摸屏滑动事件 input t
  • 每个项目类型都有单独的资源字典

    我已经在我的共享项目中创建了一个 ResourceDictionary 没有任何问题 然而 我的一些样式对于 Windows Phone 8 1 来说非常特殊 并且不会在 Windows 8 1 中使用 由于Windows Phone项目中
  • OwinContext.Request.Path 和 PathBase 是如何填充的?

    我正在根据 Katana 项目中的其他示例为 OpenID Connect 授权代码流程编写自己的 OWIN 中间件 作为此过程的一部分 我必须构造几个 URI 例如重定向 URI 和返回 URL Katana 中的其他示例通过连接当前请求
  • 通过适应度函数从群体中选择个体

    我一直在研究一种算法 我需要从大小为 k 的群体中选择 n 个个体 其中 k 比 n 大得多 所有个体都有适应度值 因此选择时应优先考虑较高的适应度值 然而 我不想简单地选择最好的n个人 最差的人也应该有机会 自然选择 因此 我决定找到人群
  • 如何在 Ninject 中使用 Provider

    我有以下代码 public class Something Inject public Configuration config get set singleton Inject public Provider
  • 按分隔符分割字符串

    我实在找不到这个答案 我在 XCode 中有一个名为 myString 的多行 NSString 它是一个 HTML 代码 我需要按行导航字符串 例如 myString 如何逐行访问 喜欢 LineOne myString Lines 0
  • 未绑定断点?

    在构建并运行代码后 我在运行一些断点时遇到了问题 我的项目是在 ASP NET 上的 我正在使用 VS 2022 到目前为止我已经尝试过以下操作 重启VS并删除我所有的断点 清理我的解决方案 构建 gt 清理解决方案 Delete vs f
  • 向上滚动不适用于 Listview 中的 SwipeRefreshLayout

    我想用 listView 实现滚动刷新功能 此外 同一布局文件中还有其他视图元素 如果列表为空 则会显示这些元素 这是我的布局文件 问题是 当我向下滚动然后尝试向上滚动时 而不是一直滚动到顶部然后刷新它只是在那里刷新并且向上滚动不起作用