Android计算滚动recyclelerView的百分比

时间:2017-10-10 06:15:28

标签: android

我试图通过此代码显示自动滚动到recylerview项目结尾的百分比:

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

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

但它没有改正,我试图找到什么是解决方案,这意味着显示当前滚动的Recyclerview项目的百分比

2 个答案:

答案 0 :(得分:10)

Recyclerview为垂直和水平滚动提供了以下方法,使用下面的代码

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

        float percentage = (100.0f * offset / (float)(range - extent));

答案 1 :(得分:0)

我认为这会奏效。试试看,让我知道:

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