如何设置在Android上单击GridView的项目背景绘制? [英] How to set a background drawable on a clicked GridView item in Android?

查看:192
本文介绍了如何设置在Android上单击GridView的项目背景绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件与一堆图标,我需要选择一个。并选择我的意思是:

I have a GridView with a bunch of icons and I need to select one. And by select I mean:


  • 我需要绘制ID存储到数据库中(这样我就可以在以后访问它)

  • 我需要借助某种视觉提示对电网,让用户知道哪个图标当前选择

我发现这一点:结果
http://groups.google.com/group/android-developers/browse_thread/thread/f08a58167dbaa8c8

所以我猜 setSelection 是出来的图片,我不能用它来选择图标本身也不绘制视觉线索。我知道在 onItemClickListener 电网项目的位置,我可以将其保存为私有类变量。我最大的问题是绘图视觉线索。

So I guess setSelection is out of the picture and I can't use it to select the icon itself nor draw the visual cue. I know the grid item position in the onItemClickListener and I can save it as a private class variable. My biggest problem is drawing that visual cue.

我怎样才能做到这一点?我怎样才能在点击项目绘制一个视觉提示,如果用户点击不同的项目,以拉开的视觉提示,并在新项目重绘?

How can I achieve that? How can I draw a visual cue on the clicked item and if the user clicks different items, to "undraw" that visual cue and redraw it in the new item?

推荐答案

本解决了几个小时后,我想我终于找到了我一直在寻找解决方案。虽然答案是几乎没有关系,伊恩解决方案最初的编辑帮我找到这个解决方案:)

After tackling with this for a couple of hours I think I finally found the solution I was looking for. Although the answers are barely related, the initial edits on Ian solution helped me find this solution :)

我不打算解释一切,我做到了,我认为这是pretty言自明。短短几句话:

I'm not going to explain everything I did, I think it's pretty self explanatory. Just a few remarks:


  • 首先,我试过 view.Invalidate() view.postInvalidate()而不是 iconAdapter.notifyDataSetChanged(),但既不工作。文档指出,只有无效方法问道可能的情况下重绘视图。

  • First I tried view.Invalidate() and view.postInvalidate() instead of iconAdapter.notifyDataSetChanged() but neither worked. The documentation stated that the invalidate methods only "asked" to redraw the view "when possible".

我一直在寻找一个简单的解决方案,而不是合并两个可绘制。例如,画上了的ImageView 背景和视觉提示的图像的图标。对于一些奇怪的原因,视觉线索开始随机遍布其他图标显示 GridView控件已滚动时。我不明白为什么,但在背景图像的顶部视觉提示的想法使我感觉良好和的ImageView 允许,不需要额外的合并方法。但是,我不能让它没有它工作...

I was looking for a simpler solution than to merge two drawables. For instance, draw the icon on the ImageView background and the visual cue as the image. For some strange reason, the visual cue started to show randomly all over the other icons when the GridView was scrolled. I don't understand why, but the idea of a visual cue on top of a background image makes perfect sense to me and ImageView allows that, no need for that extra merge method. However, I couldn't make it work without it...

MyActivity.java

public class MyActivity extends Activity {
    private GridView mGridViewIcon;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGridViewIcon = (GridView)findViewById(R.id.gridview_icon);
        mGridViewIcon.setAdapter(new IconAdapter(this));

        mGridViewIcon.setOnItemClickListener(new GridView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IconAdapter iconAdapter = (IconAdapter)parent.getAdapter();

                iconAdapter.setSelectedItemPosition(position);
                iconAdapter.notifyDataSetChanged();
            }

        });
    }

}

IconAdapter.java

public class IconAdapter extends BaseAdapter {

    private int mSelectedPosition;
    private Integer[] mThumbIds;
    private int mIconSize;
    private Context mContext;

    public IconAdapter(Context context) {
        mThumbIds = AppHelper.ICON_SET.keySet().iterator().next();
        mIconSize = context.getResources().getDimensionPixelSize(R.dimen.default_icon_size);
        mContext = context;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mContext.getResources().getDrawable(mThumbIds[position]);
    }

    @Override
    public long getItemId(int position) {
        return mThumbIds[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        if(convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
        } else {
            imageView = (ImageView)convertView;
        }

        if(mSelectedPosition == position) {
            imageView.setImageDrawable(mergeDrawableLayers(mThumbIds[position],
                    R.drawable.ic_note_selected_mark));
        } else {
            imageView.setImageResource(mThumbIds[position]);
        }

        return imageView;
    }

    public void setSelectedItemPosition(int position) {
        mSelectedPosition = position;
    }

    private Drawable mergeDrawableLayers(int background, int overlay) {
        Drawable[] drawableLayers = new Drawable[2];

        drawableLayers[0] = mContext.getResources().getDrawable(background);
        drawableLayers[1] = mContext.getResources().getDrawable(overlay);

        return new LayerDrawable(drawableLayers);
    }

}

这篇关于如何设置在Android上单击GridView的项目背景绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆