跟踪列表的所有项目当前在屏幕上可见 [英] Tracking what all items of a list are currently visible on the screen

查看:96
本文介绍了跟踪列表的所有项目当前在屏幕上可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含10个项目的recyclerview.在这10个中,一次就能看到0-4.向下滚动时,第0个元素离开屏幕,第5个元素进入.我想跟踪这些事件-我知道元素0-4是可见的,然后滚动时是1-5.

Suppose I have a recyclerview with 10 items. Out of these 10, 0 - 4 are visible in one go. On scrolling down, 0th element goes away from the screen and 5th one enters. I want to track these events - where I get to know that elements 0 - 4 are visible and then on scrolling, 1-5 are.

反正有实现这一目标的方法吗?我想跟踪用户花了多长时间在recyclerview的特定项目上.

Is there anyway to achieve this? I want to keep track of how long a user is spending time on a particular items of the recyclerview.

推荐答案

在RecyclerView上设置 addOnScrollListener 并获取第一个和最后一个可见行

set addOnScrollListener on RecyclerView and get first and last visible rows

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            int firstVisiblePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
            int lastVisiblePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();

            // Now you can easily get all rows b/w first and last item
        }
    }
});

这篇关于跟踪列表的所有项目当前在屏幕上可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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