问题
My app has a grid view, By default, Its item does not highlight when I click on it (I don't know why? ). I try to add it a list selector as below, But it does not work too,
<GridView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/load_more_process_id"
android:layout_alignParentTop="true"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/grid_selector"
>
</GridView>
Here my selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@android:color/black"></item>
<item android:state_pressed="false" android:drawable="@android:color/white"></item>
回答1:
If you have a focusable element in the list item contents (or the views for each grid item), the selector isn't drawn
For example, if the template for your list items is:
<LinearLayout android:orientation="horizontal" ... >
<CheckBox android:id="@+id/checkBox" ... />
<TextView android:id+"@+id/textView" ... />
</LinearLayout>
then ListView will not draw a selector for each ListView item, because the CheckBox is, by default, focusable.
You can either provide a background on each item that changes with selection state, or disable focus on all focusable elements (which in turn requires you to write a fairy fancy adapter to check the checkboxes when selection state changes.
eg:
<CheckBox android:id="@+id/checkBox" android:focusable="false" ... />
will cause an enclosing ListView to start drawing the selector again.
Example of a stateful drawable:
drawable/my_list_selector_bg.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_background_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/list_background_focused"
android:state_focused="true" />
<item android:drawable="@android:color/transparent" />
</selector>
You then apply that to the background of each view returned by your adapter:
<LinearLayout android:orientation="horizontal"
android:background="@drawable/my_list_selector_bg"
... >
Either approach will work.
The list adpater classes (GridView, ListView, &c) call hasFocusable() on each view returned by the adapter, and disable selection drawing if hasFocusable() returns true. Fortunately, they also replicate the selection/focus/pressed/active state to the currently focused or selected adapter item, so you can draw it yourself if you want.
回答2:
For those of you who are having the same issue I was, try
gridView.setDrawSelectorOnTop(true);
I have an ImageView and a TextView in each of my GridView items. My selector was working, but it was drawing behind my image.
回答3:
Layout of grid view:
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:listSelector="@drawable/gridselector"/>
Create custom selector gridselector.xml like:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selector_item"> <item android:state_pressed="true" android:drawable="@drawable/focus_offfocus"> </item> <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/focus_onfocus"> </item> <item android:state_enabled="true" android:drawable="@drawable/focus_offfocus"> </item> </selector>
来源:https://stackoverflow.com/questions/13666012/grid-view-item-not-hightlight-when-press