Android custom view sliding deletion effect

View Slide delete renderings

Realize function

1. You can slide left and delete appears on the right. 2. Slide left. If more than half of the deletion occurs, let go to open the deletion, otherwise close the deletion. 3. The deletion function of wechat messages in the application scenario

Implementation principle

1. There is a listview 2 outside, an entry is a custom control, inheriting ViewGroup 1), a textview on the left, and a textview 2 outside the screen on the right, so it inherits ViewGroup

Implementation steps

1. Create a slidedeleteview class

1) . construction method to associate

2. Set the child view in the slidedeleteview in the layout file

SlideDeleteView height=80

TextView width:matchParent height:matchparent TextView

3. Override onmeasure to measure the child view

4. Override onlayout to lay out the child views

Note: set both leftview and rightview. Don't write them all as leftview

5. Set the sliding event ontouchevent of view to realize sliding 1). Consume the event to return true because you want to slide 2). Use viewdraghelper to realize sliding effect

matters needing attention:

Only the leftview can be slid. The rightview on the right cannot see it, so it cannot be slid

You can only set listening for a sliding view. When sliding, reset the layout of another view to follow the sliding

6. Override the listening of sliding events onviewpositionchanged to solve the problem of only lefview sliding

1) . the overridden method is in the sub implementation class of viewdraghelper.callback

2) . to implement the sliding event, you must return true in the trycaptureview method

7. Override clapviewpositionhorizontal horizontal position movement to solve the problem of left and right out of bounds

1. The return value is the sliding distance on the left when moving. 2. If the sliding control is leftview, it is out of bounds. 3. If the sliding control is rightview, it is out of bounds

8. When the hand is released, override the onviewreleased method to realize whether the rightview is open or closed when the sliding hand is released

1. When using the viewdraghelper to slide, call the invalidate method and call back the computescroll method. 2. Rewrite the computescroll method

1) . first determine whether to inherit the sliding

2) . use the compatible invalidate method to achieve uniform sliding

9. Click Delete event to delete rightview

1. Find the rightview on the right in the listview adapter. 2. Call the click event of rightview. 3. Delete the entry

1) Delete the data list. Remove (position) in the collection; 2) Update adapter notifydatasetchanged(); 3) Redraw the entire entry requestlayout();

matters needing attention

1. When rewriting the onlayout method and setting the layout for the rightview, there is always an error in writing leftview. 2. Return true in ontouchevent because the event is consumed. 3. When using viewdraghelper.callback, return true when rewriting trycaptureview. 4. In ontouchevent, instead of using scrollby or scrollto, use the viewdraghelper tool class, There is no need to judge the sliding distance

summary

1. First, the control is a custom view, not a composite control, because the rightview cannot be implemented on the right side of the screen if it is combined. 2. It inherits the ViewGroup in the custom view, because the leftview on the left and the rightview on the right are textviews and do not need to be drawn by themselves

Source code

Slidedelete source code

Mainactivity.java source code

Slidedeleteadapter.java source code

activity_ main.xml

item_ slide.xml

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