1

How i can get the "RecyclerView" current visible item. already try different methods of recyclerview but i can't get the solution so please help and guide me

Hammad
  • 31
  • 1
  • 6

1 Answers1

7
 private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int visibleItemCount = linearLayoutManager.getChildCount();
        int totalItemCount = linearLayoutManager.getItemCount();
        int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
        final int lastItem = firstVisibleItemPosition + visibleItemCount;
    }
};

Declare LinearLayoutManger globally,

private LinearLayoutManager linearLayoutManager;

Initialize RecyclerView like this,

linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
notificationListRecyclerView.setLayoutManager(linearLayoutManager);
notificationListRecyclerView.addOnScrollListener(recyclerViewOnScrollListener);
hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38