在GridView中选择项目时的边框和图标

时间:2016-10-12 07:01:13

标签: android gridview

为了做到这一点,我一直在做一些研究。我想要的是我有一个GridView项目具有不同的背景颜色和圆角。当用户点击任何项目时,它将在网格项目的中心和白色边框显示一个刻度图标。用户也只能一次选择一个,这意味着如果他们按下另一个项目,则勾选标记和边框需要隐藏在当前项目上。

这是我到目前为止所尝试的:

MianActivity:

//somecode
    setContentView(R.layout.activity_main);
            final GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));

ImageAdapter.java

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        // Constructor
        public ImageAdapter(Context c) {
            mContext = c;
        }

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

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            final MarkableImageView imageView;

            if (convertView == null) {
                imageView = new MarkableImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
                imageView.setPadding(20, 20, 20, 20);
            }
            else
            {
                imageView = (MarkableImageView) convertView;
            }
//setting rounded corners on every item
            imageView.setBackgroundResource(R.drawable.bg);
            final GradientDrawable drawable = (GradientDrawable) imageView.getBackground();
//setting bacground colour every item based on array
            drawable.setColor(Color.parseColor(mThumbIds[position]));
//tick mark and border
            imageView.setChecked(true);
            return imageView;
        }

        // Keep all Background Colors in array
        public String[] mThumbIds = {"#9e9d25","#9c26b0","#c62828","#01bcd4","#4e342e","#ffeb3b","#9c26b0","#2f7d32","#ffeb3b","#ff5722","#673ab7"
        };

        public class MarkableImageView extends ImageView {
            private boolean checked = true;

            public MarkableImageView(Context context) {
                super(context);
            }

            public MarkableImageView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }

            public MarkableImageView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }

            public void setChecked(boolean checked) {
                this.checked = checked;
                invalidate();
            }

            public boolean isChecked() {
                return checked;
            }

            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                if(checked) {
                    Bitmap check = BitmapFactory.decodeResource(
                            getResources(), R.drawable.tick);
                    canvas.drawBitmap(check, 50, 50, new Paint());

                }
            }

问题是它会在每个Grid项目上显示一个刻度线,也没有边框。我做错了什么?最重要的是我如何让用户每次选择一个项目?如果他们按下其他项目当前滴答需要隐藏。任何帮助,将不胜感激!谢谢!

0 个答案:

没有答案