美文网首页
RecyclerView的scrollToPosition/sm

RecyclerView的scrollToPosition/sm

作者: 带带我 | 来源:发表于2021-08-20 17:22 被阅读0次

    学废了,分享出来

    1、scrollToPosition滚动指定item到RecyclerView顶部

    这个就很简单,调用RecyclerView的layoutManager.scrollToPositionWithOffset(position, 0);即可

    2、smoothScrollToPosition滚动指定item到RecyclerView顶部

    这个是平滑带动画的滚动

    原理:recyclerView的smoothScrollToPosition方法中调用了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 :
    我们重写calculateDtToFit()方法,即可实现smoothScrollToPosition()使item自动置顶功能.

    public class StickyTopicScroller extends LinearSmoothScroller {
        public StickyTopicScroller(Context context) {
            super(context);
        }
    
        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
          //原本的返回值      
          //return super.calculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, snapPreference);
    
          //修改,返回item置顶的偏移量
          return boxStart - viewStart;
        }
    
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return super.calculateSpeedPerPixel(displayMetrics);
        }
    }
    

    如上方法实现了item自动置顶功能,我们自定义StickyTopicItemLayoutManager继承LinearLayoutManager 重写smoothScrollToPosition方法,方法中设置上面的StickyTopicScroller

    public class StickyTopicItemLayoutManager extends LinearLayoutManager {
        private Context mContext;
        public StickyTopicItemLayoutManager(Context context) {
            super(context);
            mContext = context;
        }
    
        public StickyTopicItemLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
    
        public StickyTopicItemLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
            StickyTopicScroller stickyTopicScroller = new StickyTopicScroller(mContext);
            stickyTopicScroller.setTargetPosition(position);
            startSmoothScroll(stickyTopicScroller);
        }
    }
    
    

    使用

    给recyclerView设置我们重写的StickyTopicItemLayoutManager
    recyclerView.setLayoutManager(new StickyTopicItemLayoutManager(this));
    调用平滑滚动即可实现我们想要的置顶
    recyclerView.smoothScrollToPosition(position);

    说明
    smoothScrollToPosition置顶摘自这篇文章---RecyclerView 滚动到指定position,并置顶

    相关文章

      网友评论

          本文标题:RecyclerView的scrollToPosition/sm

          本文链接:https://www.haomeiwen.com/subject/aebtiltx.html