Application details of Android touch events

preface

The last article talked about the transmission mechanism of Android touch events. For details, see here to learn about the transmission mechanism of Android touch events. Since we know the delivery and distribution of touch events in Android, it is important to know what problems it can solve and how to apply it in our actual development. We know that the principle is prepared to solve problems. The core of this article talks about how to solve the sliding conflict of view. This problem is very common in daily development. For example, the internal nested fragment view slides left and right, and the external is contained by a Scrollview, which can slide up and down. If the sliding conflict is not handled, the external sliding direction and internal sliding direction will be inconsistent.

catalogue

Common sliding conflict scenarios sliding conflict handling rules external interception method internal interception method summary

Common sliding conflict scenarios

Common sliding conflict scenarios can be simply divided into the following three types:

Scenario 1: the external sliding direction is inconsistent with the internal sliding direction scenario 2: the external sliding direction is consistent with the internal sliding direction scenario 3: nesting of the above two cases

As shown in the figure:

Scenario 1 is mainly the page sliding effect composed of viewpager and fragment. Almost all mainstream applications will use this effect. In this effect, you can switch pages by sliding left and right, and there is often a listview inside each page, resulting in sliding conflict. However, this sliding conflict is handled inside viewpager, so we don't need to pay attention to this problem when using viewpager. If you change viewpager to Scrollview, you must handle it manually, Otherwise, the result is that only one of the inner and outer layers can slide.

Scenario 2 is a little more complicated. When the inner and outer layers can slide in the same direction, there is obviously a logic problem. Because when the fingers is sliding, the system is too laggy to know which layer the user wants to slide, so when the fingers slide, there will be problems, or only one layer of sliding, or two layers of both inside and outside will slip but it is very Catton.

Scenario 3 is the nesting of scenario 1 and scenario 2, which is more complex. For example, there is a slidemenu effect outside, a viewpager inside, and a listview in each page of the viewpager. Although the sliding conflicts in scenario 3 seem very complex, they are the superposition of several single sliding conflicts, so they need to be disassembled one by one.

Handling rules for sliding conflicts

Generally speaking, no matter how complex the sliding conflict is, it has established rules. According to these rules, we can choose the appropriate method to deal with it.

For scenario 1, the processing rule is: when the user slides left and right, the external view needs to intercept the click event; when the user slides up and down, the internal view needs to intercept the click event. Specifically, it determines who will intercept the event according to whether the sliding is horizontal or vertical.

As shown in the figure:

In short, it is judged according to the distance difference between the horizontal direction and the vertical direction. If DX > Dy, it is horizontal sliding; if dy > DX, it is vertical sliding.

Scenario 2 is quite special. It cannot be judged according to the sliding angle, distance difference and speed difference. At this time, we need to find a breakthrough in business. For example, when in a certain state, we need an external view to respond to the user's sliding, and when in another state, we need an internal view to respond to the sliding of the view

For scenario 3, its sliding rules are also more complex. Like scenario 2, it also finds a breakthrough in business.

External interception method

The external interception method means that all click events are intercepted by the parent container first. If the parent container needs this event, it will be intercepted. If it does not need this event, it will not be intercepted. In this way, the problem of sliding conflict can be solved. The external interception method needs to rewrite the onintercepttouchevent method of the parent container and intercept it internally. The pseudo code is as follows:

First, action_ For the down event, the parent container must return false to ensure that subsequent move and up events can be passed to the child view. Whether to intercept or not is determined according to the move event. If the parent container intercepts, it returns true, otherwise it returns false.

Implement a custom control similar to viewpager and the effect of nested listview. The source code is as follows:

The interception condition in this case is that if the horizontal distance difference of the parent container is greater than the vertical distance difference during the sliding process, it will be intercepted. Otherwise, it will not be intercepted and continue to deliver events.

Internal interception method

The internal interception method means that the parent container does not intercept any events, and all events are passed to the child element. If the child element needs this event, it will be consumed directly, otherwise it will be handed over to the parent container for processing. This method is inconsistent with the event distribution mechanism in Android, and it needs to cooperate with the requestdisallowunterceptouchevent method to work normally, It is more complex than the external interception method. The pseudo code is as follows:

When the child element calls the requestdisallowintercepttouchevent (false) method, the parent element can continue to intercept the required event.

Previously, we used a custom similar viewpager. Now rewrite a listview. We can customize a listview called listviewex, and then modify the template code of the internal interception method.

At the same time, modify the external layout containing listviewex, and do not intercept the onintercepttouchevent event

This interception rule is also a comparison between the horizontal distance difference and the vertical distance difference of the parent container during sliding.

Summary

In general, there are three scenarios of sliding conflict: inconsistent internal and external directions, consistent internal and external directions, and nested front. No matter how complex the sliding conflict is, it can be split. According to certain rules, the first case can be solved according to the sliding distance difference, speed difference and angle difference. In the second and third cases, the breakthrough point can be found according to the business. One state of the business needs to respond, but it does not respond when switching to another state, The corresponding processing rules are obtained according to the business requirements. With the processing rules, you can proceed to the next step.

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