Question

I have a ListView:

<ListView
        android:id="@+id/sensorList"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:layout_below="@+id/chooseHint"
        android:choiceMode="multipleChoice" >
</ListView>

Which has CheckedTextViews in it:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_margin="5dp"
    style="@android:style/TextAppearance.Medium"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" />

I have added an OnItemClickListener to do something when a list item is clicked. But also when an item is clicked, the checkbox is toggled. How can i achieve that the listener is catching the click event, but the checkbox is not?

Was it helpful?

Solution

To prevent the CheckedTextView from toggling, do the following:
1. Subclass CheckedTextView.
2. Override setChecked(boolean) method.
3. Replace your CheckedTextView with the overridden one.

Code for steps 1 and 2:

package com.example.multichoicelist;
public class UnresponsiveCheckedTextView extends CheckedTextView {

    public UnresponsiveCheckedTextView(Context context) {
            this(context, null);
    }

    public UnresponsiveCheckedTextView(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
    }

    public UnresponsiveCheckedTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setChecked(boolean checked) {
              //Do nothing.
    }

}  

It is the setChecked method that refreshes the checked state of the CheckedTextView which brings about the toggle effect. Overriding setChecked will prevent the checkbox of the CheckedTextView from getting toggled when the user clicks a list item.

XML for step 3:

<com.example.multichoicelist.UnresponsiveCheckedTextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    style="@android:style/TextAppearance.Medium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
 />  

Below is the activity containing a multiple choice ListView with an OnItemClickListener that shows the list item that has been clicked. Notice that during such a click event the checkbox is not toggled (thanks to the overridden CheckedTextView implementation):

package com.example.multichoicelist;
public class MainActivity extends ListActivity implements OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView mListView = getListView();
        mListView.setOnItemClickListener(this);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mListView.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.phonetic_alphabet, R.layout.list_item));
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View childView,
            int adapterPosition, long arg3) {
        Toast.makeText(this,
                ((CheckedTextView) childView).getText() + " selected",
                Toast.LENGTH_SHORT).show();
    }

}  

The image below shows the behavior of the multiple choice ListView when the list item named 'Charlie' is clicked:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top