美文网首页
判断RecyclerView滑动到底部和顶部

判断RecyclerView滑动到底部和顶部

作者: 涛涛123759 | 来源:发表于2018-12-14 11:06 被阅读11次

    方法1

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    Log.i(TAG, "--------------------------------------");
                    if(mRecyclerView.canScrollVertically(1)){
                        Log.i(TAG, "direction 1: true");
                    }else {
                        Log.i(TAG, "direction 1: false");//滑动到底部
                    }
                    if(mRecyclerView.canScrollVertically(-1)){
                        Log.i(TAG, "direction -1: true");
                    }else {
                        Log.i(TAG, "direction -1: false");//滑动到顶部
                    }
                }
            });
    

    方法2

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    Log.i(TAG, "--------------------------------------");
                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                    Log.i(TAG, "firstCompletelyVisibleItemPosition: "+firstCompletelyVisibleItemPosition);
                    if(firstCompletelyVisibleItemPosition==0)
                        Log.i(TAG, "滑动到顶部");
    
                    int lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition();
                    Log.i(TAG, "lastCompletelyVisibleItemPosition: "+lastCompletelyVisibleItemPosition);
                    if(lastCompletelyVisibleItemPosition==layoutManager.getItemCount()-1)
                        Log.i(TAG, "滑动到底部");
                }
            });
    

    相关文章

      网友评论

          本文标题:判断RecyclerView滑动到底部和顶部

          本文链接:https://www.haomeiwen.com/subject/dxmwhqtx.html