Android RecyclerView.smoothScrollToPosition()的实现

在Android中,RecyclerView没有直接的smoothScrollToPosition()方法来实现类似ListView的效果。本文介绍了如何通过计算滚动距离并使用smoothScrollBy()方法模拟此功能。首先估算滚动距离,然后使用smoothScrollBy()滚动并设置onScrollListener监听滚动事件,当到达目标位置时停止滚动。提供了向上滚动(scrollUpToPosition())和向下滚动的代码示例。
摘要由CSDN通过智能技术生成

本以为RecyclerView会像ListView一样有smoothScrollToPosition的方法,用来做双击返回列表顶部的方法,但实际添加这个方法以后并没有什么卵用,于是查看器源代码

public void smoothScrollToPosition(int position) {
        if (mLayoutFrozen) {
            return;
        }
        if (mLayout == null) {
            Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " +
                    "Call setLayoutManager with a non-null argument.");
            return;
        }
        mLayout.smoothScrollToPosition(this, mState, position);
    }
然后继续往里点

public void smoothScrollToPosition(RecyclerView recyclerView, State state,
                int position) {
            Log.e(TAG, "You must override smoothScrollToPosition to support smooth scrolling");
        }

SDK居然没有给出这个方法的实现,还让我们自己复写这个方法来实现,真是太坑了,于是我只能自己去复写RecyclerView的smoothScrollToPo
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值