OnItemClickListener 不适用于复选框? [英] OnItemClickListener was not work with the checkbox?

查看:26
本文介绍了OnItemClickListener 不适用于复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an item layout like this, and set the background by using item selector

<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 :

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() 但它不起作用.我发现如果我删除项目中的复选框,一切都会好起来的.

I have a ListView which will content some items. Then I used setOnItemClickListener() but it doesn't work. I found that the if I remove the checkbox in the item, everything will be okey.

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

What was the problem between the checkbox and the listener here? Can you give me some solution?

更新:这是监听器的代码

Update : This is the code of listener

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());
                    }
});

ps:我想让收件箱像 Android 上的 gmail 一样.每行都有一个复选框,如果用户想查看消息,可以单击项目

ps: I want to make the inbox like gmail on android. Each row has a checkbox and user can click on item if they want to see the message

推荐答案

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

The onItemClickListener for the listView will not work if you have any clickables like buttons, ImageButton, Checkbox, etc in the listView. Add

mainListView.setItemsCanFocus(true);

参考 ListView OnItemClickListener 没有响应?

这篇关于OnItemClickListener 不适用于复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆