【问题标题】:SwipeRefreshLayout stops working / RecyclerView inconsistently triggers SwipeRefreshLayoutSwipeRefreshLayout 停止工作/RecyclerView 不一致地触发 SwipeRefreshLayout
【发布时间】:2017-05-25 09:13:28
【问题描述】:

我发现 RecyclerView 在 Nexus 6P API 25 7.1.1 模拟器上不一致地激活 SwipeRefreshLayout 存在问题。 我们使用的是最新版本的支持库:

compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'

我有一个带有数据集的 RecyclerView,该数据集在发生拉动刷新(将项目添加到顶部)时预先添加,它在我的布局中声明如下:

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
</android.support.v4.widget.SwipeRefreshLayout>

我能够触发手势大约 2-3 次,将项目添加到 RecyclerView 但它不再允许我触发拉动刷新。我们从不调用 SwipeRefreshLayout.setEnabled(false)。即使我们位于项目列表的顶部,RecyclerView 似乎也在内部对 canScrollVertically(-1) 做出响应。

我们处理的唯一有趣的事情是我们覆盖了 RecyclerView 的布局管理器,让我们滚动到底部:

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setStackFromEnd(true); // Start with most recent messages (enables scroll-to-bottom)
recyclerView.setLayoutManager(llm);

【问题讨论】:

    标签: android android-recyclerview android-support-library swiperefreshlayout


    【解决方案1】:

    View 中,计算canScrollVertically(-1) 的代码取决于computeVerticalScrollOffsetcomputeVerticalScrollRangecomputeVerticalScrollExtent 的结果。

    RecyclerView 依赖于LinearLayoutManager 来执行这些计算。这转发到ScrollbarHelper,如果smoothScrollbarEnabledtrue(默认值),它会做一些近似,以显示一个漂亮的滚动条。

    如果您的RecyclerView 中的项目大小不同,则这些近似值将完全错误。

    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setSmoothScrollbarEnabled(false);
    recyclerView.setLayoutManager(llm);
    

    解决了我们的问题并让SwipeRefreshLayout 每次我们到达列表顶部时都能可靠地触发。

    【讨论】:

      猜你喜欢
      • 2022-10-06
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 2015-08-10
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多