使用smoothScrollToPosition跳转到第i项,如何让这个i项置顶
您的每一个用心回答,都会让这个世界变得更美好一些!
public class MyLinearSmoothScroller extends LinearSmoothScroller { public MyLinearSmoothScroller(Context context) { super(context); } @Override protected int getHorizontalSnapPreference() { return SNAP_TO_START; } @Override protected int getVerticalSnapPreference() { return SNAP_TO_START; // 将子view与父view顶部对齐 } }
public class MyLinearLayoutManager extends LinearLayoutManager { public MyLinearLayoutManager(Context context) { super(context); } public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public MyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { MyLinearSmoothScroller linearSmoothScroller = new MyLinearSmoothScroller(recyclerView.getContext()); linearSmoothScroller.setTargetPosition(position); startSmoothScroll(linearSmoothScroller); } }
自定义一下MyLinearLayoutManager 即可,主要是要修改SNAP_TO_START
自定义一下MyLinearLayoutManager 即可,主要是要修改SNAP_TO_START