android 父子视图都设置了点击事件 点击子视图,父视图监听的事件不触发怎么办?我需要父子视图的时间都触发。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/rootView"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮"
/>
</FrameLayout>
this.findViewById(R.id.btn).setOnClickListener((view) -> {
LogUtil.debug("child click handle");
});
this.findViewById(R.id.rootView).setOnClickListener(view -> {
LogUtil.debug("rootview click handle");
});
自定义视图继承
viewgroup
,重载onInterceptTouchEvent
方法,在里面针对down/move/up
事件做条件判断可实现父子视图同时触发或仅子视图触发或仅父视图触发。代码如下: