我在使用 gridview 时遇到一点问题,iamges 正常显示,但是当我向下滚动然后向上滚动时,图像位置发生变化或重复 我不知道为什么我有点困惑,我使用了 50 到 100 多张图像,但现在只有 19 张图像我正在测试它会如何,所以我遇到了那个问题并且它不会坚持它的位置。 谢谢。
public class ImageAdapter extends BaseAdapter {
private Context context;
public Integer[] images = {
R.drawable.f_alicate,R.drawable.f_arcoajustable,
R.drawable.f_bateria,R.drawable.f_bisagra,
R.drawable.f_bisagra2,R.drawable.f_cadena_galvanizada,
R.drawable.f_canilla_bronze ,R.drawable.f_canilla_con_palanca,
R.drawable.f_casco_rojo,R.drawable.f_casco_rojo2,
R.drawable.f_cerraduracajon,R.drawable.f_cerradura_para_cajon2,
R.drawable.f_conexion_cromado,R.drawable.f_cotra_candena,
R.drawable.f_cuchara_albanil,R.drawable.f_cutter,
R.drawable.f_cutter2,R.drawable.f_disco,
R.drawable.f_corta_hiero};
// Constructor
public ImageAdapter(Context c){
context = c;
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return images[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null ;
if (convertView == null) {
imageView = new ImageView(context);
new BitmapWorkerTask(imageView).execute(images[position]);
//create new ImageView if it is not present and populate it with some image
} else {
imageView = (ImageView) convertView;
//re-use ImageView that already exists in memory
}
return imageView;
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(ImageAdapter.this.context.getResources(), data, 450, 450);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(450, 450));
}
}
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static 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) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
最佳答案
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.gridview_item_layout, parent, false);
} else {
v = (View) convertView;
}
TextView text = (TextView)v.findViewById(R.id.grid_item_text);
text.setText(mTextIds[position]);
ImageView image = (ImageView)v.findViewById(R.id.grid_item_image);
image.setImageDrawable(mThumbIds[position]);
return v;
}
关于Android GridView 重复图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28221912/