首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwipeRefreshLayout停止工作/ RecyclerView不一致地触发SwipeRefreshLayout

SwipeRefreshLayout停止工作/ RecyclerView不一致地触发SwipeRefreshLayout
EN

Stack Overflow用户
提问于 2017-05-25 09:13:28
回答 1查看 309关注 0票数 0

我在Nexus6PAPI257.1.1模拟器上看到了RecyclerView不一贯地激活SwipeRefreshLayout的问题。我们正在使用支持库的最新版本:

代码语言:javascript
复制
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,当发生拉力刷新时(将项添加到顶部),它将在布局中声明如下:

代码语言:javascript
复制
<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),尽管我们位于项目列表的首位。

关于我们处理的唯一有趣的事情是,我们重写了回收视图的布局管理器,让我们滚动到底部:

代码语言:javascript
复制
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setStackFromEnd(true); // Start with most recent messages (enables scroll-to-bottom)
recyclerView.setLayoutManager(llm);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-25 09:24:36

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

RecyclerView依赖于LinearLayoutManager来完成这些计算。这将转发给ScrollbarHelper,如果smoothScrollbarEnabledtrue (默认值),它会执行一些近似,以显示一个花哨的滚动条。

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

代码语言:javascript
复制
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setSmoothScrollbarEnabled(false);
recyclerView.setLayoutManager(llm);

解决了我们的问题,并使SwipeRefreshLayout在每次我们登上榜首时都能可靠地触发。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44176685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档