OnItemClickListener不能与复选框一起使用?

15

我有一个类似这样的项目布局,并使用项目选择器设置背景

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/itemselector"
android:orientation="horizontal" >
<CheckBox
    android:id="@+id/message_row_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/message_row_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:textColor="@color/black" />

itemselector.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
 android:state_pressed="true" 
 android:drawable="@color/yellow" />
<item 
 android:state_selected="true" 
 android:drawable="@color/green" />
<item 
 android:drawable="@color/white" />
</selector>

我有一个ListView,其中包含一些项目。然后我使用了setOnItemClickListener()方法,但它没有起作用。我发现如果我移除项目中的复选框,一切都会正常。

复选框和监听器之间的问题是什么?你能给我一些解决方案吗?

更新:这是监听器的代码

mainListView.setAdapter(messageAdapter);
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        Message p = (Message) arg0.getItemAtPosition(arg2);
                        Toast.makeText(TarsiusActivity.this, p.getTitle(), Toast.LENGTH_LONG);
                        Log.i("Item Clicked", p.getTitle());
                    }
});

提示:我想让收件箱在Android上像Gmail一样。每行都有一个复选框,用户可以单击项目以查看消息。


1
请展示您所使用的代码来设置 onClickListener() - PearsonArtPhoto
可能是重复的问题:ListView OnItemClickListener Not Responding? - Heath Borders
5个回答

45

最佳方法是为您的复选框设置以下属性:

  android:focusable="false"
  android:focusableInTouchMode="false"

我曾经遇到同样的问题,然后这样解决了。


1
如果禁用复选框,则起作用! - TacB0sS
也请为我工作。谢谢。 - Guillaume Ilitch Tchaïkovsky
不起作用 - linjiejun

18

如果在listView中有任何可点击的元素,例如按钮、图片按钮、复选框等,则listView的onItemClickListener将无法起作用。请添加

mainListView.setItemsCanFocus(true);

请参考ListView OnItemClickListener Not Responding?


不起作用。 - linjiejun

9

只需在列表项的顶级LinearLayout中添加

android:descendantFocusability ="blocksDescendants"

即可。


注:此属性可防止子视图获得焦点,以便父视图可以处理所有输入事件。

不适用于我。 - linjiejun

1

对于复选框,请使用setOnCheckedChangeListener而不是onItemClickListener

CheckBox check;
check=new CheckBox(this);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
    }
});

还有其他解决方案吗?我想让它看起来像 Android 上的 Gmail。仍然需要复选框,如果用户想要阅读消息,则单击该项目。 - TrungNguyen

1
您可以在您的OnItemClickListener方法中添加以下代码:
public void onItemClick(AdapterView parent, View view, int position, long id){
   CheckBox box = (CheckBox)view.findViewById(R.id.course_search_checkbox);
   box.setChecked(true);
}

谢谢你的解决方案,但是你在安卓上使用GMAIL了吗? :) - TrungNguyen
1
是的,在收件箱中,如果您单击复选框,将会有一些选项供您选择。但是,如果您单击其他地方,它将打开消息。这正是我想要的 ;) - TrungNguyen
首先,您必须在与父视图底部对齐的同一XML中创建选项视图。在活动的onCreate中,将整个选项视图的可见性设置为gone。接下来,当您单击复选框时,在onItemClick中将选项视图的可见性设置为可见。 - Pradeep Sodhi
这不是正确的解决方案,因为在检查列表项后它无法删除复选框的值。 - Arpit Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接