4

我有一個擴展的BaseAdapter,它將LinearLayout子元素(每個中的ImageView和TextView)連接到自定義圖庫。Android:自定義圖庫setSelection()問題

當我第一次啓動我的活動時,我想調用setSelection(position)以使ImageView將其選擇器更改爲「選定」圖像。一旦我將圖庫扔到後續選定的孩子身上,這種方法就行得通,但不是第一次啓動該應用程序。

我的選擇:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true" 
    android:drawable="@drawable/home_image_select" /> 
<item android:state_selected="false" 
    android:drawable="@drawable/home_image" /> 
</selector> 

我的第一個猜測是調用適配器上notifyDataSetChanged()調用爲setSelection(),在這之後我試圖這樣做:

((CustomAdapter) gallery.getAdapter()).notifyDataSetChanged(); 

這沒」不要做任何事情。我也嘗試覆蓋Gallery類的setSelection()來做到這一點:

View v = this.getAdapter().getView(position, null, this);  
((ImageView) v.findViewById(R.id.gallery_image)).setSelected(true); 

這也行不通。任何我失蹤或可以嘗試?

回答

0

我找到了我自己的問題的解決方案,通過覆蓋畫廊的setSelection()(它終究工作)。

@Override 
public void setSelection(int position) { 
    super.setSelection(position); 

    View v = this.getAdapter().getView(position, null, this); 
    v.setFocusable(true); 
    v.requestFocus(); 
} 
0

我想你不應該叫notifyDataSetChanged(),當基礎數據集改變選擇狀態將被清除。

只需撥打setSelection(position),它就可以在我的應用程序中使用。