1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
//监听RecyclerView滚动状态 mRecyclerView.addOnScrollListener( new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super .onScrollStateChanged(recyclerView, newState);
if (recyclerView.getLayoutManager() != null ) {
getPositionAndOffset();
}
}
}); /** * 记录RecyclerView当前位置
*/
private void getPositionAndOffset() {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
//获取可视的第一个view
View topView = layoutManager.getChildAt( 0 );
if (topView != null ) {
//获取与该view的顶部的偏移量
lastOffset = topView.getTop();
//得到该View的数组位置
lastPosition = layoutManager.getPosition(topView);
}
} /** * 让RecyclerView滚动到指定位置
*/
private void scrollToPosition() {
if (mRecyclerView.getLayoutManager() != null && lastPosition >= 0 ) {
((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(lastPosition, lastOffset);
}
} |
本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1899155,如需转载请自行联系原作者