Detailed explanation of nestedscrolling sliding mechanism in Android

1. Nestedscrolling is now used in many places. To make a better sliding transformation, we basically use this. Let's have a simple understanding.

2. Nestedscrolling mechanism enables parent and child views to cooperate in scrolling mode. Its basic process is as follows:

To implement such an interaction mechanism, the parent view must first implement the nestedscrollingparent interface, while the child view needs to implement the N Ernst slave rollingchild interface. In this mechanism, the child view is the initiator, and the parent view accepts the callback and responds.

Here are some key classes and interfaces

Some new system views have implemented the above two interfaces for us, that is, they support nestedscrolling, for example:

Nestedscrollview has implemented nestedscrollingchild and nestedscrollingparent interfaces

Recycleview has implemented nestedscrollingchild

Coordinatorlayout implements nestedscrollingparent

Nestedscrollingchild interface

Start the nested scrolling process (in fact, some preparations have been made before nested scrolling).

When a parent view capable of nested scrolling with the current child view is found, the return value is true (returns: true if a cooperative parent was found and needed scrolling has been enabled for the current situation).

This method is called before the child view rolls itself, asking whether the parent view will scroll before sub view.

The first two parameters of this method are used to tell the parent view the distance to scroll this time; The third and fourth parameters are used for the child view to obtain the distance consumed by the parent view and the offset of the parent view position.

The first and second parameters are input parameters, that is, conventional function parameters. When calling a function, we need to pass the exact value. The third and fourth parameters are output parameters. When calling the function, we only need to pass the container (here are two arrays). After the call, we can get the output value of the function from the container.

This method returns true if the parent consumes part or all of the distance.

After the child view has rolled itself, call this method to ask whether the parent view has to scroll the remaining (unconsumed).

The first four parameters are input parameters, which are used to tell the distance that the parent view has consumed and has not consumed. The last parameter is output parameter, which is used for the child view to obtain the offset of the parent view position.

Return value: (there may be ambiguity in the translation, and the original text is directly released) true if the event was dispatched, false if it could not be dispatched

Finally, the stopnestedscroll () method corresponds to startnestedscroll (int axes) to end the nested scrolling process; The two methods related to inertial scrolling are similar to the two methods related to touch scrolling, which will not be repeated here.

Nestedscrollingparent Interface Overview

From the naming, we can see that these are callback methods. When the method in nestedscrollingchild is called, the corresponding method in nestedscrollingparent will be called back. The specific correspondence between methods is as follows:

From the above interfaces and methods, we can get some simple processes

3. Customize nestedscrolling control

Let's see the effect first

Let's first look at the layout file activity_ main.xml

Layout files are simply nested. Mynestedscrollparent inherits LinearLayout and implements the nestedscrollingparent interface. The same is true for mynestedscrollchild. Let's take a look at the class mynestedscrollchild first.

MyNestedScrollChild.java

Mainly in OnTouchEvent, we have called startNestedScroll () and dispatchNestedPreScroll () methods successively, and have completed the NestedScrollingParent interface method with helper.

MyNestedScrollParent.java

Mynestedscrollparent mainly implements the following functions

① In onstartnestedscroll(), judge which child view the parameter target is and the direction of scrolling, and then decide whether to cooperate with it for nested scrolling

② . obtain the distance to scroll in onnestedprescoll(), decide whether to scroll according to the situation, and finally store the consumed distance in the consumed array and return it to child

That's it. It's very simple. Do you have it? Let's see the effect we want to achieve next, as shown in the figure:

Source code: mynestedscrolling_ jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>