android event mechanism

I found a giant artificial intelligence learning website a few days ago, which is easy to understand and humorous. I can’t help but share it with everyone. Click to jump to the tutorial

One, touch event

The event mechanism is like an example in life

You are an employee of a company, with a supervisor above you, and a manager above the supervisor. For simplicity, your team has these three people. So if one thing is arranged at the top to be dealt with, what is the process?
Obviously, your manager should arrange this matter to your supervisor to deal with, and your supervisor will arrange this matter to you to deal with. After you finish this matter, you should report it to your supervisor, and then your supervisor will report to your manager
. Obviously, your supervisor and manager also have the authority to deal with this matter. If they think the matter is complicated and you can’t handle it, or if they take care of their subordinates, they may take care of the matter themselves. At this time, this matter is not. It will be
passed to the next level for processing. The process of handling this event is too easy to understand!

To deeply understand the event mechanism in android, you need to look at the following two aspects: event delivery and processing mechanism

Quoting the example from https://www.cnblogs.com/fuly550871915/p/4983682.html

A VewiGroupB is nested in ViewGroupA, and then a MyView is nested in ViewGroupB. So what happens when a touch event is passed over?

After the touch event is passed, ViewGroupA sees that there is still an employee in it that can be used, that is, ViewGroupB. If you don't need it for nothing, it will pass the event to ViewGroupB and tell him that you handled this event for me!
When I look at ViewGroupB, I’m not afraid. There is also an employee in me, MyView, who has to do the work for me, so it will pass this event to MyView for processing. When MyView looked at it, there was no way. I had no employees under my hand.
What should I do? I can only handle it myself (provided it has the ability to handle this event), so I handled the touch event. What about after processing? MyView is to report to ViewGroupB. I have done everything right. You can review it
and see how it goes . Once ViewGroupB reviews and finds it good, it will present the results to ViewGroupA. ViewGroupA re-reviewed and passed it. During this process, there may be several situations:
(1) MyView said that it’s over, my ability to handle this matter is not good, so I reported to VeiwGroupB, I did not handle it, please handle it, you are my boss , The ability is better than me. So ViewGroupB will come to help. Of course,
if ViewGroupB is not able to handle it, it can only feed back to VeiwGroupA and let it digest the event.
(2) It is also possible that MyView processing is very perfect. Once you report to ViewGroupB, ViewGroupB will not need to hand it over to ViewGroupA for review as soon as it is happy. I guarantee it to pass, so the incident is terminated directly.

In a professional term, the parent view manages the child view, passing from top to bottom, and whoever consumes it will terminate.

In ViewGroup, there are the following three methods
dispatchTouchEvent
onInterceptTouchEvent
onTouchEvent
In View, there are the following two methods
dispatchTouchEvent
onTouchEvent

For the interception of events, we mainly rewrite the OnInterceptTouchEvent and onTouchEvent methods. Two sentences can be summed up:
(1) For the delivery of events, the return result is true, which means interception, no further transmission, false, no interception, continue to pass down. The main target is the OnInterceptTouchEvent method.
(2) For event processing, the return result is true, which means interception and no longer passes up (that is, I processed it perfectly, and you don’t need to review me again!), the return result is false (the event was not successfully processed), continue Pass up.
The target is the onTouchEvent method.

2. Click event

The APIs involved are
dispatchKeyEvent
onKeyDown
onKeyUp
onKeyLongPress

For onKeyUp, if the return key is pressed, return true will not exit the interface.

The series of events are down–>down–>down–>... ->up
up is called only once.
For long press events, you need to set the return value of onKeyDown to true and enable tracking

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
    
    
		Log.e("TAG", "onKeyDown() action="+event.getAction()+" code="+event.getKeyCode());
		event.startTracking();//追踪事件, 用于长按监听
		return true;
	}

Guess you like

Origin blog.csdn.net/huangbaokang/article/details/112258678