如何在recycleview中找到每个可见项目的百分比 [英] How to find percentage of each visible item in recycleview

查看:288
本文介绍了如何在recycleview中找到每个可见项目的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();

所有这些方法都给出了可见项目,但是如何在Recycleview中找到可见项目的百分比?

All this method is giving which item is visible, but how to find percentage of visible item in Recycleview?

即findFirstVisibleItemPosition();给出第一个可见项目的位置,但是我想在第一个可见项目超过50%时执行一些操作

i.e findFirstVisibleItemPosition(); is giving position of first visible item but I want to perform some operation when first is visible more then 50 percentage

推荐答案

您需要为recyclerview编写addOnScrollListener(),并在其中编写以下代码,以获取firstVisibleItem的位置,然后获得尺寸每个滚动条上的那个项目.只需将先前的尺寸保存在全局变量中,然后将其与当前尺寸进行比较,并在每次滚动更改时计算百分比即可.当您获得所需的百分比时,请执行操作.

You need to write addOnScrollListener() for recyclerview and inside that write following code where you will get position of firstVisibleItem and then you can get dimension of that item on each scroll. Just save previous dimension in global variable and compare it with the current dimension and calculate percentage on every scroll change. When you get your desired percentage then perform your operations.

int position = linearLayoutManager.findFirstVisibleItemPosition();
Rect rect = new Rect();
linearLayoutManager.findViewByPosition(position).getGlobalVisibleRect(rect);

这篇关于如何在recycleview中找到每个可见项目的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆