Home > Java > javaTutorial > Android slide-up gesture triggers and expands the click area without increasing the layout level

Android slide-up gesture triggers and expands the click area without increasing the layout level

高洛峰
Release: 2016-12-01 15:32:00
Original
1644 people have browsed it

In recent projects, it is necessary to realize the effect of gesture sliding up or clicking to slide out of the interface. The implementation is to use GestureDetector, and then judge in onFling, but encounter a problem: the gesture sliding up is for the entire layout, but if there is a separate pair View sets click monitoring, and sliding up gestures on the View will be invalid.

If this View itself is specifically used for clicking, there is no problem. Unfortunately, the size of this View is not large, so the click area is required to be expanded.

The first way we can think of is to wrap the View with a container, similar to RelativeLayout, LinearLayout, etc., but this will add one more layer to the layout.

Can we think of a way to expand the click area without adding layers?

GestureDetector is a gesture operation class provided by Android. It provides single click, double click, long press and other operations, and will pass these events to onTouch, so we can use this to determine which control triggered the click event.

But our problem is to expand the click area, not to identify the control, so we need to know which area the user's click action falls on.

Fortunately, MotionEvent provides the coordinates when the event occurs, so we can know the coordinates when we clicked, and further determine whether it is within the specified area.

MotionEvent is a crucial class for touch coding in Android. It provides user touch information on the screen. We can obtain the touch event type and touch coordinates through MotionEvent, and now it supports multi-touch, and can further obtain the touch index information.

Let’s start with the code:

@Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            int downX = (int)event.getX();
            int[] location = new int[2];
            ivMore.getLocationInWindow(location);
            if(Math.abs(downX - location[0]) <= 50){
                 ...
            }
        }
        return gestureDetector.onTouchEvent(event);
    }
Copy after login

We first get the touch event type through getAction of MotionEvent. The type of click is ACTION_DOWN, then get the x coordinate of the click through getX of MotionEvent, and then get the specified location through getLocationInWindow. The coordinates of the View, here specify the x-axis coordinate of the component plus or minus 50 (indicating the left and right range) as the trigger range.

We can now expand the click range without increasing the layout level.

It is very simple to use GestureDetector to realize slide-up judgment. You only need to implement the OnGestureListener interface and perform the operations we specify in the corresponding callback. However, because GestureDetector itself does not capture touch events, you must implement the onTouch event and call GestureDetector onTouchEvent, pass the corresponding event.

The sliding operation is performed in the onFling callback, which will pass in the two historical MotionEvents. We only need to determine whether their Y-axis coordinates have changed.

Android touch control is a quite large topic. We only need to find the corresponding quick solution on the premise of clarifying our own needs.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template