如何在ListView子条目中获取部分组件点击事件?

1、我希望在ListView的子条目中获部分组件的点击事件。
这是一个仿微信的项目,我希望当且仅当点击左边按钮、头像、文本框的情况下触发onitemclick方法。错误效果 如下。
clipboard.png

2、ListView的XML代码

<ListView android:id="@+id/lv_id" android:layout_width="match_parent"
        android:layout_height="match_parent" android:layout_below="@id/et_search_id"
        android:descendantFocusability="blocksDescendants"/>

子条目的XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView android:id="@+id/first_name_id" android:layout_width="wrap_content"
        android:layout_margin="6dp"    android:clickable="false"
        android:layout_height="wrap_content" android:textSize="@dimen/text_size_xs"/>
    <TextView android:id="@+id/name_id" android:layout_width="match_parent"
        android:layout_height="60dp" android:gravity="center_vertical"
        android:paddingStart="100dp" android:layout_below="@id/first_name_id"
        android:paddingEnd="0dp" android:background="@drawable/d_bg_item"/>
    <com.wuxianedu.wechat.widget.RoundImageView android:id="@+id/head_round_id" 
        android:layout_width="50dp" android:layout_alignTop="@id/name_id"
        android:layout_height="50dp" android:layout_alignBottom="@id/name_id"
        android:contentDescription="@string/load_fail"
        android:src="@drawable/head_nor" android:layout_marginTop="5dp"
        android:layout_marginStart="40dp" android:layout_marginBottom="5dp"/>
    <CheckBox android:id="@+id/cb_id" android:layout_width="30dp" 
        android:layout_height="30dp" android:layout_margin="6dp"
        android:layout_alignTop="@id/name_id" android:layout_alignBottom="@id/name_id"
        android:clickable="false" android:focusable="false"
        android:focusableInTouchMode="false" android:button="@drawable/d_bg_but_jy"/>
</RelativeLayout>
阅读 5.6k
3 个回答

checkbox.setOnClickListener(new MyListener());
imageview.setOnClickListener(new MyListener());
textview.setOnClickListener(new MyListener());

自己写一个 MyListener 继承ClickListener... 做统一事件处理。完事。

不推荐这么做,推荐这三个要接收点击事件的控件外包裹一层View,用View来接收点击事件。这样点击操作失误率会小一些

以上手打伪代码

那你可以不直接设置list的item事件,直接设置item里面子View的事件不就行了吗

不设置onItemClickListener 然后设置你想监听的那几个控件即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题