Добавление 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>
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" />
Добавить
Андроид : 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>
Внести следующие изменения
<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. тогда вы начнете получать индивидуальный щелчок по внутренней строке списка. В вашем случае флажок становится фокусом, поэтому он не позволяет щелкнуть по строке вашего списка. Это должно сработать, если не стесняйтесь комментировать мой вопрос.
Похожие вопросы
Новые вопросы
android
Android — это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфонов, планшетов, автомобилей, телевизоров, одежды, очков, IoT). Для тем, связанных с Android, используйте теги, специфичные для Android, такие как android-intent, android-activity, android-adapter и т. д. Для вопросов, отличных от разработки или программирования, но связанных с Android framework, используйте эту ссылку: https://android .stackexchange.com.