1、事件分发机制
2、Activity启动流程
1、事件分发机制
参考资料:
https://blog.csdn.net/ClAndEllen/article/details/79365369
https://www.jianshu.com/p/555ffeb64e68
<com.lak.app.test.java.view.JScrollView >
<com.lak.app.test.java.view.JLinearLayout>
<com.lak.app.test.java.view.JTextView
android:clickable="true" />
</com.lak.app.test.java.view.JLinearLayout>
</com.lak.app.test.java.view.JScrollView>
// activity
class TestJavaActivity : AppCompatActivity() {
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("activity ---------- dispatchTouchEvent ----- ${eventActionToString(ev)}")
val result = super.dispatchTouchEvent(ev)
HiLog.d("activity ---------- dispatchTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
HiLog.d("activity ---------- dispatchTouchEvent ----- ${eventActionToString(event)}")
val result = super.onTouchEvent(event)
HiLog.d("activity ---------- dispatchTouchEvent ----- ${eventActionToString(event)} ----- $result")
return result
}
}
class JScrollView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ScrollView(context, attrs, defStyleAttr) {
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("scrollview ---------- dispatchTouchEvent ----- ${eventActionToString(ev)}")
val result = super.dispatchTouchEvent(ev)
HiLog.d("scrollview ---------- dispatchTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("scrollview ---------- onInterceptTouchEvent ----- ${eventActionToString(ev)}")
val result = super.onInterceptTouchEvent(ev)
HiLog.d("scrollview ---------- onInterceptTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
override fun onTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("scrollview ---------- onTouchEvent ----- ${eventActionToString(ev)}")
val result = super.onTouchEvent(ev)
HiLog.d("scrollview ---------- onTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
}
class JLinearLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("linearlayout ---------- dispatchTouchEvent ----- ${eventActionToString(ev)}")
val result = super.dispatchTouchEvent(ev)
HiLog.d("linearlayout ---------- dispatchTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("linearlayout ---------- onInterceptTouchEvent ----- ${eventActionToString(ev)}")
val result = super.onInterceptTouchEvent(ev)
HiLog.d("linearlayout ---------- onInterceptTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
// HiLog.d("linearlayout ---------- onInterceptTouchEvent ----- ${eventActionToString(ev)}")
// val result = true
// HiLog.d("linearlayout ---------- onInterceptTouchEvent ----- ${eventActionToString(ev)} ----- $result")
// return result
}
override fun onTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("linearlayout ---------- onTouchEvent ----- ${eventActionToString(ev)}")
val result = super.onTouchEvent(ev)
HiLog.d("linearlayout ---------- onTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
}
class JTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("textview ---------- dispatchTouchEvent ----- ${eventActionToString(ev)}")
val result = super.dispatchTouchEvent(ev)
HiLog.d("textview ---------- dispatchTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
override fun onTouchEvent(ev: MotionEvent?): Boolean {
HiLog.d("textview ---------- onTouchEvent ----- ${eventActionToString(ev)}")
val result = super.onTouchEvent(ev)
HiLog.d("textview ---------- onTouchEvent ----- ${eventActionToString(ev)} ----- $result")
// HiLog.d("textview ---------- onTouchEvent ----- ${eventActionToString(ev)}")
// val result = false
// HiLog.d("textview ---------- onTouchEvent ----- ${eventActionToString(ev)} ----- $result")
return result
}
}
// 运行结果: 以 down 事件为例
activity ---------- dispatchTouchEvent ----- down
scrollview ---------- dispatchTouchEvent ----- down
scrollview ---------- onInterceptTouchEvent ----- down
scrollview ---------- onInterceptTouchEvent ----- down ----- false
linearlayout ---------- dispatchTouchEvent ----- down
linearlayout ---------- onInterceptTouchEvent ----- down
linearlayout ---------- onInterceptTouchEvent ----- down ----- false
textview ---------- dispatchTouchEvent ----- down
textview ---------- onTouchEvent ----- down
textview ---------- onTouchEvent ----- down ----- true
textview ---------- dispatchTouchEvent ----- down ----- true
linearlayout ---------- dispatchTouchEvent ----- down ----- true
scrollview ---------- dispatchTouchEvent ----- down ----- true
activity ---------- dispatchTouchEvent ----- down ----- true
事件分发流程
2、Activity启动流程
深入理解Activity启动流程和AMS框架(一)
深入理解Activity启动流程和AMS框架(二)
深入理解Activity启动流程和AMS框架 - 完整
网友评论