2013-10-30 14 views
0

使用GridView並嘗試讓選定的項目突出顯示爲灰色。這工作正常,直到我在Android 2.3上測試它。在Android 2.3,這是當你選擇一個項目時會發生什麼:GridView listSelector on Gingerbread突出顯示整個網格

GridView with selection

在這個畫面我曾在餐館項目挖掘。本質上,整個GridView以灰色突出顯示,而不是選定的項目。但是,在Android 4.0及更高版本中,GridView按預期工作,並且點擊項目時正確突出顯示它們(而不是GridView!)呈灰色。

這裏是我的layout.xml的relavent部分:

<FrameLayout 
    android:id="@+id/grid_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <GridView 
     android:id="@+id/categories_grid_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:horizontalSpacing="10dp" 
     android:numColumns="4" 
     android:listSelector="@drawable/list_view_selector" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="10dp" /> 
</FrameLayout> 

這裏是res/drawable/list_view_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_pressed="true" 
     android:state_enabled="true" 
     android:drawable="@color/light_grey" /> 
    <item 
     android:state_focused="true" 
     android:state_enabled="true" 
     android:drawable="@color/light_grey" /> 
    <item 
     android:drawable="@color/transparent" /> 
</selector> 

我試過this的解決方法,但它只是導致沒有得到強調了2.3和4.0+設備上的所有(甚至不包括所選項目)。

任何幫助表示讚賞。

回答

4

將選擇器應用於項目佈局的background,並設置android:listSelector="@null"

網:

<FrameLayout 
    android:id="@+id/grid_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <GridView 
     android:id="@+id/categories_grid_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:horizontalSpacing="10dp" 
     android:numColumns="4" 
     android:listSelector="@null" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="10dp" /> 
</FrameLayout> 

項目:

<FrameLayout 
    android:id="@+id/grid_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/list_view_selector"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</FrameLayout> 
+0

謝謝。這解決了4.0+和2.3設備上的問題。 – smoak

+0

在我的情況下,它顯示的是gridview周圍的空間而沒有選擇它。 android:listSelector =「@ null」清除了問題,但我不明白爲什麼? – Diffy

0
<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/calendergrid" 
     android:layout_width="42dp" 
     android:layout_height="45dp" 
     android:gravity="center" 
     android:textColor="#ffffff" 
     android:textStyle="bold" 
     android:background="@drawable/listselectorforgridview" 
     > 
</TextView> 

只需粘貼,並使用像...

ArrayAdapter<String> dateadapter = new ArrayAdapter<String>(context, 
       R.layout.textviewforcalendergrid, calenderdays);