0

在更改我的代码(第一次尝试)后,我遇到了类似的问题。我已经更新了我的 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 没有得到状态变化?有人有什么想法吗?

4

1 回答 1

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

它运行良好!

于 2012-08-09T18:47:05.980 回答