ListFragment
複数の選択肢が含まれていますListView
。CursorAdapter
my toでレイアウトを設定するandroid.R.layout.simple_list_item_multiple_choice
と、チェックした項目が my に表示されますListView
。しかし、カスタム レイアウトを使用して、項目がチェックされたときに背景色を変更したいと考えています。それはかなり単純に思えましたが、私の問題に対する答えを見つけることができませんでした。アダプターの bindView で色を設定することを考えましたが、アダプターがチェックされている項目を認識していないため、それがどこに属しているかわかりません。そこで、セレクターを使用して、次のようにテキストビューの背景に適用しようとしました:
行.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="6dp"
android:background="@drawable/list_selector"/>
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item android:state_pressed="true"
android:drawable="@drawable/list_pressed" />
<!-- Selected -->
<item android:state_selected="true"
android:drawable="@drawable/list_active" />
<!-- Checked -->
<item android:state_checked="true"
android:drawable="@drawable/list_active" />
<!-- Normal -->
<item android:drawable="@drawable/list_normal" />
</selector>
押された状態は機能していますが、チェックされた状態ではありません。私の TextView がまったくチェックされているのだろうか。しかし、そうでない場合、チェックされたアイテムをどのListView
ように追跡しますか?