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

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

推荐阅读更多精彩内容