How to use a Selector with a ListView and custom Adapter to indicate the selected item

孤街浪徒 提交于 2020-01-04 13:07:06

问题


I've got an activity which has a ListView and I've created a custom Adapter based on BaseAdapter.

The GetView method of the custom adapter uses a custom layout:

view = context.LayoutInflater.Inflate(Resource.Layout.BluetoothDeviceListItem, null);

The layout looks like this:

<?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>

Basically I display the name of the Bluetooth device and it's address underneath.
But I'd like the user to be able to select an item in the ListView and that item should remain highlighted (at the very least) or I might want to add an icon to it or whatever.

As I understand it, since I'm using a custom layout, I lose the default selection indicator and have to use my own selector. I found something like this in an online tutorial:

<?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>

But the only thing that does is change the color as long as the user presses and holds an item in the ListView. As soon as he lets go, nothing is highlighted anymore.

Now I can't figure out what's really causing the problem.

  • Did my ListView lose selection support somewhere along the way
  • Is the XML for the Selector simply wrong
  • Should I add selection support to the Adapter (which seems strange as it should be independent of the view)

Disclaimer: I've seen a few related questions, but they do not really help me.
also, I'm currently unable to get to most of the online documentation due to the great wall


回答1:


In the end, based on @CommonsWare comments I simply used the activated state:

<?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>

That said, I have now moved away from displaying the selected item and I simply navigate to a new Activity directly upon clicking an item in order to comply with Android design guidelines.




回答2:


You could keep the selected position in memory. In your adapter, add a variable to store the selected item. Then inside your getView() method, if the current position equals your selected item, do something different. If you want to highlight your item, setBackgroundColor().

Here is an example

 @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; }


来源:https://stackoverflow.com/questions/15069845/how-to-use-a-selector-with-a-listview-and-custom-adapter-to-indicate-the-selecte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!