How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom?

后端 未结 11 2098
一向
一向 2020-12-02 06:24

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

private void laySwipeToggle() {
    if (mRecyclerView.getChildCou         


        
11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 07:07

    Just keep a reference to your layoutManager and set onScrollListener on your recycler view like this

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
    
            visibleItemCount = mRecyclerView.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            firstVisibleItemIndex = mLayoutManager.findFirstVisibleItemPosition();
    
            //synchronizew loading state when item count changes
            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                }
            }
            if (!loading)
                if ((totalItemCount - visibleItemCount) <= firstVisibleItemIndex) {
                    // Loading NOT in progress and end of list has been reached
                    // also triggered if not enough items to fill the screen
                    // if you start loading
                    loading = true;
                } else if (firstVisibleItemIndex == 0){
                    // top of list reached
                    // if you start loading
                    loading = true;
                }
            }
        }
    });
    

提交回复
热议问题