RecyclerView自动滑动到指定的position

问题:

一般来说我们会使用RecyclerView的smoothScrollToPosition(int position) 方法来实现自动滚动,但是这个方法最大的问题就是,一旦目标position的item出现在屏幕中,列表就不会继续滚动,这也就造成了一种“BUG”:如果目标position的item原本处于列表下方,且没有在屏幕中出现,调用smoothScrollToPosition(int position)方法,会导致目标position的item滑动到屏幕下方最后一个可见位置的时候就停止滑动,在大多数需求中,这并不是我们想要的效果。

解决:

这是在StackOverflow上找到的解决办法:

RecyclerView.SmoothScrollersmoothScroller = newLinearSmoothScroller(this){

@Override

protected intgetVerticalSnapPreference() {

        returnLinearSmoothScroller.SNAP_TO_START;

    }

};

LinearLayoutManager mManager = new LinearLayoutManager(context);

smoothScroller.setTargetPosition(position);

mManager.startSmoothScroll(smoothScroller);

    原文作者:just_a_man
    原文地址: https://www.jianshu.com/p/c61083ff2c2e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞