Android monitors touch events in fragment

This paper introduces the implementation of monitoring fragment's touch event. If you have a better mechanism, you can leave a message. Let's see the detailed introduction below:

As we all know, our activity has ontouchevent method, which can be used to monitor touch events.

Touch event for activity

But for fragment, there is no such method. How can we implement it if we want to listen to touch events in fragment?

My method is to distribute touch events by myself.

Define an interface first

Create a list to store the listener

Then register, destroy and distribute by yourself

Fragment touch event distribution, which distributes the touch event to each fragment that can respond

In the fragment, we can use it in this way: initialize the listener, register with the activity, and listen for gesture actions in the listener.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>