定义两个成员变量:
private int lastPosition = 0;
private int lastOffset = 0;
记录位置,
OnScrollListener,onScrollStateChanged()里添加:
View topView = mLayoutManager.getChildAt(0); //获取可视的第一个view
lastOffset = topView.getTop(); //获取与该view的顶部的偏移量
lastPosition = mLayoutManager.getPosition(topView); //得到该View的数组位置
恢复位置:
//mLayoutManager.scrollToPosition(lastPosition);
//这样更精确
((LinearLayoutManager)mLayoutManager).scrollToPositionWithOffset(lastPosition, lastOffset);