RecyclerView 開發筆記:精準恢復到原來瀏覽位置

項目中遇到了一個問題,一個 RecyclerView 列表切換到別的位置再切換回來的時候但願自動恢復到原來的位置。java

代碼以下:

第一步:定義兩個成員變量spa

private int music_last_position;
private int music_last_offset;
複製代碼

第二步:獲取最後位置和偏移量code

music_last_position = gridLayoutManager.findLastVisibleItemPosition();
music_last_offset = gridLayoutManager.findViewByPosition(music_last_position).getTop();
複製代碼

第三步:移動到該位置get

gridLayoutManager.scrollToPositionWithOffset(music_last_position, music_last_offset);
複製代碼
相關文章
相關標籤/搜索