RecyclerView选择item滚动到顶部

来源:互联网 发布:北京java语言编程培训 编辑:程序博客网 时间:2024/04/27 15:44

代码块

实现SnappingLinearLayoutManager 继承LinearLayoutManager 。
public class SnappingLinearLayoutManager extends LinearLayoutManager {

public SnappingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {    super(context, orientation, reverseLayout);}@Overridepublic void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,                                   int position) {    RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());    smoothScroller.setTargetPosition(position);    startSmoothScroll(smoothScroller);}private class TopSnappedSmoothScroller extends LinearSmoothScroller {    public TopSnappedSmoothScroller(Context context) {        super(context);    }    @Override    public PointF computeScrollVectorForPosition(int targetPosition) {        return SnappingLinearLayoutManager.this                .computeScrollVectorForPosition(targetPosition);    }    @Override    protected int getVerticalSnapPreference() {        return SNAP_TO_START;    }}

}
“`
使用方法
1.Rv.setLayoutManager(new SnappingLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false))
2.Rv.smoothScrollToPosition(i);

若本文对你有帮助,希望能够评论下哈,这将鼓励博主继续写文章(^__^)

0 0
原创粉丝点击