2012-12-02 40 views
6

我的應用程序有一個網格視圖,默認情況下,它的項目不突出顯示,當我點擊它(我不知道爲什麼?)。我嘗試如下它添加的列表選擇,但它不工作過,網格視圖項目不高時按

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

這裏我選擇:網格視圖

<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

高光顏色是黑色,背景顏色是什麼? – Sam

+0

背景顏色是透明的,父視圖背景是白色的... –

+0

您使用的是自定義適配器嗎?如果是這樣,請在自定義充氣的視圖組上使用您的選擇器。 – mango

回答

9

如果在列表項內容(或每個網格項目的視圖)一個聚焦元素,選擇不繪製

例如,如果爲您的列表項的模板:

<LinearLayout android:orientation="horizontal" ... > 
    <CheckBox android:id="@+id/checkBox" ... /> 
    <TextView android:id+"@+id/textView" ... /> 
</LinearLayout> 

然後ListView中不會繪製每個ListView項選擇,因爲該複選框,默認情況下,可獲得焦點。

您可以提供每個與選擇狀態,或者在所有可聚焦元素禁用焦點更改項目背景(這又要求你寫一個童話幻想適配器檢查複選框,當選擇狀態的變化。

例如:

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

將導致一個封閉的ListView再次開始繪製選擇器有狀態可繪製的

例:

繪製/ 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> 

你然後應用到你的適配器返回的每個視圖的背景:

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

無論哪種方法會奏效。

列表adpater類(GridView,ListView,& c)在適配器返回的每個視圖上調用hasFocusable(),如果hasFocusable()返回true,則禁用選擇繪圖。幸運的是,它們還將選擇/焦點/按下/激活狀態複製到當前聚焦或選定的適配器項目,因此如果需要,您可以自己繪製它。

+0

我明白了一點,你能給我代碼的例子嗎? –

+0

一個常見的陷阱:把一個複選框放在列表項tempate。 –

+0

(編輯原始帖子提供的例子) –

-1
  1. 佈局:

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:listSelector="@drawable/gridselector"/> 
    
  2. 創建自定義選擇器網格selector.xml喜歡:

    <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> 
    
+0

不工作,男人:( –

7

對於那些你們誰是有同樣的問題我是,嘗試

gridView.setDrawSelectorOnTop(真);

我在每個GridView項目中都有一個ImageView和一個TextView。我的選擇器正在工作,但它正在我的形象背後畫。

相關問題