美文网首页android
解决RecyclerView使用smoothScrollToPo

解决RecyclerView使用smoothScrollToPo

作者: 等一个人咖啡_8906 | 来源:发表于2020-11-10 14:34 被阅读0次

解决RecyclerView使用smoothScrollToPosition方法时,item顶部显示不全的bug

TopLinearSmoothScroller topLinearSmoothScroller;
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(context) {
       @Override
       public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
           super.smoothScrollToPosition(recyclerView, state, position);
           if (topLinearSmoothScroller == null) 
             topLinearSmoothScroller = new TopLinearSmoothScroller(recyclerView.getContext());
           topLinearSmoothScroller.setTargetPosition(position);
           startSmoothScroll(topLinearSmoothScroller);
       }
};
mRecyclerView.setLayoutManager(mLinearLayoutManager);

public class TopLinearSmoothScroller extends LinearSmoothScroller {
    public TopLinearSmoothScroller(Context context) {
        super(context);
    }

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

相关文章

网友评论

    本文标题:解决RecyclerView使用smoothScrollToPo

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