记录一次事件分发处理

2020-05-09  本文已影响0人  朱_c713

项目组其他兄弟接手以前老代码,遇到一个点击问题。


image.png

a控件(tv_chatcontent_main_layout) 包含一个b(tv_chatcontent)控件,他在A上设置了长按事件。想实现,无论点A,B区域都能触发长按。

这里手动哭,这不是本来就应该是这样的么?!
可是我看了他的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="0dp"
    android:descendantFocusability="blocksDescendants"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingRight="10dp"
    android:paddingBottom="5dp">

    <FrameLayout
        android:id="@+id/iv_userhead_layout"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true">

        <ImageView
            android:id="@+id/iv_userhead"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/default_avatar_for_chattingui_40_3x"
            android:clickable="true"
            android:focusable="false"
            android:scaleType="fitXY" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/chatting_list_view_circle_hold_mask_35_35_3x"
            android:gravity="center"></ImageView>

    </FrameLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dp"
        android:layout_toLeftOf="@id/iv_userhead_layout"
        android:gravity="center_vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_sendtime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="5dp"
                android:padding="0dp"
                android:paddingLeft="4dp"
                android:paddingTop="0dp"
                android:paddingRight="4dp"
                android:paddingBottom="0dp"
                android:text="09:20"
                android:textColor="#cfd1c6"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/tv_ruan_read_hint"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="5dp"
                android:padding="0dp"
                android:paddingLeft="4dp"
                android:paddingTop="0dp"
                android:paddingRight="4dp"
                android:paddingBottom="0dp"
                android:text="悄悄话"
                android:textColor="#cfd1c6"
                android:visibility="gone"
                android:textSize="10sp" />

        </LinearLayout>

        <ImageView
            android:id="@+id/tv_chat_send_faild"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:clickable="false"
            android:focusable="false"
            android:src="@drawable/chatting_list_view_item_msg_send_error" />

        <LinearLayout
            android:id="@+id/tv_chatcontent_main_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:background="@drawable/chatto_bg"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center_vertical"
            android:lineSpacingExtra="2dp"
            android:longClickable="true"
            android:minWidth="80dp"
            android:minHeight="30dp"
            android:paddingLeft="10dp"
            android:paddingTop="8dp"
            android:paddingRight="15dp"
            android:paddingBottom="8dp">

            <TextView
                android:id="@+id/tv_chatcontent"
                style="@style/text_witheShadow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center"
                android:autoLink="all"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:longClickable="true"
                android:padding="0dp"
                android:text="11111111111111"
                android:textColor="@color/black"
                android:textColorLink="#2782d7"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

核心代码:

   <LinearLayout
            android:id="@+id/tv_chatcontent_main_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:background="@drawable/chatto_bg"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center_vertical"
            android:lineSpacingExtra="2dp"
            android:longClickable="true"
            android:minWidth="80dp"
            android:minHeight="30dp"
            android:paddingLeft="10dp"
            android:paddingTop="8dp"
            android:paddingRight="15dp"
            android:paddingBottom="8dp">

            <TextView
                android:id="@+id/tv_chatcontent"
                style="@style/text_witheShadow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center"
                android:autoLink="all"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:longClickable="true"
                android:padding="0dp"
                android:text="11111111111111"
                android:textColor="@color/black"
                android:textColorLink="#2782d7"
                android:textSize="14sp" />
        </LinearLayout>

尝试方案:

首先想到的就是,去除,内层textview android:longClickable="true" 属性。
因为我想,正常的view嵌套,不久完全可以满足产品需求么。

结果:失败!!

解决方案:

这些去除竟然没起作用,显示是哪个隐秘的地方出问题了。竟然不能有正常的的点击效果。
果断换思路,自定义外层LinearLayout,在拦截的地方,返回为true,让父控件直接拦截所有事件。此时问题解决。
核心代码:

   @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                return true;
             
        }
        return super.onInterceptTouchEvent(ev);
    }

正真的原因是:

去除,内层textview android:longClickable="true" 属性在我这里可以正常工作(我把关键代码,放到我的demo中,用系统的长按点击事件。),在他那里不可以正常工作(后面会讲,他用的 不是系统原生的长按点击,他用的basequcikadapter的childLongClickListener)。

最终总结:
basequcikadapter的childLongClickListener是经过了开源的封装,他已经处理了上面的情况。导致各个child是分离的,点击A控件,只有“属于A非B”的空白区域会响应点击childLongClick。
至此悬案解决!!!!

上一篇 下一篇

猜你喜欢

热点阅读