GridView中的StateListDrawable

时间:2012-08-08 21:41:46

标签: android android-layout android-widget

更改我的代码(first attempt)后,我遇到了类似的问题。我已经更新了我的getView()以执行正确的方法。

@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;
}

此适配器正在名为videoGallery的GridView上使用:

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

我遇到的问题是,通过长按在GridView上选择图像时,图像不会改变。操作栏更改,我的上下文菜单出现等。我还尝试通过XML创建StateListDrawable,结果相同。想法?

更新

更改

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

drawable.addState( StateSet.WILD_CARD, layerDrawable );
我的getView()中的

显示了我正在寻找的边框。那么StateListDrawable可能没有得到状态改变?有人有什么想法吗?

1 个答案:

答案 0 :(得分: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会在之后的那个状态之前匹配这个状态。此外,尽管安卓知道项目被“检查”,实际状态是“激活”。所以我将两行更改为:

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

它完美无缺!