ListFragment: highlight selected row

心已入冬 提交于 2019-12-12 02:22:24

问题


I am using ListFrament and I want to highlight selected rows.

My selector:

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

The code in my ListFragment: in onCreateActivity:

this.getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

on click:

@Override
public void onListItemClick(ListView list, View v, int position, long id) {
    super.onListItemClick(list, v, position, id);
    String tag = this.getTag();
    Log.d(TAG, "id, position " + id + " " + position);
    list.setSelection(position);
}

unfortunately this does not work - it only blinks "gold", when clicking the row... I want that the selected row stays highlighted...

Thanks in advance for your answers,


回答1:


Try this

set a Global Variable of your row type. I am taking View as it is generic

View previous;

initialize view in onCreateView

previous=new View(getContext);

And when onListItemClick do like this

public void onListItemClick(ListView list, View v, int position, long id) {
    super.onListItemClick(list, v, position, id);
    String tag = this.getTag();
    Log.d(TAG, "id, position " + id + " " + position);
   previous.setSelected(false);
   v.setSeleted(true);
   previous=v;
}

Try this and Get back to I have used this in my old apps... I will work for sure.. I hope this will help you.



来源:https://stackoverflow.com/questions/15115559/listfragment-highlight-selected-row

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