Android expands the click area of view

Sometimes, after the effect is made according to the visual diagram, it is found that the click area is too small, it is not easy to click, and the user experience is certainly bad. Expanding the view will cause the whole visual image to become ugly. So is there any way to expand the click area without changing the view size?

The answer is yes!

To solve this problem, you must have a certain understanding of the event distribution mechanism of view.

Next, I will briefly introduce the event distribution mechanism of view to facilitate you to understand the following solutions.

In order to explain the whole mechanism more clearly, the following view is used to illustrate the click event distribution mechanism. The following figure shows a FrameLayout (ViewGroup), which contains an ImageView (view).

First customize a myframelayout, inherit the FrameLayout, and implement two click related interfaces; The specific codes are as follows:

Then, do similar operations for ImageView. The specific code is as follows:

It should be noted here that only ViewGroup has onintercepttouchevent method. Ordinary view does not have it. It cannot intercept events.

At this time, if we click the ImageView inside, what will be the output? The results are shown in the figure below.

What if you click on the outer layer?

0, 1 and 2 represent action respectively_ DOWN,ACTION_ UP,ACTION_ MOVE; It can also be seen that a click action includes a down, an up, and multiple move operations.

Let's look at the source code:

The above code makes the relationship between the three very clear. For a ViewGroup, after the click event is generated, it will be passed to it first. At this time, dispatchtouchevent will be called. If the onintercepttouchevent of the ViewGroup returns true, it means that it wants to intercept the event and will also be handed over to its ontouchevent for processing. If the onintercepttouchevent of the ViewGroup returns false, it will be passed to the child element, and the dispatchtouchevent of the child element will be called. This cycle will be repeated. This is consistent with the result shown in the figure above.

It is also noted here that if the code sets ontouchlistener, the ontouch method will be called first, and then ontouchevent will be called. Onclicklistener has the lowest priority, so onclick is called last.

Therefore, it can also be seen from the second result diagram that after ontouch exists, both ontouchevent and onclick methods will not be called.

I believe that by now, you have a certain understanding of the event distribution mechanism of view.

Back to the question raised at the beginning, is there any way to expand the click area of view?

Answer: set ontouchlistener in the parent view to intercept click events. Determine whether it is the event of the corresponding child view or the event of the parent view by judging the click location.

The specific implementation code is as follows:

Touchfactory encapsulates click events and determines whether to intercept click events by judging the click area.

The following is the specific implementation of myframelayout. Since it is a custom view, the variable myimageview must be empty, so it needs to be assigned.

Note: when onclicklistener is set for the child view and the click area is just inside the child view, this matter will be consumed. See, the interception processing of the parent view will be invalid. Therefore, once interception is selected to expand the click area, do not set click callback in the child view to consume Click events.

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