OnItemClickListener无法使用复选框?

Tru*_*yen 12 checkbox android onitemclicklistener

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

<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" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我有一个ListView,它将包含一些项目.然后我使用了setOnItemClickListener()但它不起作用.我发现,如果我删除项目中的复选框,一切都将是okey.

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

更新:这是监听器的代码

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());
                    }
});
Run Code Online (Sandbox Code Playgroud)

ps:我想在android上制作像gmail一样的收件箱.每行都有一个复选框,用户可以点击项目,如果他们想看到该消息

sil*_*tos 40

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

  android:focusable="false"
  android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

我有同样的问题,并做到了这一点.


use*_*414 16

如果您在listView中有任何可点击的按钮,ImageButton,Checkbox等,则listView的onItemClickListener将不起作用.加

mainListView.setItemsCanFocus(true);
Run Code Online (Sandbox Code Playgroud)

请参阅ListView OnItemClickListener无响应?

  • 如果列表节点中存在复选框,则不起作用 (5认同)

bre*_*dan 9

只需添加

android:descendantFocusability="blocksDescendants"

到listitem的顶级LinearLayout.