如何知道是否RecyclerView / LinearLayoutManager滚动到顶部或底部? [英] How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom?

查看:2329
本文介绍了如何知道是否RecyclerView / LinearLayoutManager滚动到顶部或底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用的是后续code,检查SwipeRefreshLayout是否应该启用。

Currently I am using the follow code to check whether SwipeRefreshLayout should be enabled.

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() RecyclerView.isScrolledToTop()

编辑:(mRecyclerView.getChildAt(0).getTop()== 0安培;&安培; linearLayoutManager.findFirstVisibleItemPosition()== 0)那种做的 RecyclerView.isScrolledToTop(),但对于 RecyclerView.isScrolledToBottom()

(mRecyclerView.getChildAt(0).getTop() == 0 && linearLayoutManager.findFirstVisibleItemPosition() == 0) kind of does the RecyclerView.isScrolledToTop(), but what about RecyclerView.isScrolledToBottom()?

推荐答案

解决的办法是在布局管理器。

The solution is in the layout manager.

LinearLayoutManager layoutManager=new LinearLayoutManager(this);
//Add this to your Recyclerview
recyclerview.setLayoutManager(layoutManager);

//Tocheck if  recycler is on top
if(layoutManager.firstCompletelyVisibleItemPosition()==0){
//Its at top ..
}

//Tocheck if  recycler is on bottom
    if(layoutManager.lastCompletelyVisibleItemPosition()==data.size()-1){
    //Its at bottom ..
    }

PS:其实如果直接将RecyclerView的SwipeRefreshview内你不会需要做到这一点

修改 柜面您的项目规模大于屏幕使用以下方法来检测的顶级盛会。

Edit Incase your item size is larger than the screen use the following to detect the top event.

RecyclerView rc=(RecyclerView)view; 
LinearLayoutManager lm= (LinearLayoutManager) rc.getLayoutManager(); 
if(lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop()==0 && lm.findFirstVisibleItemPosition()==0) 
return true; 

这篇关于如何知道是否RecyclerView / LinearLayoutManager滚动到顶部或底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆