Android how to highlight a selection in a list
What I want to do is very simple.
I have a ListFragment filled with items. If I touch one item I want the text or background of the item to change color. This means that this item is selected. Now when I touch another item I want the last item to change back to normal and select the new one.
How can I do this?
Than开发者_开发百科k you very much.
EDIT:
The solutions that remember the last views work but are dependant on accessing the last view to either set it's state or color. Now this works.
However I have new problem. I also have a function for moving an item up in the list.
Here it is:
public void MoveUp(){
if (lastselected > 0){
String item = adapter.getItem(lastselected);
adapter.remove(item);
adapter.insert(item, lastselected-1);
lastselected = lastselected -1;
System.err.println("Is View NULL " + (getListView().getChildAt(lastselected) == null));
}
}
The problem (And this is why I insisted on "what to do in Unmark" in the second solution proposed) is that for the last items in a 64 item lists the function getChildAt returns null. Since it turns null I can neither set it's selected state nor set it's color back.
This is my debug printout:
07-21 18:30:54.490: WARN/System.err(6733): Is View NULL true
07-21 18:30:54.670: WARN/System.err(6733): Is View NULL true
07-21 18:30:54.860: WARN/System.err(6733): Is View NULL true
07-21 18:30:55.020: WARN/System.err(6733): Is View NULL true
07-21 18:30:55.180: WARN/System.err(6733): Is View NULL false
07-21 18:30:55.720: WARN/System.err(6733): Is View NULL false
07-21 18:30:55.930: WARN/System.err(6733): Is View NULL false
07-21 18:30:56.110: WARN/System.err(6733): Is View NULL false
07-21 18:30:56.340: WARN/System.err(6733): Is View NULL false
And all I kept doing is pressing the up button. If I can find someway for this function to return a non null pointer it would be greatly appreciated. Thanks
Use a Selector (See StateList):
<?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/white" />
<item
android:state_selected="true"
android:drawable="@color/selected_color" />
<item
android:drawable="@color/default_color" />
</selector>
In the background for your list_item put this which is a reference to the above XML.
android:background="@drawable/list_item_bg_selector"
You could use the onListItemClick()
-method to do that. It is called when one Item is clicked.
You could then change Background/Text color and mark it as highlighted. If you want to safe this state, you could do it using a Field in your class, which holds the selected item's ID (or something).
Just a little illustration:
public class YourFragment extends ListFragment{
private long current_id;
private ListView current_lv;
public void onListItemClick (ListView l, View v, int position, long id){
// Check if the fields are initialized:
if (current_id != null && current_lv != null){
// Unmark the previously selected entry:
unmark();
}
// Mark the currently selected entry by using the
// given ListView "l" and long "id"
// Change the field-values:
current_id = id;
current_lv = l;
}
private void unmakr(){
// Take the fields to unmark the entry
}
}
I used the ListView
and the ID of the entry to determine which one was the previously selected one, you can however use the View
or something else if you like.
But you should note, which parameters the method gives you.
精彩评论