ListView を持つアクティビティがあり、BaseAdapter に基づいてカスタム アダプターを作成しました。
カスタム アダプターの GetView メソッドは、カスタム レイアウトを使用します。
view = context.LayoutInflater.Inflate(Resource.Layout.BluetoothDeviceListItem, null);
レイアウトは次のようになります。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/BluetoothDeviceListSelector">
<TextView android:id="@+id/txtBluetoothDeviceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
style="@android:style/TextAppearance.Large"/>
<TextView android:id="@+id/txtBluetoothDeviceAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
style="@android:style/TextAppearance.Medium"/>
</LinearLayout>
基本的に、Bluetooth デバイスの名前とその下のアドレスを表示します。
しかし、ユーザーが ListView 内の項目を選択できるようにしたいと思います。その項目は (少なくとも) 強調表示されたままにするか、アイコンを追加したい場合があります。
私が理解しているように、カスタム レイアウトを使用しているため、デフォルトの選択インジケーターが失われ、独自のセレクターを使用する必要があります。オンラインチュートリアルで次のようなものを見つけました。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#1DB38B"
android:angle="270" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#2B37AE"
android:angle="270" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<gradient
android:startColor="#E77A26"
android:endColor="#EEFFEE"
android:angle="270" />
</shape>
</item>
</selector>
ただし、ユーザーが ListView 内の項目を押し続けている限り、色が変わるだけです。彼が手放すとすぐに、何も強調表示されなくなります。
今、私は本当に問題を引き起こしているのかを理解できません。
- 私のListViewは途中で選択サポートを失いましたか
- セレクターの XML は単純に間違っていますか?
- アダプターに選択サポートを追加する必要がありますか (ビューから独立している必要があるため、これは奇妙に思えます)。
免責事項: 関連する質問をいくつか見ましたが、あまり役に立ちません。
また、現在、万里の長城のため、ほとんどのオンライン ドキュメントにアクセスできません。