2016-01-23 26 views
1

我在我的活動中有一個網格視圖。我已經使用GridView.setSelector()設置了網格視圖的項目高亮顏色。保持網格視圖選中的項目在焦點丟失後突出顯示

XML

<GridView 
     android:id="@+id/gridView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="10dp" 
     android:background="#DDDDDD" 
     android:choiceMode="singleChoice" 
     android:columnWidth="@dimen/category_edit_grid_item_size" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" /> 

代碼

gridView.setSelector(R.color.theme_primary); 

當我選擇一個項目,它得到R.color.theme_primary突出。但是,只要我移動到活動中的另一個輸入元素(比如說我觸摸了一個編輯文本),網格視圖高亮就會消失。

即使在網格視圖失去焦點後,如何保持突出顯示?

+0

http://stackoverflow.com/questions/15598301/set-background-in-gridview-by-item-position –

回答

1

一種解決方案是對要保持突出顯示的項目使用自定義狀態。您可以看到this answer瞭解如何添加狀態。在添加狀態之後,只需添加用於確定何時啓用/禁用該狀態的邏輯。

+0

這將工作。但我希望我能找到一個更簡單的方法 –

相關問題