Skip to content Skip to sidebar Skip to footer

How To Get Current Visible Item In Recycler View

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

Solution 1:

private RecyclerView.OnScrollListenerrecyclerViewOnScrollListener=newRecyclerView.OnScrollListener() {
    @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        intvisibleItemCount= linearLayoutManager.getChildCount();
        inttotalItemCount= linearLayoutManager.getItemCount();
        intfirstVisibleItemPosition= linearLayoutManager.findFirstVisibleItemPosition();
        finalintlastItem= firstVisibleItemPosition + visibleItemCount;
    }
};

Declare LinearLayoutManger globally,

private LinearLayoutManager linearLayoutManager;

Initialize RecyclerView like this,

linearLayoutManager = newLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
notificationListRecyclerView.setLayoutManager(linearLayoutManager);
notificationListRecyclerView.addOnScrollListener(recyclerViewOnScrollListener);

Post a Comment for "How To Get Current Visible Item In Recycler View"