android RecyclerView的滚动事件OnScrollListener详解

(1)滚动事件分类

列表的滚动一般分为两种:

1.手指按下 -> 手指拖拽列表移动 -> 手指停止拖拽 -> 抬起手指
2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表继续滚动 -> 停止滚动

上面的过程的状态变化如下:

1.静止 -> 被迫拖拽移动 -> 静止
2.静止 -> 被迫拖拽移动 -> 自己滚动 -> 静止

(2)监听RecyclerView的滚动

有两种方式可以监听滚动事件:

1.setOnScrollListener(OnScrollListener listener)
2.addOnScrollListener(OnScrollListener listener)

其中 setOnScrollListener 由于可能出现空指针的风险,已经过时。建议用addOnScrollListener。
(3)OnScrollListener

/**

  • An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
  • has occurred on that RecyclerView.
  • @see RecyclerView#addOnScrollListener(OnScrollListener)
  • @see RecyclerView#clearOnChildAttachStateChangeListeners()

/
public abstract static class OnScrollListener {
/
*
* Callback method to be invoked when RecyclerView’s scroll state changes.
*
* @param recyclerView The RecyclerView whose scroll state has changed.
* @param newState The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
*/
public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

/**
 * Callback method to be invoked when the RecyclerView has been scrolled. This will be
 * called after the scroll has completed.
 * <p>
 * This callback will also be called if visible item range changes after a layout
 * calculation. In that case, dx and dy will be 0.
 *
 * @param recyclerView The RecyclerView which scrolled.
 * @param dx The amount of horizontal scroll.
 * @param dy The amount of vertical scroll.
 */
public void onScrolled(RecyclerView recyclerView, int dx, int dy){}

}

OnScrollListener类是个抽象类,有两个方法:

void onScrollStateChanged(RecyclerView recyclerView, int newState): 滚动状态变化时回调
void onScrolled(RecyclerView recyclerView, int dx, int dy): 滚动时回调

3.1 onScrollStateChanged(RecyclerView recyclerView, int newState)方法

回调的两个变量的含义:
recyclerView: 当前在滚动的RecyclerView
newState: 当前滚动状态.

其中newState有三种值:

/**

  • The RecyclerView is not currently scrolling.(静止没有滚动)
    */
    public static final int SCROLL_STATE_IDLE = 0;

/**

  • The RecyclerView is currently being dragged by outside input such as user touch input.
    *(正在被外部拖拽,一般为用户正在用手指滚动)
    */
    public static final int SCROLL_STATE_DRAGGING = 1;

/**

  • The RecyclerView is currently animating to a final position while not under outside control.
    *(自动滚动)
    */
    public static final int SCROLL_STATE_SETTLING = 2;

3.2 onScrolled(RecyclerView recyclerView, int dx, int dy)方法

回调的三个变量含义:
recyclerView : 当前滚动的view
dx : 水平滚动距离
dy : 垂直滚动距离

dx > 0 时为手指向左滚动,列表滚动显示右面的内容
dx < 0 时为手指向右滚动,列表滚动显示左面的内容
dy > 0 时为手指向上滚动,列表滚动显示下面的内容
dy < 0 时为手指向下滚动,列表滚动显示上面的内容
(4)canScrollVertically和canScrollHorizontally方法

public boolean canScrollVertically (int direction)
这个方法是判断View在竖直方向是否还能向上,向下滑动。
其中,direction为 -1 表示手指向下滑动(屏幕向上滑动), 1 表示手指向上滑动(屏幕向下滑动)。

public boolean canScrollHorizontally (int direction)
这个方法用来判断 水平方向的滑动

例如:
RecyclerView.canScrollVertically(1)的值表示是否能向下滚动,false表示已经滚动到底部
RecyclerView.canScrollVertically(-1)的值表示是否能向上滚动,false表示已经滚动到顶部
(5)两种判断是否到底部的方法:
5.1方法一:

如果 当前
第一个可见item的位置 + 当前可见的item个数 >= item的总个数
这样就可以判断出来,是在底部了。

loadingMoreListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

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

        if (dy > 0) //向下滚动
        {
            int visibleItemCount = mLinearLayoutManager.getChildCount();
            int totalItemCount = mLinearLayoutManager.getItemCount();
            int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();

            if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                loading = true;
                loadMoreDate();
            }
        }
    }

};

通过
visibleItemCount + pastVisiblesItems) >= totalItemCount
来判断是否是底部。
5.2方法二:

通过canScrollVertically 来判断

loadingMoreListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(!loading && !recyclerView.canScrollVertically(1)){
loading = true;
loadMoreDate();
}
}

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

// if (dy > 0) //向下滚动
// {
// int visibleItemCount = mLinearLayoutManager.getChildCount();
// int totalItemCount = mLinearLayoutManager.getItemCount();
// int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
// if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
// loading = true;
// loadMoreDate();
// }
// }
}
};

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值