How to highlight selected list item in android?

两盒软妹~` 提交于 2020-01-15 11:38:07

问题


In my android application i have list view and detail view for each list item. For tablets i have shown list view of items and selected item's detail view as follows.

So my problem is how can i highlight the selected item after user clicks on list item.

I am using a BaseAdapter to load list view.How can i do this any idea??

EDIT:

Yes as chintan khetiya mentioned i have used following xml file as the background of list item but it will not delighted the selected item. What have i missed?

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@color/abs__background_holo_light" />
<item android:state_pressed="true" 
    android:drawable="@color/action_bar_blue" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@color/action_bar_blue" />
</selector>

回答1:


Your Query :

my problem is how can i highlight the selected item after user clicks on list item.

I think you are asking about selector. Mean if the list row in focus state then it should be look different form all other row. Same thing when you press or Touch the Row.

For that you have to make Selector.xml File in Drawable folder and just put that selector file in your list row

That file should have different tag like Focus-Click-Press and change the Drawable as per state.

Update :

Just Replace Your icon and save in Drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Pressed -->
    <item android:drawable="@drawable/p_paly_press" android:state_pressed="true"/>

    <!-- selected -->
    <item android:drawable="@drawable/p_play" android:state_selected="true"/>

    <!-- focused -->
    <item android:drawable="@drawable/p_paly_press" android:state_focused="true"/>

    <!-- default -->
    <item android:drawable="@drawable/p_play"/>

</selector>



回答2:


The code similar to the following code below can be used to highlight the selected item for sure if other ways do not do that you need:

class Adapter extends ArrayAdapter<String> {

    private int selectedPos = -1;
    Drawable selectedBackground;

    public MainSelectAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        selectedBackground =
              context.getResources().getDrawable(R.color.selecteditembackground);
    }
    public void setSelectedPosition(int pos){
        selectedPos = pos;
        notifyDataSetChanged();
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (selectedPos == position) {
            v.setBackgroundDrawable(selectedBackground);
        } else {
            v.setBackgroundDrawable(null);
        }
        return v;
    }
}

And the in the main activity

  Adapter adapter = new Adapter(this, android.R.layout.simple_list_item_1,
    myItemsToShow);

  list = (ListView) findViewById(R.id.flows);
  list.setItemsCanFocus(true);
  list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> adapter, View view,
                int pos, long id) {
                adapter.setSelectedPosition(pos);
    }
  });

With this approach, you take own control on the selected item highlighting, you have a listener to capture selection events and you set yourself the required Drawable.




回答3:


You can define in your styles.xml

    <style name="Theme.Base" parent="...">
        <item name="activatableItemBackground">@drawable/activatable_item_background</item>
    </style>

   <style name="ListItemContainerBase">
        <item name="android:background">?activatableItemBackground</item>
    </style>

In res/drawable define activatable_item_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/item_focused" android:state_focused="true" />
    <item android:drawable="@drawable/item_focused" android:state_selected="true" />
    <item android:drawable="@drawable/item_activated" android:state_activated="true" />
    <item android:drawable="@drawable/item_checked" android:state_checked="true" />
    <item android:drawable="@android:color/transparent" />
</selector>

item_pressed, item_focused..... are images in res/drawable-xxx

Define for each items in your view a layout like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ListItemContainerBase">


来源:https://stackoverflow.com/questions/14645275/how-to-highlight-selected-list-item-in-android

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