Android custom stickinessview sticky sliding effect

With the emergence of the design package, the Android interface has undergone great changes, and various sliding matching effects. Below, I will customize one of the viscous sliding. The effect diagram is as follows:

You can see the effect. Here I inherited linerlayout, which is more convenient. If it is ViewGroup, it will be a little more complex. It is divided into three parts:

1. Head1, top movable layout. 2.head2. The fixed head will not slide except the screen. 3. Slidable layout (here, it can only be listview, but it can also be any slidable view, as long as the time when the head can be slidable is given)

The difficulty of this stickinessview is to solve sliding conflicts and intercept events. Next, I'll come together.

1、 First, to determine when the headlayout can intercept events, it is necessary to determine the timing when the listview reaches the top and bottom.

The reason is very simple, because the gettop and getbottom methods of view are relative to the parent container. If you are familiar with the layout method, you must understand it.

2、 Knowing the timing of the event intercepted by headview, we need to find out when to intercept the click event and slide on this basis.

Let's talk about onintercepttouchevent (motionevent EV). This method will be called first. When an event sequence is intercepted once, the subsequent event actions of this event will not call this method again, that is, when the ViewGroup decides to intercept an event, it is destined to consume the subsequent event actions. The location status of headview is posted here

As for the details, you can draw a picture. Note that when intercepting the event sequence, the general action_ The down event cannot be intercepted, because if it is intercepted, it is meaningless, and subsequent events cannot be controlled. It is impossible to continue to pass the event sequence to childview.

3、 Move headview.

Here, in order to make the sliding smooth, I use the scroller class, which is a tool class specialized in elastic sliding. First initialize the constructor, call the startscroll() method (including four parameters: sliding x, sliding y, sliding x offset, sliding y offset), then refresh the view, and finally rewrite the computescroll() method,

OK, it's basically finished. We need to get the height of the headview for the first time. It's better to get it in onmeasure(), and only get it once, as shown below

In the onfinishinflate () method, the execution of this method indicates that all views have been added. Here, it is more appropriate for us to initialize.

Well, that's basically it. GitHub address: https://github.com/yzzAndroid/LianXinView

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
分享
二维码
< <上一篇
下一篇>>