4

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;
    }

}
arun
  • 153
  • 1
  • 10
  • Because RecyclerView only holds some of the items which are shown and are "close" to being shown so recyclerView.getChildCount() will return a smaller and not-constant value. you can used : int itemCount = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition(); – Sayan Manna Apr 05 '18 at 14:16

2 Answers2

7

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.

Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
  • Edited my answer – arun Apr 05 '18 at 14:47
  • I suggest you to follow this totorial, it might help: [link](https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b) You could also try using Paginate library: [link](https://github.com/MarkoMilos/Paginate) And if I were you, I would use Googles Paging library, because it is easy to use and is very efficient:[link](https://developer.android.com/topic/libraries/architecture/paging.html) – Tomas Jablonskis Apr 05 '18 at 15:08
  • findLastVisibleItemPosition return last item of list not last item which is view. For example recyclerview has 23 entry only 5 are visible. so findLastVisibleItemPosition return it return 23 not 5 – Ashish Garg Jan 10 '19 at 09:09
  • the method findLastVisibleItemPosition() ever returns 0 – Lucas Sabino Nov 08 '21 at 17:06
0
To get the number of items visible on the screen of the recycler view.

rvBreakingNews.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val mLayoutManger = recyclerView.layoutManager as LinearLayoutManager
                //To get the total Number of items visible on the screen
                val visibleItemCount = mLayoutManger.childCount 
              //To get the total items loaded in the RecyclerView
                val totalItemCount = mLayoutManger.itemCount 

        })
BAIJU SHARMA
  • 276
  • 4
  • 9