The following worked for me for:
res/color/menu_highlight.xml
<?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/red" />
<item
android:state_selected="true"
android:drawable="@color/red" />
<item
android:drawable="@color/white" />
</selector>
res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="red">#FF0000</color>
</resources>
res/layout/menuitem.xml:: (XML for every item in the list)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/textmenu"
android:layout_height="wrap_content"
android:text="text"
android:textColor="#FFFFFF"
android:background="@color/menu_highlight"
android:visibility="visible"
android:layout_width="fill_parent" />
</LinearLayout>
Finally, in the ListFragment class, add View previous and add the following code to the onlistitemclick function.. (mentioned in ListFragment: highlight selected row)
public class MenuListFragment extends ListFragment{
View previous;
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Logic to highlight selected item
previous.setSelected(false);
v.setSelected(true);
previous=v;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…