RecyclerView向上/向下滚动侦听器

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;

}