问题
I am trying to set a custom selector to a ListView. It's working fine on newer devices but not working on lower version of devices.I want the ListView selected item to be stay highlighted.
Please help.
Thanks in advance.
ListView.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"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewBell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_color" >
</ListView>
</LinearLayout>
list_selectror_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/list_selector" />
<stroke
android:dashWidth="2dp"
android:width="1dp"
android:color="@color/white" />
</shape>
I have also tried with selector but nothing happens
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_selector_color" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selector_color" android:state_focused="true"/>
<item android:drawable="@drawable/list_selector_color" android:state_selected="true"/>
<item android:drawable="@drawable/list_selector_normal"/>
</selector>
Here is my Custom adapter getView Method
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_bell_items, parent,
false);
ImageView imageview = (ImageView) convertView
.findViewById(R.id.list_bell_image);
imageview.setImageResource(mDataImage[position]);
TextView textview = (TextView) convertView
.findViewById(R.id.txt_bell_title);
textview.setText(mDataText[position]);
return convertView;
}
回答1:
I had similar problem. Sorry, cannot comment, so I post a possible answer.
Remove
android:listSelector="@drawable/list_selector_color"property from yourListViewdeclarationIn your
R.layout.listview_bell_itemsspecify your custom selector for a root layout. E.g., if root layout of your list item isRelativeLayout, try:<RelativeLayout ... android:background="@drawable/listitem_selector">...
The same goes for any other type of Layout.
If this still still does not give you the result you want, provide more details.
Update Ok, if nothing else helps, there's a temporary dirty workaround of your problem. I do not see why it would not work.
Introduce a selectedPos variable holding currently selected item.
private class MyAdapter extends .../*your base adapter*/ {
private static final int NOT_SELECTED = -1;
private int selectedPos = NOT_SELECTED;
// if called with the same position multiple lines it works as toggle
public void setSelection(int position) {
if (selectedPos == position) {
selectedPos = NOT_SELECTED;
} else {
selectedPos = position;
}
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == selectedPos) {
// your color for selected item
view.setBackgroundColor(Color.parseColor("#000000"));
} else {
// your color for non-selected item
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return view;
}
}
Now, add the following code after you create and set ListView's adapter:
final MyAdapter adapter = new MyAdapter(...);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelection(position);
}
});
回答2:
That is what I found out:
To get the list selector working the are two approaches:
1) You use the OnItemClickListener. Then the list selector drawable/color will work as expected when set it on the list view. Then you may use a TouchListener for getting ClickEvents of any child view instead of using a ClickListener.
2) You have set ClickListener on any child of the row view. In this case the list selector will not work when been set on the list view, so you have to set your list selector drawable/color as background of the row view.
来源:https://stackoverflow.com/questions/22422196/android-listview-listselector-not-working