RecyclerView滑动指定item到屏幕最顶部

一开始调用RecyclerView提供的方法

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);
    }

发现当指定item滑动到屏幕可见时就停止了,即item可能位于屏幕顶部、中部或者底部。

由代码可以看出,RecyclerView的滑动方法是调用LayoutManager的smoothScrollToPosition方法

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

其中LinearSmoothScroller提供了滑动策略

    public static final int SNAP_TO_START = -1;

    /**
     * Align child view's right or bottom with parent view's right or bottom
     *
     * @see #calculateDtToFit(int, int, int, int, int)
     * @see #calculateDxToMakeVisible(android.view.View, int)
     * @see #calculateDyToMakeVisible(android.view.View, int)
     */
    public static final int SNAP_TO_END = 1;

    /**
     * <p>Decides if the child should be snapped from start or end, depending on where it
     * currently is in relation to its parent.</p>
     * <p>For instance, if the view is virtually on the left of RecyclerView, using
     * {@code SNAP_TO_ANY} is the same as using {@code SNAP_TO_START}</p>
     *
     * @see #calculateDtToFit(int, int, int, int, int)
     * @see #calculateDxToMakeVisible(android.view.View, int)
     * @see #calculateDyToMakeVisible(android.view.View, int)
     */
    public static final int SNAP_TO_ANY = 0;

重写LinearSmoothScroller的getVerticalSnapPreference方法

class LinearTopSmoothScroller extends LinearSmoothScroller{

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

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }

然后滑动的代码可以这样写

    void scrollItemToTop(int position){
        LinearSmoothScroller smoothScroller = new LinearTopSmoothScroller(this);
        smoothScroller.setTargetPosition(position);
        linearLayoutManager.startSmoothScroll(smoothScroller);
    }

完美解决。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近在开发的时候,遇到了需要通过代码使得RecyclerView能够滑到指定item顶部位置的需求,在查看源码之后...
    我啊翔1314阅读 21,845评论 1 15
  • 主要是在使用 RecyclerView 过程中遇到的细碎问题和解决方案。 简单使用 LinearLayoutMan...
    三流之路阅读 9,471评论 0 5
  • 5月26日,西北师范大学敦煌学院的学子们在敦煌小镇隆重举办了敦煌市第二届白马塔文化艺术节晚会。5月27日在敦煌白马...
    灰太狼_e542阅读 1,573评论 0 0
  • 你还记得那些对自己得承诺吗? 学会一种乐器,孤独的时候弹给自己听 练习一种运动,遇到心爱的人,展示曲线美 读书和旅...
    夏夜冬日阅读 1,274评论 0 0
  • 四月二十九日 小到中雨 蒙特利尔 今天一睁眼,外面就在淅沥沥的下着小雨,我照旧起来准备去做早饭,没多会,领导起来了...
    曾经小飞阅读 1,040评论 1 1