我使用CustomGridView
taht ImageViews
。如何在android GridView
中获取视图的唯一标识符。知道android会回收视图,所以如果我view.setTag();
我可能会在一个视图中设置很多标记。
我想我不得不使用view.setTag();
命令,但我应该把它放在哪里?
这是我的代码:
import java.io.File;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class CustomGridViewAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private final String TAG = "CustomGridViewAdapter";
public Context mContext;
public int layoutResourceId;
public Bitmap icon;
private ArrayList<String> itemList = new ArrayList<String>();
private ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
public ArrayList<CustomGridViewAdapter.GridHolder> mViews = new ArrayList<CustomGridViewAdapter.GridHolder>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public CustomGridViewAdapter(Context context) {
// TODO Auto-generated constructor stub
Log.i(TAG, "costruttore");
this.mContext = context;
cont = 0;
}
public void add(String path) {
Log.i(TAG, "adding path: " + path);
itemList.add(path);
}
public void addBitmap(Bitmap bm) {
Log.i(TAG, "adding bitmap");
bitmapList.add(bm);
}
@Override
public int getCount() {
return itemList.size();
//return bitmapList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
Log.w(TAG, "im in getItem");
return itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
Log.i(TAG, "getItemId");
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.i(TAG, "im in getView");
View view = convertView;
GridHolder holder = null;
if (view == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.grid_elements, null);
holder = new GridHolder();
holder.btnAddPhoto = (Button) view.findViewById(R.id.btnAddPhoto);
holder.ivAddedPhoto = (ImageView) view.findViewById(R.id.ivAddedPhoto);
holder.ivFeaturedImage = (ImageView) view.findViewById(R.id.ivFeaturedImage);
holder.ivDeleteImageButton = (ImageView) view.findViewById(R.id.ivDeleteImageButton);
holder.ivFirstPhoto = (ToggleButton) view.findViewById(R.id.ivFirstPhoto);
holder.ivFeaturedImage.setVisibility(View.GONE);
view.setTag(holder);
//i was trying to add a tag to each single view but it seems that when it recycles it associates new tags to the view.
//i would like to have 1 tag per view
//mViews.add(holder);
} else {
holder = (GridHolder) view.getTag();
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 95, 95);
holder.ivAddedPhoto.setImageBitmap(bm);
//mViews.add((GridHolder) view.getTag(position));
holder.ivDeleteImageButton.setVisibility(View.VISIBLE);
holder.ivDeleteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "PositionD: " + Integer.toString(position));
File file= new File(itemList.get(position));
if(file.exists()) {
file.delete();
}
itemList.remove(position);
notifyDataSetChanged();
}
});
holder.ivFirstPhoto.setTag(holder);
holder.ivFirstPhoto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, "Position First Photo: " + Integer.toString(position));
Log.i(TAG, "Tag is: " + buttonView.getTag().toString());
Log.i(TAG, "mViews size: " + Integer.toString(mViews.size()));
Log.i(TAG, "getCount size: " + Integer.toString(getCount()));
Log.i(TAG, "id's: " + Integer.toString(getCount()));
GridHolder holderView;
//set all the ivFeaturedImage to invisible
for (int i=0; i < mViews.size(); i++){
Log.i(TAG, "mViews elm: " + mViews.get(i).toString());
holderView = mViews.get(i);
holderView.ivFeaturedImage.setVisibility(View.GONE);
}
holderView = (GridHolder) buttonView.getTag();
if (isChecked) {
buttonView.setBackgroundResource(R.drawable.first_photo_on);
holderView.ivFeaturedImage.setVisibility(View.VISIBLE);
} else {
buttonView.setBackgroundResource(R.drawable.first_photo_off);
holderView.ivFeaturedImage.setVisibility(View.GONE);
}
//notifyDataSetChanged();
//view.setVisibility(View.VISIBLE);
}
});
return view;
}
static class GridHolder {
ImageView ivAddedPhoto;
ImageView ivFeaturedImage;
ImageView ivDeleteImageButton;
ToggleButton ivFirstPhoto;
Button btnAddPhoto;
int id;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
}
答案 0 :(得分:0)
这样做会在适配器中创建自定义持有者
private static class ViewHolder {
public long id;
public ImageView coverView;
}
然后创建一个新视图&amp;在适配器中充气
View view = mInflater.inflate(layout, null);
ViewHolder holder = new ViewHolder();
holder.coverView = your image resource ;
holder.id = your id ;
view.setTag(holder);
然后只需return view
希望这有助于让我知道如果你没有得到它我也可以发布正确的书面代码..