目前我正在使用以下代码来检查是否应启用 SwipeRefreshLayout。
private void laySwipeToggle() {
if (mRecyclerView.getChildCount() == 0 || mRecyclerView.getChildAt(0).getTop() == 0) {
mLaySwipe.setEnabled(true);
} else {
mLaySwipe.setEnabled(false);
}
}
但问题就在这里。当它滚动到另一个项目的视图边界时mRecyclerView.getChildAt(0).getTop()
也返回 0。
有没有类似的东西RecyclerView.isScrolledToBottom()
or RecyclerView.isScrolledToTop()
?
EDIT: (mRecyclerView.getChildAt(0).getTop() == 0 && linearLayoutManager.findFirstVisibleItemPosition() == 0)
有点像RecyclerView.isScrolledToTop()
, 但是关于RecyclerView.isScrolledToBottom()
?
解决方案是在布局管理器中。
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// Add this to your Recycler view
recyclerView.setLayoutManager(layoutManager);
// To check if at the top of recycler view
if(layoutManager.firstCompletelyVisibleItemPosition()==0){
// Its at top
}
// To check if at the bottom of recycler view
if(layoutManager.lastCompletelyVisibleItemPosition()==data.size()-1){
// Its at bottom
}
EDIT
如果您的项目尺寸大于屏幕,请使用以下方法来检测顶部事件。
RecyclerView recyclerView = (RecyclerView) view;
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int pos = linearLayoutManager.findFirstVisibleItemPosition();
if(linearLayoutManager.findViewByPosition(pos).getTop()==0 && pos==0){
return true;
}
PS:实际上,如果你把RecyclerView
直接在里面SwipeRefreshview
你不需要这样做
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)