新出的RecyclerView网上的资料比较少,根据ListView以及参考了一下官方API得出。
定义两个成员变量:
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);
相关文章
- 史上最精简Android RecyclerView实现拖拽排序改变位置代码
- Android 自动滚动的RecyclerView,手动滑动和自动滑动无缝衔接,手动滑动时数据不重复
- 用SmoothScroller实现RecyclerView滚动到指定位置并置顶
- Android 记录和恢复ListView滚动的位置的三种方法
- Android 解决RecyclerView删除Item导致位置错乱的问题
- android Recyclerview仿京东,滚动屏幕标题栏渐变
- Android 记录和恢复ListView和GridView的滑动位置
- Android RecyclerView 记录恢复滚动位置
- android记录和恢复ListView滚动的位置
- android开发之记录ListView滚动位置