2012-08-08 47 views
0

改變我的代碼後(first attempt)我有一個類似的問題。我更新了我的getView()以執行正確的方式。StateListDrawable GridView內

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Resources res = activity.getResources(); 

    if(convertView == null) { 
     LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = vi.inflate(R.layout.video_gallery_item, parent, false); 
    } 

    Bitmap bmp = getBitmap(videoIds[ position ]); 

    /* This layer drawable will create a border around the image. This works */ 
    Drawable[] layers = new Drawable[ 2 ]; 
    layers[ 0 ] = new BitmapDrawable(res, bmp); 
    layers[ 1 ] = res.getDrawable(R.drawable.border_selected); 
    LayerDrawable layerDrawable = new LayerDrawable(layers); 

    /* Create the StateListDrawable */ 
    StateListDrawable drawable = new StateListDrawable(); 
    drawable.addState(StateSet.WILD_CARD, new BitmapDrawable(res, bmp)); 
    drawable.addState(new int[]{ android.R.attr.state_checked, android.R.attr.state_selected }, layerDrawable); 
    ImageView v = (ImageView)convertView.findViewById(R.id.image); 
    v.setImageDrawable(drawable); 
    v.setAdjustViewBounds(true); 
    thumbnails[ position ] = bmp; 
    return convertView; 
} 

此適配器是一個GridView稱爲videoGallery使用:

videoGallery.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); 
videoGallery.setMultiChoiceModeListener(new MultiChoiceModeListener() { ... } 

我遇到的問題是,當他們在GridView上通過長按選擇的圖像不會改變。操作欄更改,我的上下文菜單出現等。我也嘗試通過XML創建StateListDrawable,結果相同。思考?

UPDATE

在我getView更改

drawable.addState(new int[]{ android.R.attr.state_checked, android.R.attr.state_selected }, layerDrawable); 

drawable.addState(StateSet.WILD_CARD, layerDrawable); 

()顯示我正在尋找的邊界。所以也許StateListDrawable沒有得到狀態改變?任何人有任何想法?

回答

0

我想通了。我的州有幾個問題。首先在順序:

drawable.addState(StateSet.WILD_CARD, new BitmapDrawable(res, bmp)); 
drawable.addState(new int[]{ android.R.attr.state_checked, android.R.attr.state_selected }, layerDrawable); 

在第一個addState我使用通配符。因爲這個android會在接下來的那個之前匹配這個狀態。另外,儘管android說這個項目被「檢查」,但實際狀態是「激活」的。所以我把兩條線改爲:

drawable.addState(new int[] { android.R.attr.state_activated }, layerDrawable); 
drawable.addState(new int[] { -android.R.attr.state_activated }, new BitmapDrawable(res, bmp)); 

而且它工作完美!