ListView on Fragment
How can I by pressing a line of ListView (onListItemClick) mark that line selected (maintain the pressed color, which is by default 开发者_StackOverflow中文版yellow)?
I'm not on HoneyComb, i'm using android-support-v4.jar an Android compatibility jar.
I want to give the aspect like Gmail on HoneyComb ( http://www.youtube.com/watch?v=5swwUeZ0-ww ) that you know what item is selected as it maintains the blue color.
I think what i want to do is a new state called activated (on HoneyComb), but.. i want to do it with the compatibility jar on other versions.
Btw, i'm using a SimpleCursorAdapter to populate the ListFragment
Thanks.
I implemented this in the list's adapter class. I had 2 types of rows in the list: 1 for normal, unselected rows, another for selected rows. When the user selects a list item, you notify the adapter to set that item 'selected', and call .notifyDatasetChanged(). In the .getView() of the adapter, you check whether the current item is selected or not, and inflate the corresponding layout for that row.
It might seem more complicated, but it certainly works. This way you can always ask the adapter for the selected element(s) also, without messing around with instance variables in your activity to store this info. I think this's a bit cleaner.
use following code
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
view.setBackgroundColor(Color.WHITE);
}
});
精彩评论