我在Nexus6PAPI257.1.1模拟器上看到了RecyclerView不一贯地激活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(假)。看起来RecyclerView在内部响应canScrollVertically(-1),尽管我们位于项目列表的首位。
关于我们处理的唯一有趣的事情是,我们重写了回收视图的布局管理器,让我们滚动到底部:
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setStackFromEnd(true); // Start with most recent messages (enables scroll-to-bottom)
recyclerView.setLayoutManager(llm);发布于 2017-05-25 09:24:36
在View中,计算canScrollVertically(-1)的代码取决于computeVerticalScrollOffset、computeVerticalScrollRange和computeVerticalScrollExtent的结果。
RecyclerView依赖于LinearLayoutManager来完成这些计算。这将转发给ScrollbarHelper,如果smoothScrollbarEnabled是true (默认值),它会执行一些近似,以显示一个花哨的滚动条。
如果您的RecyclerView中的项目是不同的大小,这些近似将是完全错误的。
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setSmoothScrollbarEnabled(false);
recyclerView.setLayoutManager(llm);解决了我们的问题,并使SwipeRefreshLayout在每次我们登上榜首时都能可靠地触发。
https://stackoverflow.com/questions/44176685
复制相似问题