简体   繁体   中英

How to get fully visible cells/items in recyclerview?

I have a screen where items/cells are designed in the 2x2 grid form. What I want to do is I want to fire APIs on item visibility/impression when RecyclerView scrolls up/down.

As my list is pretty huge

For example scenario is,

  1. On the first load, it shows eg 6 (3x2) items/cells. Then, fire API for all those 6 items as those are fully visible.
  2. On scroll up, eg it shows another 2 items and above/top 2 items go out of screen hence, it should fire API for newly 2 items.

This way I want to complete traversal of the entire list and call API on newly found items.

What I tried is,

  1. Below methods of GridLayoutManager not worked for me as each of them giving different values and seems to be random

int findFirstVisibleItemPosition();

int findFirstCompletelyVisibleItemPosition();

int findLastVisibleItemPosition();

int findLastCompletelyVisibleItemPosition();

  1. Checking bounds of View lying on screen with the help of Rect | getGlobalVisibleRect() Rect | getGlobalVisibleRect() but that is also not working inside onScrolled() | OnScrollStateChanged() onScrolled() | OnScrollStateChanged()

Please suggest approach where I can get the correct index of the fully visible cell.

Try to create separate thread for the action of onScrolled() | onScrollStateChanged() onScrolled() | onScrollStateChanged() . It will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM