In Android, the side pull and draw effect of items in listview is realized through viewdraghelper

Let's take a look at the effect picture of the custom control to be implemented today:

About the use of viewdraghelper, you can first read this article about the use of viewdraghelper

The general steps to implement the custom control are as follows:

1. The three parts used by viewdraghelper initialize viewdraghelper, pass touch events, and implement the viewdraghelper.callback abstract class

2. You need to create two direct sub views, foreground view and background view, which represent the composition of the layout of each item in the listview, as shown below:

Frontview displayed when not marked out:

Backview is displayed on the right after marking out:

The above two parts are the two direct child views to be included in the custom control

3. You need to obtain the width and height of the frontview. The width is actually the width of the screen, and the height is the height of each item in the listview; You also need to get the width of the backview, because this width is the maximum range of sideslip

4. The initial positions of frontview and backview need to be determined in the onlayout method, that is, only frontview is displayed by default. This implementation is also very simple. The left of frontview = 0 and the left of backview = right of frontview

5. It is necessary to synchronize the sliding of frontview and backview, that is, when sliding frontview, backview also needs to be marked out. Similarly, when sliding backview, frontview also needs to slide with it

6. It is necessary to solve whether the side pull drawing effect has animation effect. The smooth sliding animation can be easily realized through viewdraghelper

OK, go directly to the custom swipelayout source code:

How to use it?

When using this control, it must have 2 direct child controls, as shown in the following layout:

It's that simple. You can use it when you run. However, this only defines the swipelayout control. If you want to integrate it into the listview, you need to do further processing. For example, you can achieve the following effects:

Two points need to be considered:

1. In the custom swipelayout control, you need to handle three states: open, close and drag

2. A sideslip monitoring interface needs to be added to expose the opening, dragging, opening and closing of the current swipelayout. The interface definition is as follows:

The three states of swipelayout are represented by enum, that is, define the method 1 of receiving and obtaining swipeewlistener listeners

Add one more method in onviewpositionchanged method to handle drag listening

Finally, let's take a look at the test of mainactivity:

summary

The above is what Xiaobian introduced to you. In Android, the side pull and draw effect of listview item is realized through viewdraghelper. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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