GridView更改更多项

时间:2019-02-25 22:42:21

标签: android gridview

我的GridView有问题。我使用它简化了代码,并且代码与教程中的代码相同

public class CustomGrid extends ArrayAdapter<MuserObject> implements View.OnClickListener {

private ArrayList<MuserObject> musers;
Context context;

private static class ViewHolder {
    GridViewItem image, image2;
}

public CustomGrid(ArrayList<MuserObject> musers, Context context) {
    super(context, R.layout.grid_single, musers);
    this.musers = musers;
    this.context = context;

}

@Override
public void onClick(View v) {
}

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

    final MuserObject muser = getItem(position);
    final CustomGrid.ViewHolder viewHolder;

    if (convertView == null) {

         viewHolder = new CustomGrid.ViewHolder();
        final LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.grid_single, parent, false);
        viewHolder.image = convertView.findViewById(R.id.image);
        viewHolder.image2 =  convertView.findViewById(R.id.image2);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (CustomGrid.ViewHolder) convertView.getTag();
    }

    Picasso.get().load(muser.getImage()).into(viewHolder.image);

    // Change to green background
    if (muser.isDone()) {
        viewHolder.image2.setBackgroundColor(Color.parseColor("#ff669900"));
    }

    return convertView;
}

}

在代码底部,您可以看到我检查当前对象是否为isDone(),如果是,我想更改正方形的颜色,但是当我(仅针对一个对象)执行更改时,它将在更多地方更改gridview

这是一些照片

Original gridview

This happened when I set isDone(true) only for first item

请问我该怎么做才能仅在第一张图像上更改绿色?如果您需要更多代码或信息,我会给您。非常感谢您的建议。

0 个答案:

没有答案