我需要一个 GridView,但在每个网格中,都会有一个 ImageView 和 TextView 上面/里面。
它就像每个网格中的项目图像,以及图像上的项目名称。
我正在尝试:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
TextView nameView = new TextView(mContext);
nameView.setText("Name");
nameView.setTextSize(20);
parent.addView(nameView);
return imageView;
}
在我的网格视图适配器中。但我不能在这里添加它。我也尝试在 ImageView 中添加它,但由于它不是 ViewGroup,我无法实现。
更新:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewHolder holder;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
convertView = inflater.inflate(R.layout.gridviewitem, null, true);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.gridViewItemImage);
holder.name = (TextView) convertView
.findViewById(R.id.gridViewItemName);
holder.price = (TextView) convertView
.findViewById(R.id.gridViewItemPrice);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(mThumbIds[position]);
holder.name.setText("Item " + String.valueOf(position));
holder.price.setText("$15");
return convertView;
}
这解决了我的问题