Get visible items in RecyclerView

前端 未结 9 2172
礼貌的吻别
礼貌的吻别 2020-11-22 07:17

I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the OnScrollListener.onScroll(...) method on ListViews. I tried to work w

9条回答
  •  猫巷女王i
    2020-11-22 08:09

    Every answer above is correct and I would like to add also a snapshot from my working codes.

    recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
               //some code when initially scrollState changes
            }
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //Some code while the list is scrolling
                LinearLayoutManager lManager = (LinearLayoutManager) recycler.getLayoutManager();
                int firstElementPosition = lManager.findFirstVisibleItemPosition();
                
            }
        });
    

提交回复
热议问题