2

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 は単純に間違っていますか?
  • アダプターに選択サポートを追加する必要がありますか (ビューから独立している必要があるため、これは奇妙に思えます)。

免責事項: 関連する質問をいくつか見ましたが、あまり役に立ちません。
また、現在、万里の長城のため、ほとんどのオンライン ドキュメントにアクセスできません。

4

2 に答える 2

3

最後に、@CommonsWare のコメントに基づいて、単にアクティブ化された状態を使用しました。

<?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_activated="true">
    <shape>
      <gradient
       android:startColor="#E77A26"
         android:endColor="#EEFFEE"
         android:angle="270" />
    </shape>
  </item>
</selector>

そうは言っても、選択したアイテムを表示することから離れて、Android の設計ガイドラインに準拠するために、アイテムをクリックすると直接新しいアクティビティに移動するだけです。

于 2013-02-27T04:31:49.500 に答える
1

選択した位置をメモリに保持できます。アダプターに、選択したアイテムを保管するための変数を追加します。次に、getView()メソッド内で、現在の位置が選択したアイテムと等しい場合は、別のことを行います。アイテムを強調表示する場合は、setBackgroundColor()

これが例です

 @Override public View getView(int position, View convertView,
    ViewGroup parent) {

        View row = convertView;

        if (row == null) {      
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
             row = inflater.inflate(R.layout.my_xml_file, parent,false);    }
        if(position==selectedPosition){            
             row.setBackgroundColor(Color.parseColor("#800ff000"));     }   
        else{       
             row.setBackgroundColor(Color.TRANSPARENT);     }

       return row; }
于 2013-02-25T15:12:20.003 に答える