问题
I know that android doesn't highlight anything in TouchMode. But I am doing something similar to the gmail application in which you select stuff from the left side and show details on the right side of the activity(wonder how Google did that).
So the story is I have to highlight what's been selected on the left side ListView. I've found some similar questions and the solutions are basically:
1.override the adapter's getView method and setBackground for selected position
2.setBackground of the view onItemClick and clear it for anther selection
But none of them worked for me due to a weird behaviour: As I click on one item and highlight it, the fifth item after it is highlighted as well, and so on so forth as I scroll down the list.
Any suggestions? THX!
回答1:
Use listView.setChoiceMode(int choiceMode);
Parameters
choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView
http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)
You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE
(android.widget.AbsListView.MultiChoiceModeListener)
Refer to the sample below
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List16.html
回答2:
this is for custom listactivity or ListFragment
highlight selected item in ListView
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(Color.GREEN);
}
回答3:
////@drawable/list_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal"
android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_touch"
android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_touch"
android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="@drawable/list_item_bg_pressed"
android:state_activated="true"/>
</selector>
////////////////////and on ListView
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"
android:divider="#060606"/>
///////////////////////listview Item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector">
<ImageView
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/ic_home"
android:layout_centerVertical="true" />
</RelativeLayout>
回答4:
The reason you are highlighting multiple items is probably because you are either: reusing the whole view, or setting the background of both of these views to the same Drawable instance. If you have the same drawable on the screen twice, all events that happen to the first will happen to all the others, because that logic is implemented in the instance of the Drawable itself.
To solve this, either: do not re-use Views for multiple rows, or do not re-use Drawables for multiple rows (create a new one each time)
I know this sounds resource intensive and it is, but unless you have a better solution figured out this is the easiest fix.
回答5:
Because the items in the listviews mimics the ones at the top, they also mimics the background of them. You have to set every one of the background of your items in getView() function. In every item in getView() you have to set the background for both selected ones and the unselected ones.
回答6:
use this as list selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_bg" />
</selector>
and this list_bg
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/list_bg_color" />
</shape>
choose the preferred highlight color
来源:https://stackoverflow.com/questions/8565999/how-to-highlight-selected-item-in-listview