8

i'm trying to show percentage of scrolling automatically to end of recylerview item by this code:

visibleItemCount = bookContentLayoutManager.getChildCount();
totalItemCount   = bookContentLayoutManager.getItemCount();
firstVisibleItem = bookContentLayoutManager.findFirstVisibleItemPosition();

float percentage = (visibleItemCount * 100 / totalItemCount);
Log.e("percent ", percentage + "");

but it doesn't correct and i'm trying to find whats is solution of that, it means show percentage of current scrolled recyclerview items

3
  • try getAdapterPosition(); ? Commented Oct 10, 2017 at 6:19
  • do you mean % of ongoing count which is already scrolled with total number of items which we have ? Commented Oct 10, 2017 at 6:21
  • check out my answer, it would be useful if you could provide usecase Commented Oct 10, 2017 at 6:41

2 Answers 2

26

Recyclerview provides the following methods for vertical and horizontal scroll use the below code,

        int offset = recyclerView.computeVerticalScrollOffset();
        int extent = recyclerView.computeVerticalScrollExtent();
        int range = recyclerView.computeVerticalScrollRange();

        float percentage = (100.0f * offset / (float)(range - extent));
3
  • You can always round final value by call Math.round(percentage);
    – deadfish
    Commented Dec 28, 2018 at 14:02
  • You are my hero. Thanks!
    – jordiz
    Commented Oct 7, 2019 at 15:14
  • this doesn't seem to work fine with a recyclerview that has multiple view types... do you know a solution for that case? Commented May 17, 2022 at 20:15
0

I think This will work . try this out and let me know :

 int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
    int totalItemCount = layoutManager.getItemCount();
    float percentage=(lastVisibleItemPosition/totalItemCount)*100;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.