Solution of Android sliding event conflict

Sliding is an indispensable part of Android. Multiple sliding will inevitably lead to conflict. For example, the most common thing is that listview is nested in Scrollview. The general method is to calculate the total height of listview, so you don't have to slide listview. For another example, a viewpager nested a fragment with a listview in it. Originally, there was a sliding conflict, but viewpager helped us solve this conflict internally. What if we want to solve the conflict ourselves.

There are two ways to solve this problem:

External interception method external interception method refers to that when there is a click event, it must pass through the parent container. If the parent container needs to be intercepted, it will be intercepted and processed by itself. If not, it will be passed to the next layer for processing. See an example below:

First, define a horizontal Scrollview ex that slides horizontally. Look at the main code

The main interception is to rewrite onintercepttouchevent

The down event does not need to be intercepted and returns false. Otherwise, the child view cannot receive the event and will be processed by the parent container. This is not desirable; The up event should also return false, otherwise the last child view cannot be received.

Look at the move event. When the horizontal sliding distance is greater than the vertical distance, it represents horizontal sliding and returns true. It is handled by the parent class, otherwise it is handled by the child view. Here, the move event is the main interception condition judgment. If you encounter conditions that are not as simple as horizontal and vertical conditions, you can change them here. For example, if the Scrollview nested the listview, the condition will become. When the listview slides to the bottom or top, it will return true and be handed over to the parent class for sliding. Otherwise, the listview itself will slide.

The ontouchevent is mainly used for sliding switching

An ordinary listview is nested in this, so that the problem of horizontal and vertical sliding conflict can be solved.

Other parts of the code can be downloaded if necessary

Internal interception method

The internal interception method is that the parent container does not intercept any events. All events are passed to the child view. If necessary, they are consumed directly and do not need to be passed to the parent container for processing

Next, rewrite a listview. Just rewrite a dispatchtouchevent method

Call getparent(). Requestdisallowintercepttouchevent (true) in the down event. This code means that the parent container of the view will skip onintercepttouchevent. Judge in move that if it slides horizontally, it will be handled by the parent container. The parent container only needs to change the previous onintercepttouchevent to the following, and everything else remains unchanged.

The final implementation effect is as follows. The two are implemented in two ways. The circle above is a simple custom view exercise

Download address: Android sliding event conflict

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