有没有一种方法可以将ID分配给gridview上的每个项目?

时间:2018-10-04 16:41:24

标签: android

有没有一种方法可以将id分配给gridview上的每个项目?我正在使用gridview,并按以下方式填充它:

 public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator=LayoutInflater.from(this.mContext);
        View layout=inflator.inflate(R.layout.activity_main, parent, false);
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(170, 170));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(4, 4, 4, 4);
        } else {
            imageView = (ImageView) convertView;
        }


        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {

            R.drawable.container_circle_blue,
            R.drawable.container_circle_dblue,
            R.drawable.container_circle_green,
            R.drawable.container_circle_yellow,
            R.drawable.container_circle_red,

            R.drawable.container_square_blue,
            R.drawable.container_square_dblue,
            R.drawable.container_square_green,
            R.drawable.container_square_yellow,
            R.drawable.container_square_red,



            R.drawable.container_hexagone_blue,
            R.drawable.container_hexagone_dblue,
            R.drawable.container_hexagone_green,
            R.drawable.container_hexagone_yellow,
            R.drawable.container_hexagone_red,

            R.drawable.container_star_blue,
            R.drawable.container_star_dblue,
            R.drawable.container_star_green,
            R.drawable.container_star_yellow,
            R.drawable.container_star_red,

            R.drawable.container_triangle_blue,
            R.drawable.container_triangle_dblue,
            R.drawable.container_triangle_green,
            R.drawable.container_triangle_yellow,
            R.drawable.container_triangle_red,
    };

我需要为每个项目添加一个ID,以便以后可以处理碰撞并与其中的每个图像视图和其他图像进行比较

2 个答案:

答案 0 :(得分:1)

您应该设置Tag而不是为动态创建的视图分配ID。

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflator=LayoutInflater.from(this.mContext);
    View layout=inflator.inflate(R.layout.activity_main, parent, false);
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(170, 170));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(4, 4, 4, 4);
    } else {
        imageView = (ImageView) convertView;
    }
    // set tag
    imageView.setTag(position);

    return imageView;
}

稍后在onClickonItemClicked中查看时,您可以执行以下操作-

public void onClick(View view) {
    Object tag = view.getTag();
    if (tag instanceOf Integer) {
        int pos = (Integer)tag;
        // use position to identify that item.
    }
}

由于您是新手,如果使用RecyclerView,我建议使用Listview。如果仍然需要使用普通适配器,请使用ViewHolder模式。

答案 1 :(得分:0)

该职位有效地充当了ID。还是我错过了重点?