How to tell RecyclerView to start at specific item position

前端 未结 6 2178
难免孤独
难免孤独 2021-02-20 10:43

I want my RecyclerView with LinearLayoutManager to show up with scroll position at specific item after adapter got updated. (not first/last position) Means the at first (re-)lay

6条回答
  •  野性不改
    2021-02-20 11:27

    If you want to scroll to a specific position and that position is the adapter's position, then you can use StaggeredGridLayoutManager scrollToPosition

    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
           staggeredGridLayoutManager.scrollToPosition(10);
           recyclerView.setLayoutManager(staggeredGridLayoutManager);
    

    If I understand the question, you want to scroll to a specific position but that position is the adapter's position and not the RecyclerView's item position.

    You can only achieve this through the LayoutManager.

    rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter);
    

提交回复
热议问题