Grid item not showing when clicked

My application has a grid view. By default, its element is not highlighted when I click on it (I donโ€™t know why?). I am trying to add it to the list as shown below, but it doesnโ€™t work either,

<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 is 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> 

+6
source share
3 answers

If the content of the list item (or views for each grid item) has a focus element, the selector is not 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 the ListView will not draw a selector for each ListView element, because the CheckBox is customizable by default.

You can either provide a background for each element that changes with the selection state, or turn off focus on all custom elements (which, in turn, requires you to write an enchanting adapter to check the boxes when the selection state changes.

eg:

 <CheckBox android:id="@+id/checkBox" android:focusable="false" ... /> 

will cause the included ListView to start drawing the selector again.

Status reconciliation example:

range hood / 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> 

Then you apply this against the background of each view returned by your adapter:

 <LinearLayout android:orientation="horizontal" android:background="@drawable/my_list_selector_bg" ... > 

Any approach will work.

The adpater class classes (GridView, ListView, and c) call hasFocusable () for each view returned by the adapter, and disable highlighting if hasFocusable () returns true. Fortunately, they also replicate the highlight / focus / press / active state to the currently focused or selected adapter element, so you can do it yourself if you want.

+9
source

For those of you who have the same problem as me, try

gridView.setDrawSelectorOnTop (true);

I have an ImageView and a TextView in each of my GridView elements. My selector worked, but he painted behind my image.

+7
source
  • Mesh view layout:

      <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:listSelector="@drawable/gridselector"/> 
  • Create your own gridselector.xml selector, for example:

     <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> 
-1
source

All Articles