0

我正在使用带有自定义适配器的gridview。Grid 中的每个项目都包含一个图像视图和复选框,但我希望最后一个项目是一个按钮。

这是我的代码:

@Override
    public int getCount() {
        return thumbUrls.size()+1;
    }

我在 getcount 中返回 +1 以使 getview 被称为额外时间。

在getview中: -

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;
        if(row==null)
        {
            row = layoutInflater.inflate(R.layout.griditem, parent, false);
            holder = new ViewHolder();
            holder.mainImage = (ImageView)row.findViewById(R.id.itemgrid);
            holder.checkBox = (CheckBox)row.findViewById(R.id.itemselected);
            row.setTag(holder);
        }
        else {
            holder = (ViewHolder)row.getTag();
        }
        if (position==(thumbUrls.size()+1)) { //this is just to show what i am trying to do.
        Button button = new Button(context);
        return button;
        }
        else
        {
            setImageSize(holder.mainImage);

            holder.mainImage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    intent.putExtra("position", position);
                    intent.putStringArrayListExtra("MainUrls", mainUrls);
                    intent.putStringArrayListExtra("Thumbnails", thumbUrls);
                    context.startActivity(intent);
                }
            });

            holder.checkBox.setOnCheckedChangeListener(null);
            holder.checkBox.setTag(position);

            holder.checkBox.setChecked(positions.contains(thumbUrls.get(position)));
            holder.checkBox.setOnCheckedChangeListener(this);

            UrlImageViewHelper.setUrlDrawable(holder.mainImage, thumbUrls.get(position), R.drawable.extras_load);
        }
        return row;

    }
4

1 回答 1

2

代替

    if (position==(thumbUrls.size()+1)) { //this is just to show what i am trying to do.
    Button button = new Button(context);
    return button;
    }

采用

    if (position == thumbUrls.size()) { 
    Button button = new Button(context);
    return button;
    }

中的第一个参数positiongetView0 开始。您还应该LayoutParameterButton.

于 2012-10-12T12:13:17.770 回答