Добавление CheckBox в listView-row деактивирует onClickListener самой строки! Я хочу иметь строки, в которых вы можете щелкнуть саму строку и также установить флажок. Как это сделать?

// Слушатель строк в основном действии

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

// Флажок-слушатель в адаптере

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

// Расположение строк

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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>
-1
XxGoliathusxX 8 Сен 2016 в 01:16

3 ответа

Лучший ответ

Добавьте эти два свойства в флажок в xml:

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

Пример :

<CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="CheckBox" />
1
SANAT 12 Сен 2016 в 05:53

Добавить

Андроид : descendantFocusability = " blocksDescendants "

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:descendantFocusability="blocksDescendants"
        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>
1
Chandan kushwaha 12 Сен 2016 в 05:49

Внести следующие изменения

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

Вам нужно установить для флажка фокусируемое значение false. тогда вы начнете получать индивидуальный щелчок по внутренней строке списка. В вашем случае флажок становится фокусом, поэтому он не позволяет щелкнуть по строке вашего списка. Это должно сработать, если не стесняйтесь комментировать мой вопрос.

0
Anant Shah 12 Сен 2016 в 05:43