Android StateListDrawable内部GridView

Android StateListDrawable内部GridView,android,android-layout,android-widget,Android,Android Layout,Android Widget,在()前后更改代码之后,我遇到了类似的问题。我已经更新了getView()以正确的方式执行 @Override public View getView( int position, View convertView, ViewGroup parent ) { Resources res = activity.getResources(); if( convertView == null ) { LayoutInflater vi = (LayoutInflate

在()前后更改代码之后,我遇到了类似的问题。我已经更新了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的系统上使用:

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


在my getView()中,显示我要查找的边界。那么可能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 );
在第一个
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 ) );
而且它工作得很好

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