美文网首页Android 安卓技术分享程序员Android知识
RecyclerView自动滑动到指定的position

RecyclerView自动滑动到指定的position

作者: just_a_man | 来源:发表于2017-12-12 10:56 被阅读158次

    问题:

    一般来说我们会使用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);

    相关文章

      网友评论

        本文标题:RecyclerView自动滑动到指定的position

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