Analysis of methods provided in RecyclerView
It provides scrollTo(), scrollBy(), scrollToPosition(), smoothScrollBy(), smoothScrollToPosition() methods. The functions of these methods will be explained in detail below.
1.scrollTo(int x, int y)
1 2 3 |
|
The meaning of this method in View is to slide to an absolute position, for example, the current position is (10,10), through scrollTo(30,30), and the final position is (30,30). However, as can be seen from the above scrollTo method source code, RecyclerView does not support sliding to an absolute position, because this method is an empty implementation, but the scrollToPosition(position) method is used instead.
2.scrollBy(int x, int y)
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The method is to slide to a relative position, for example, the current position is (10,10), through scrollBy(30,30), and the final position is (40,40). In this method, it will first determine whether mLayout is empty, mLayout is the LayoutManager held by RecyclerView, and then determine whether RecyclerView is sliding horizontally or vertically. If it is horizontal sliding, take (x, 0) and pass it to the scrollByInternal method, and vice versa. Then take (0,y).
3.scrollToPosition(int position)
1 2 3 4 5 6 7 8 9 10 11 |
|
This method is to slide to the specified position in RecyclerView (there is no animation during the sliding process). As can be seen from the above code, it is actually delegated to the scrollToPosition method of LayoutManager. The scrollToPosition method of LinearLayoutManager is:
4.smoothScrollBy(int dx, int dy)和smoothScrollBy(int dx, int dy, Interpolator interpolator)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
这个两个方法都是平滑滑动到相对位置。smoothScrollBy(int dx, int dy)方法间接调用的smoothScrollBy(int dx, int dy, Interpolator interpolator)方法,参数interpolator为插值器,它是一个接口,和动画使用的插值器一样。