Custom behavior for Android coordinatorlayout advanced usage

Last time, I briefly talked about the basic usage of coordinatorlayout (an example of coordinatorlayout usage of Android feature). Among them, coordinatorlayout provides us with a new event processing method, behavior. Remember that string?

In fact, it is not a string resource, but it represents a class, a behavior. In fact, this thing can be customized.

First, let me see what it really is:

Behavior is an internal generic abstract class of coordinatorlayout. The view type specified in the inner class specifies which type of view can be used for behavior. Therefore, if there are no special requirements, you can directly specify view as view.

1. A view needs to control its own behavior according to the behavior of listening to another. At this time, we need to rewrite two methods:

2. Our view needs to change its state according to the scrolling behavior of the child views in the coordinatorlayout. Now we need to rewrite the following method:

Let's take a look at case 1. Let one view follow the behavior of another view to change the state. We define a behavior, called footerbehavior, with the following code:

In the custom behavior, the structure with parameters must be overloaded, because the structure is taken when using reflection to obtain the behavior in coordinatorlayout.

In layoutdependson, we set the state of the view to follow appbarlayout, that is, only changes in the state of appbarlayout will affect the view.

The next step is to make corresponding state changes to the view in ondependentviewchanged. In the code, the change we make is to move along the y-axis with the dependcy to achieve the purpose of display and hiding. The first layout is as follows:

activity_ Main.xml layout:

appbar_ The layout of main.xml is as follows:

content_ The layout of main.xml is as follows:

footer_ The layout of main.xml is as follows:

Look, on the footer_ In main.xml, we set

This is exactly the absolute path of our custom footerbehavior. Let's take a look at the renderings:

On the rendering, we can see that when we slide the screen up and down, the bottom footer layout and the title toolbar move together to achieve the effect of display and hiding.

After learning the first simple custom behavior, let's take a look at the second case, sliding. Because this changes our state according to the scrolling behavior of the sub view in the coordinatorlayout, we don't need to rewrite the two methods in case 1. Next, we use case 2 to achieve the above effect. Let's take a look at the following parameters:

Child: simply put, it is to use the child view of the current coordinatorlayout to respond to this behavior.

Target: the child view of coordinatorlayout, which causes scrolling. In fact, the state change of child is realized according to target.

In the method onstartnestedscroll, first obtain the distance between the target on the Y axis and the top of the screen, and then judge whether to scroll on the Y axis. In the method onnestprescroll, the scrolling distance is calculated from time to time according to the distance from the target to the top of the screen, and then the moving distance of the child is calculated according to the proportion. The screenshot is no different from the above comparison:

This is the basic custom behavior. The above is all the content of this article. I hope it will be helpful to your learning and support more 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
分享
二维码
< <上一篇
下一篇>>