Android imitation wechat list sliding deletion how to realize sliding list swipelistview

Following the previous article, this article mainly talks about how to implement the swipelistview of the sliding list.

The previous part completed the sliding control swipeitemview. This control is a custom ViewGroup. As an item of the list, it provides some methods for the list, so that the swipeitemview can slide its view content, and there will be smooth animation effect in the sliding process. The swipelistview in this article is the concrete implementation of this list. Of course, the swipelistview inherits from the listview. In order to realize the functions we need, the focus is to rewrite the methods ontouchevent() and onintercepttouchevent() of the listview. Let's start with ontouchevent():

In the above code, first analyze the user's operation of sliding open an item, which is represented by action_ Down starts with a series of actions_ Move to action_ Up as the end, so in action_ In the down event, we first record the initial event positions, mcactiondownx and mcactiondowny; Then act_ In the move event, we must first make a judgment. This judgment is divided into two steps. The first step is to judge the action_ Under the move event, the positions curx and cury of the current event are on the x-axis and on the Y-axis and action_ Does the distance of the position recorded in down exceed touch_ The value of slope, which is the threshold used by Android to judge whether to slide once. In the second step, we need to further judge whether the user slides the whole list vertically or a certain item left and right. The logical judgment here is simple. If it exceeds touch_ In the case of slop threshold, if the distance in the x-axis direction is greater than the y-axis, we think that the user slides a single item left and right, and vice versa, the entire list vertically. Here, we use three states to distinguish, direction_ Unknow indicates that the current sliding operation has not been carried out. It is necessary to judge whether left-right sliding or vertical sliding, direction_ Horizontal indicates that the current sliding operation is determined to be left-right sliding, direction_ Vertical indicates that it is determined as longitudinal sliding. Once the sliding operation is determined, it is in action_ Before up processing, we all think that users slide in the same direction; ACTION_ In the up event, reset the sliding operation status to direction_ Unknown for the next decision and this action_ When processing the up event, if the position mlastshowingpos of the currently slid item is not - 1, it means that it is currently a sliding operation. This time, think carefully. The user may leave the screen without completely sliding the item. At this time, we should judge whether the slid item should be fully opened or closed, The logical judgment here is that if the item has slid away more than half its width, open it completely, otherwise close it. The specific code of ensureifittemopenorhide() is as follows:

The logic judgment of the first user sliding operation is finished. The next is the second time. Why the second time? For the first time, the user slides a single item open and touches the screen again. This time, we have to judge again. First, if action_ The position where the down occurs is within the range of the button displayed by the item slide, indicating that the current user clicks this button, so we will not do additional processing, but directly submit it to the default logic processing of the list; Second, if action_ The position where the down occurs is not within the button range displayed after the item is slid open. How does it mean that the current user is only in other ranges of the operation list? Here, we will close the currently open item and cancel the subsequent touch event. Here, we will intercept the action in_ The down event. You need to override the onintercepttouchevent() method of listview. The code is as follows:

The above mcancelmotionevent is used to determine whether the subsequent touch event needs to be cancelled in ontouchevent(), and how to determine the current action during that period_ Whether the down event occurs within the scope of the button, use the following code:

The rest is how to open or close an item. The code is as follows:

The above scrolltowithanimation () method is the method we implemented in the last blog to move the item with animation effect.

In this way, the overall implementation scheme of the whole simulated wechat sliding deletion operation has been explained. For specific details, you can check the source code of the project. I have uploaded the source code to my GitHub home page: https://github.com/YoungLeeForeverBoy/SlidingListViewPlus 。

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