问题
I have a listview in ListFragment which gets populated from an adapter, I want to highlight the clicked (or selected) item in the list as well as do some action based on the selection. I am able to handle the event but How do I set the color of selected item from the list.
回答1:
If you're using a ListFragment then you might not be specifying a ListView in XML which means you'll need to set the listSelector in code. Be careful not to set it too early in the activity life-cycle though as you'll end up with an IllegalStateException.
level_list_selector.xml
<?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="@drawable/level_gradient_bg" />
<item android:state_pressed="true" android:drawable="@drawable/level_gradient_bg_hover" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/level_gradient_bg_hover" />
</selector>
Fragment:
public class LevelFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Create view
...
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setSelector(R.drawable.level_list_selector);
}
}
Note: if you're using a custom layout for the cell rows, you'll need to set the list selector on that too.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/level_list_selector"
android:padding="5dip" >
...
</RelativeLayout>
回答2:
Simply create a selector file with your needed states. That will provide solution for you. For example -
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/bg_list_item_highlighted" /> <!-- @drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/bg_list_item_pressed" /> <!-- @drawable/tab_press -->
</selector>
In your ListView set this selector as listselector. Ex,
<ListView
android:id="@+id/listView1"
android:width="wrap_content"
android:height="wrap_content"
android:listSelector="@drawable/selector" />
Have a look at these examples -
ListSelector applies to the entire list
ListView with selector
Listview_and_list_selector
回答3:
for this you have to make a drawable as the background of your list items. and after "selected" any item from the lisView, set the drawable to be transparent or #000.so when item will select selector you maid will show.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://meta.android.com/apk/res/android">
<item android:drawable="@color/transparent" />
<item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
</selector>
回答4:
If you want standard ListView behaviour in your ListFragment, you can set your list item row layout to use ?android:attr/activatedBackgroundIndicator as background. Like so:
ListItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator" <!--This is the important bit-->
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight" />
<TextView
android:text=""
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
ListFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:tools="http://schemas.android.com/tools"
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@drawable/divider"
android:dividerHeight="0.5dp" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No data" />
</LinearLayout>
In your ListFragment fragment code, use the custom layout (this is mono code, but do not despair):
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
base.OnCreateView(inflater, container, savedInstanceState);
View view = inflater.Inflate(Resource.Layout.ListFragment, container, false);
//... do stuff
}
Some place in the code where you want to create adapter, use the ListItem layout. In this case a SimpleAdapter mapping data to a Icon and Text view. the backgound in the ListItem.xml (?android:attr/activatedBackgroundIndicator) makes the item behave as you see in a normal list:
var list = new List<IDictionary<string, object>>(listOfStuff.Count);
foreach (AMap map in listOfStuff) {
var dictionary = new JavaDictionary<string, object> {
{"text", map.Text}, {"icon", Resource.Drawable.SomeIcon}
};
list.Add(dictionary);
}
SimpleAdapter _adapter = new SimpleAdapter(Activity, list,
Resource.Layout.ListItem,
new string[] { "text", "icon" },
new int[] { Resource.Id.text,
Resource.Id.icon });
回答5:
in adapter class in get view method() u can do that as ..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setTag(position);
if(ur condition){
view.setBackgroundColor(Color.TRANSPARENT);
}else {
view.setBackgroundColor(Color.GREEN);
}
return view;
}
来源:https://stackoverflow.com/questions/12048921/how-to-set-color-of-selected-item-in-listfragment-in-android