RecyclerView 滚动到指定position,并置顶

转自:https://blog.csdn.net/x466911254/article/details/78525980

RecyclerView滚动的方法有

smoothScrollBy();
smoothScrollToPosition()
scrollTo();
scrollBy(); 
其中带有smooth的方法为带有滚动动画效果的方法。我们发现满足我们要求的就只有smoothScrollToPosition()方法。但是smoothScrollToPosition()只能将我指定的position的item显示在界面上,但不能将其置顶。我们查看RecyclerView的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);
    }

我们可以看到RecyclerView的滑动实际是调用了LayoutManager的滑动方法。 
我们查看LinearLayoutManager的smoothScrollToPosition()方法:

@Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
            int position) {
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext());
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }

发现LinearLayoutManager的smoothScrollToPosition()方法中new 了一个LinearSmoothScroller 的东西来控制其滑动,我们重写LinearSmoothScroller :

 class AdvertiseLinearSmoothScroller extends LinearSmoothScroller{

        public AdvertiseLinearSmoothScroller(Context context) {
            super(context);
        }

        /**
         *
         * @param viewStart RecyclerView的top位置
         * @param viewEnd RecyclerView的Bottom位置
         * @param boxStart item的top位置
         * @param boxEnd  item的bottom位置
         * @param snapPreference 滑动方向的识别
         * @return
         */
        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
            return boxStart-viewStart;//返回的就是我们item置顶需要的偏移量
        }

        /**
         * 此方法返回滚动每1px需要的时间,可以用来控制滚动速度
         * 即如果返回2ms,则每滚动1000px,需要2秒钟
         * @param displayMetrics
         * @return
         */
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return super.calculateSpeedPerPixel(displayMetrics);
        }
    }

我们重写calculateDtToFit()方法,即可实现smoothScrollToPosition()使item自动置顶功能. 
使用AdvertiseLinearSmoothScroller 重写LinearLayoutManager

class AdvertiseLinearSmoothScroller extends LinearSmoothScroller {

    public AdvertiseLinearSmoothScroller(Context context) {
        super(context);
    }

    /**
     *
     * @param viewStart RecyclerView的top位置
     * @param viewEnd RecyclerView的Bottom位置
     * @param boxStart item的top位置
     * @param boxEnd  item的bottom位置
     * @param snapPreference 滑动方向的识别
     * @return
     */
    @Override
    public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
        System.out.println("!###boxStart:"+boxStart+"!###viewStart:"+viewStart);
        return boxStart-viewStart+100;//返回的就是我们item置顶需要的偏移量
    }

    /**
     * 此方法返回滚动每1px需要的时间,可以用来控制滚动速度
     * 即如果返回2ms,则每滚动1000px,需要2秒钟
     * @param displayMetrics
     * @return
     */
    @Override
    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
        return super.calculateSpeedPerPixel(displayMetrics);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值