How to use RecyclerView.scrollToPosition() to move the position to the top of current view?

前端 未结 5 717
长情又很酷
长情又很酷 2020-12-01 03:03

The RecyclerView.scrollToPosition() is extremely strange. for example, supposed a RecyclerView named \"rv\".

  1. if now i

5条回答
  •  囚心锁ツ
    2020-12-01 03:14

    Below link might solve your problem:

    https://stackoverflow.com/a/43505830/4849554

    Just create a SmoothScroller with the preference SNAP_TO_START:

    RecyclerView.SmoothScroller smoothScroller = new 
    LinearSmoothScroller(context) {
       @Override protected int getVerticalSnapPreference() {
           return LinearSmoothScroller.SNAP_TO_START;
       }
    };
    

    Now you set the position where you want to scroll to:

    smoothScroller.setTargetPosition(position);
    

    And pass that SmoothScroller to the LayoutManager:

    layoutManager.startSmoothScroll(smoothScroller);
    

提交回复
热议问题