如何将图像添加到带有图像本身和文本的网格视图中。我尝试制作自定义图像适配器,但由于某些原因导致 imgView.setLayoutParams(new GridView.LayoutParams(85, 85)) 崩溃;
新问题如何从图像适配器中添加和删除...我需要的行为就像一张带有 a 的图片,我添加了带有文本 ab 的相同图片,所以应该有 2 张图片,然后我可以删除带有 a 的一张。 .
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AlbumAdapter extends BaseAdapter {
private Context mContext;
private ArrayList albNames;
public AlbumAdapter(Context c, ArrayList albNames) {
mContext = c;
this.albNames = albNames;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
/* 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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 3);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
*/
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.album_image, null);
}
else {
view = convertView;
}
ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
imgView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setPadding(10, 10, 10, 3);
TextView albNameView = (TextView) view.findViewById(R.id.icon_text);
if (albNames.size() > 0) {
for (int i = 0; i < albNames.size(); i++) {
imgView.setBackgroundResource(R.drawable.folder);
albNameView.setText((String)albNames.get(i));
}
}
return view;
}
// references to our images
public Integer[] mThumbIds = { R.drawable.folder};
}