我正在使用带有自定义适配器的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;
}