Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

android - OnItemClickListener was not work with the checkbox?

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 :

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Best way to do this is set these following properties for your checkbox:

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

I had a same issue and did this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...