Buttons control the sliding of RecyclerView

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

public void scrollTo(int x, int y) {

    Log.w("RecyclerView", "RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead");

}

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

public void scrollBy(int x, int y) {

    if(this.mLayout == null) {

        Log.e("RecyclerView", "Cannot scroll without a LayoutManager set. Call setLayoutManager with a non-null argument.");

    } else if(!this.mLayoutFrozen) {

        boolean canScrollHorizontal = this.mLayout.canScrollHorizontally();

        boolean canScrollVertical = this.mLayout.canScrollVertically();

        if(canScrollHorizontal || canScrollVertical) {

            this.scrollByInternal(canScrollHorizontal?x:0, canScrollVertical?y:0, (MotionEvent)null);

        }

 

    }

}

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

public void scrollToPosition(int position) {

    if(!this.mLayoutFrozen) {

        this.stopScroll();

        if(this.mLayout == null) {

            Log.e("RecyclerView", "Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.");

        } else {

            this.mLayout.scrollToPosition(position);

            this.awakenScrollBars();

        }

    }

}

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

public void smoothScrollBy(int dx, int dy) {

    this.smoothScrollBy(dx, dy, (Interpolator)null);

}

 

public void smoothScrollBy(int dx, int dy, Interpolator interpolator) {

    if(this.mLayout == null) {

        Log.e("RecyclerView", "Cannot smooth scroll without a LayoutManager set. Call setLayoutManager with a non-null argument.");

    } else if(!this.mLayoutFrozen) {

        if(!this.mLayout.canScrollHorizontally()) {

            dx = 0;

        }

 

        if(!this.mLayout.canScrollVertically()) {

            dy = 0;

        }

 

        if(dx != 0 || dy != 0) {

            this.mViewFlinger.smoothScrollBy(dx, dy, interpolator);

        }

 

    }

}

这个两个方法都是平滑滑动到相对位置。smoothScrollBy(int dx, int dy)方法间接调用的smoothScrollBy(int dx, int dy, Interpolator interpolator)方法,参数interpolator为插值器,它是一个接口,和动画使用的插值器一样。

Guess you like

Origin blog.csdn.net/qq_41943812/article/details/106412638