Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView visible count is giving the total items in Recyclerview

Tags:

android

I am using RecyclerView to show list of items. The total items are 10, but visible at a time is 3. But when i use recyclerView.getChildCount() method to get the visible count, it is giving me 10, instead of 3. What can be the issue. I am trying to implement pagination. So everytime my visible count is coming same as totalitemcount, and as a result my load more is getting called continuously till all the pages are loaded. Below is my code.

 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    if (dy > 0) {
        onScrolled();
    }



    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
        if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
        }
    }
    if (!loading
            && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
        // End has been reached

        // Do something
        current_page++;

        onLoadMoreData(current_page);

        loading = true;
    }

}
like image 380
arun Avatar asked Mar 07 '23 12:03

arun


1 Answers

Check this answer: Get visible items in RecyclerView


Based on that answer alone, you could do something like this:
LinearLayoutManager layoutManager = ((LinearLayoutManager)mRecyclerView.getLayoutManager());

int visibleItemCount = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition();

Hope it helps.

like image 198
Tomas Jablonskis Avatar answered Mar 10 '23 11:03

Tomas Jablonskis