RecyclerView跳转指定Position后将其置顶

MyLinearLayoutManager

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) {
    
    
        LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
    
    
            @Override
            protected int getHorizontalSnapPreference() {
    
    
                return SNAP_TO_START;
            }

            @Override
            protected int getVerticalSnapPreference() {
    
    
                return SNAP_TO_START;
            }
        };
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
}

使用

MyLinearLayoutManager mLinearLayoutManager = new MyLinearLayoutManager(this);
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLinearLayoutManager);

猜你喜欢

转载自blog.csdn.net/qq_36881363/article/details/106436796