在GridView中选择图像的背景颜色

时间:2014-07-23 12:27:50

标签: android gridview

我创建了一个gridView of images。我正在尝试在下面的代码中更改选择图像的背景颜色。但是选择图像时没有变化。

gridView = (GridView)rootView.findViewById(R.id.gridView);
        customGridAdapter = new GridViewAdapter(getActivity(), R.layout.row_grid, getData());
        gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                //Toast.makeText(TeamFragment.this, position + "#Selected",
                //      Toast.LENGTH_SHORT).show();
                count=position;
                gridView.setBackgroundColor(Color.argb(125,75,236,90));
                /*int currentPic = position;
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);*/

            }

        });

GridViewAdapter

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.image = (ImageView) row.findViewById(R.id.image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        row.setBackgroundColor(Color.RED);
        ImageItem item = data.get(position);
        holder.imageTitle.setText(item.getTitle());
        holder.image.setImageBitmap(item.getImage());
        return row;
    }

2 个答案:

答案 0 :(得分:2)

在GridViewAdapter中的getView(...)方法中,您可以按如下方式为ImageView设置OnClickListener,

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView== null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView= inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) convertView.findViewById(R.id.text);
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        convertView.setBackgroundColor(Color.RED);
        ImageItem item = data.get(position);
        holder.imageTitle.setText(item.getTitle());
        holder.image.setImageBitmap(item.getImage());

        final View row = convertView;

        holder.image.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        row.setBackgroundColor(Color.yourColor);
                    }
                });

        return row;
    }

不要忘记将变量行声明为final,以便可以在匿名内部类View.OnclickListener中访问它。

答案 1 :(得分:0)

看起来您在项目选择中为整个gridView设置背景颜色。你应该使用Adpater调用getView()方法并设置颜色。