Solutions to multiple sliding conflicts in Android

1、 Foreword

There are two solutions to sliding in Android: external interception method and internal interception method.

There are also two scenarios of sliding conflict: horizontal and vertical sliding conflict and co directional sliding conflict.

So I wrote four examples to learn how to solve sliding conflicts. These four examples are: external interception method to solve horizontal and vertical conflicts, external interception method to solve co directional conflicts, internal interception method to solve horizontal and vertical conflicts, and internal interception method to solve co directional conflicts.

Effect drawing first:

2、 Actual combat

1. External interception method to solve horizontal and vertical conflicts

The idea is to override the onintercepttouchevent method of the parent control, and then decide whether the parent control intercepts events according to specific requirements. If true is returned for interception, false is returned for non interception. If the parent control intercepts the event, the corresponding event processing is performed on the ontouchevent of the parent control.

My example is a horizontal sliding ViewGroup, which contains three vertical sliding listviews. Below I attach the code, horizontalex.java:

Calling code:

In fact, the main idea of external interception is to rewrite onintercepttuchevent.

This is almost a template for external event interception. It must not be in action here_ Return true in down, otherwise the child view will not have a chance to get the event, because if it is in action_ True is returned when down, and the dispatchtouchevent of the same event sequence ViewGroup will not call the onintercepttouchevent method.

And in action_ False is returned in up because if the parent control intercepts action_ Up, the child view will not get the up event, and the onclick method of the child view will be affected. But this has no effect on the parent control, because if it is the child aciton of the parent control_ Events are intercepted in move, and their up events must also be handed over to it for processing, because there is a law that once the parent control intercepts events, all events in the same event sequence will be handed over to it for processing. This conclusion has been analyzed in my last article.

The last is in action_ Whether to intercept is determined according to the demand in move.

2. Internal interception method to solve horizontal and vertical conflicts

Internal interception mainly depends on the requestdisallowintercepttouchevent method of the parent control. In fact, I analyzed this method in my previous article. It sets a flag of the parent control (flag_disallow_interrupt)

This flag can determine whether the parent control intercepts events. If this flag is set, it will not intercept. If this flag is not set, it will call onintercepttouchevent() of the parent control to ask whether the parent control intercepts events. But this flag is not valid for the down event.

You can refer to the source code:

So if we want to use the internal interception method to intercept events.

Step 1:

a. We want to override the onintercepttouchevent of the parent control in action_ False is returned when down. If the child view is responsible, it will be powerless to call requestdisallowintercepttouchevent.

b. In addition, all other events return true, which gives the child view the right to intercept events.

Step 2:

Determine whether to let the parent control intercept events in the dispatchtouchevent of the child view.

a. First in motionevent.action_ Down: mhorizontalex2.requestdisallowintercepttouchevent (true) is used when;, If you are responsible, the next event will be handed over to the parent control.

b. Then in motionevent.action_ Move: decide whether to call mhorizontalex2.requestdisallowintercepttouchevent (false) according to the business logic; To determine whether the parent control intercepts events.

Upper Code: horizontalex2.java:

ListViewEx.java

Calling code:

So far, the two interception methods have been learned. Let's learn how to solve the same direction sliding conflict.

In fact, the idea is the same as the above two examples, but the logic used to judge whether to intercept is different.

The following example is a drop-down refresh control.

3. External interception to solve the same direction sliding conflict

RefreshLayoutBase.java

It is an abstract class and needs to write subclasses to inherit the Istop () and isbottom () methods. An implementation class is given below:

4. Internal interception method to solve the same direction sliding

It is also a drop-down refresh component. Because the implementation principle is the same, it is written more casually. Mainly if the sliding conflict is solved.

RefreshLayoutBase2.java

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