RecyclerView向上/向下滚动侦听器 [英] RecyclerView scrolled UP/DOWN listener

查看:304
本文介绍了RecyclerView向上/向下滚动侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何知道用户在RecyclerView中是向下滚动还是向上滚动?

How do we know if user scrolled down or up in RecyclerView ?

我尝试使用RecyclerView#OnScrollListener,它给出了垂直滚动量和滚动状态.开始滚动时如何获得最后的滚动位置,滚动状态为空闲时如何获得滚动位置.

I tried with RecyclerView#OnScrollListener , it gives the amount of vertical scroll and the scroll state. How do we get the last scroll position when started to dragging and scrolled position when scroll state idle.

谢谢.

推荐答案

尝试这种方式:

private static int firstVisibleInListview;

firstVisibleInListview = yourLayoutManager.findFirstVisibleItemPosition();

在滚动侦听器中:

public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
{
    super.onScrolled(recyclerView, dx, dy);

    int currentFirstVisible = yourLayoutManager.findFirstVisibleItemPosition();

    if(currentFirstVisible > firstVisibleInListview)
       Log.i("RecyclerView scrolled: ", "scroll up!");
    else
       Log.i("RecyclerView scrolled: ", "scroll down!");  

    firstVisibleInListview = currentFirstVisible;

}

这篇关于RecyclerView向上/向下滚动侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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