首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StateListDrawable内部GridView

StateListDrawable内部GridView
EN

Stack Overflow用户
提问于 2012-08-08 21:41:46
回答 1查看 820关注 0票数 0

在更改代码(首次尝试)之后,我也遇到了类似的问题。我已经更新了我的getView()以执行正确的方式。

代码语言:javascript
复制
@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上使用:

代码语言:javascript
复制
videoGallery.setChoiceMode( GridView.CHOICE_MODE_MULTIPLE_MODAL );
videoGallery.setMultiChoiceModeListener( new MultiChoiceModeListener() { ... }

我遇到的问题是,当图像通过长时间点击在GridView上被选中时,它们不会改变。操作栏改变了,我的上下文菜单出现了,等等。我也尝试通过StateListDrawable创建具有相同结果的XML。有什么想法?

更新

改变

代码语言:javascript
复制
drawable.addState( new int[]{ android.R.attr.state_checked, android.R.attr.state_selected }, layerDrawable );

代码语言:javascript
复制
drawable.addState( StateSet.WILD_CARD, layerDrawable );

在我的getView()中,显示了我正在寻找的边框。所以也许StateListDrawable没有得到状态的改变?有人有想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-09 18:47:05

我想出来了。我的州有几个问题。按顺序排列第一:

代码语言:javascript
复制
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称该项目被“检查”,但实际状态仍然是“激活”的。所以我把这两行改为:

代码语言:javascript
复制
drawable.addState( new int[] { android.R.attr.state_activated }, layerDrawable );
drawable.addState( new int[] { -android.R.attr.state_activated }, new BitmapDrawable( res, bmp ) );

而且它运行得很完美!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11873615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档