mercredi 7 septembre 2016

Android CheckBox and Row-OnClickListener

Adding a CheckBox to a listView-row deactivates the onClickListener of the row itself! I want to have rows where you can click on the row itself and select the checkbox too. How to do that?

//Row-Listener in the main-activity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           //Does not get called
     }
});

//Checkbox-listener in the adapter

  final CheckBox checkBox = (CheckBox) customView.findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checked.set(position, checkBox.isChecked());
            }
        });

//Row-Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_marginLeft="@dimen/custom_margin_left_and_right"
    android:layout_marginStart="@dimen/custom_margin_left_and_right"
    android:layout_marginRight="@dimen/custom_margin_left_and_right"
    android:layout_marginEnd="@dimen/custom_margin_left_and_right">

    <TextView
        android:id="@+id/nameTv"
        android:layout_weight="0.9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/unbenannt"
        android:textColor="@color/grey"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_weight="0.1"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"/>

</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire