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

后端 未结 11 2081
一向
一向 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条回答
  •  粉色の甜心
    2020-12-02 07:05

    The solution is in the layout manager.

    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

    In case your item size is larger than the screen use the following to detect the top event.

    RecyclerView recyclerView = (RecyclerView) view;
    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    
    int pos = linearLayoutManager.findFirstVisibleItemPosition();
    
    if(linearLayoutManager.findViewByPosition(pos).getTop()==0 && pos==0){
        return true;
    }
    

    PS: Actually, if you place the RecyclerView directly inside the SwipeRefreshview you wouldn't need to do this

提交回复
热议问题