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

查看:42
本文介绍了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天全站免登陆