Saturday, September 26, 2020

Android custom view only receives ACTION_DOWN solution

 Insert picture description here

When customizing View in Android development, the dispatchTouchEvent method is often rewritten, but in some cases, only ACTION_DOWN can be obtained, but ACTION_MOVE and ACTION_UP cannot be obtained.

The solution to the custom view receiving only ACTION_DOWN is
to add the following sentence in the custom View constructor:

setClickable(true);

Or add the following parameters to the layout:

android:clickable="true"

Or override the onTouchEvent method and return true.

@Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

So you can get the response of ACTION_MOVE and ACTION_UP in the dispatchTouchEvent method.

As for the principle, I am not sure, and I don't want to figure it out. There are too many contents in this android system and too many pits. You can only encounter problems and solve them, otherwise the development progress will be too slow!

No comments:

Post a Comment