Explain the nestedscrolling mechanism in Android to take you through nested sliding

1、 Overview

Android provides you with two amazing classes in the support.v4 package:

If you have never heard of these two classes, it doesn't matter. Listen to me slowly, and you will understand what these two classes can be used for. I believe everyone has seen or used coordinatorlayout. This class can easily help us complete some dazzling effects, such as the following:

This effect is very suitable for using nestedscrolling mechanism. In fact, this mechanism is also used behind coordinatorlayout. So, I believe you already understand what this mechanism can be used for.

But I believe you have another problem

Well, let's briefly analyze:

According to the above figure:

Suppose we understand it according to the traditional event distribution. First, we slide the content area below, but the external ViewGroup is moving. Therefore, according to the traditional way, it must be the external parent that intercepts the internal child events; However, in the above effect drawing, when the parent slides to a certain extent, the child starts sliding again, and the whole process is uninterrupted. It is impossible to do this from the perspective of normal event distribution (do not call the distribution event manually, do not send the event manually), because after the parent intercepts, there is no way to hand over the event to the child. Event distribution is equivalent to a hammer deal for interception. As long as it is intercepted, the next events of the current gesture will be handed over to the parent (interceptor) for processing.

However, the nested scrolling mechanism is easy to handle, so an in-depth study of this mechanism will help us write some special effects during nested sliding; Second, I paved the way for the analysis of coordinatorlayout~~~

PS: don't delve into the specific V4 version you added. If you don't have the above two classes in V4, upgrade your V4 version. The term nestedscrolling mechanism is a personal name. It's not clear whether the official calls it that. Don't delve into it.

2、 Expected effect

Of course, the explanation of these two classes must be supported by cases, otherwise it will be too empty. Fortunately, I have a very good case to describe:

A long time ago, I wrote this article:

Android realizes the effect of 360 software details page through custom controls

It is written completely in the traditional way, and some very special processing has been done for continuous sliding, such as manually distributing down event classes. If you are interested, you can read it.

The rendering is as follows:

Today, we will use this effect as a case of nestedsroll mechanism. Finally, we will briefly analyze the source code. In fact, the source code is relatively simple~~

PS: coordinatorlayout can easily achieve this effect. Subsequent articles will also analyze coordinatelayout.

3、 Realize

The above renderings are divided into three parts: Top layout; Viewpager indicator in the middle; And the recyclerview at the bottom;

Recyclerview is actually the implementation class of nestedscrollingchild, so the main role of this example is to implement nestedscrollingparent

(1) Layout file

First preview the layout file and have a general layout in mind:

Stickynavlayout is directly inherited from LinearLayout, and the setting is orientation = "vertical", so it is intuitive that the controls are arranged vertically in order. As for measurement, some special processing needs to be done, because it is not the focus of this article. You can check the source code or the articles mentioned above.

(2) Implement nestedscrollingparent

Nestedscrollingparent is an interface. To implement it, you need to implement the following methods:

Before writing the specific implementation, briefly introduce the above methods to be used:

The main focus is on these three methods~

Here, the internal view does not have to be a direct child view, as long as it is an internal view.

Let's take a look at our specific implementation:

The above code can achieve the following effects:

For the fling method, we use overscroll's fling method. For boundary detection, we rewrite the scrollto method:

For a detailed explanation, please refer to the article mentioned above, which will not be repeated here.

Here, we can see that the nestedscrolling mechanism is very simple:

This is the view inside nestedscrollingparent. When sliding, DX and Dy will be first passed to nestedscrollingparent. Nestedscrollingparent can decide whether to consume it. Generally, some or all of it will be consumed according to the demand (but there is no actual constraint here. You can write how much it consumes, which may have a certain impact on the internal view).

The comparison between the vernacular and the original event distributor is like this (for the next gesture in the normal process):

The specific source code will be more complex than this blog post, because it involves touching some interactions in the non internal view area. If it is not the focus of this blog post, you can refer to the source code.

4、 Principle

The principle is to see when the internal view calls back various methods of nestedscrollingparent, and directly locate the ontouchevent of the internal view:

You can see:

ACTION_ Down called startnestedscroll; ACTION_ DispatchNestedPreScroll is invoked in MOVE. ACTION_ Up may trigger fling to call resettouch.

Startnestedscroll internal actually:

Go to find nestedscrollingparent, and then call back onstartnestedscroll and onnestedscrollaccepted.

The onnestedprescroll method will be called back in dispatchnestedprescroll, and the onnestedscroll method will also be called back in the internal scrollbyinternal.

The onnestedprefling and onnestedflying methods are called back in flying.

In resettouch, onstopnestedscroll will be called back.

In fact, there is no paste in the code. You can see at a glance when you directly find ontouchevent. The method name called is the dispatchnestedxxx method. In fact, it is implemented internally through nestedscrollingchildhelper.

Therefore, if you need to implement the internal view cooperating with nestedscrollingparent, remember to implement nestedscrollingchild, and then internally use nestedscrollingchildhelper as an auxiliary class to encapsulate the core methods. You only need to pass in parameters and call methods in the appropriate actual situation.

OK, such a mechanism must be tried. Many sliding related effects can be realized by this mechanism;

Source address:

GitHub address: https://github.com/hongyangAndroid/Android-StickyNavLayout

Local download: http://xiazai.jb51.net/201705/yuanma/Android-StickyNavLayout (jb51.net).rar

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>