У меня есть небольшая проблема с gridview, ямки показывают нормально, но когда я прокручиваю вниз, я прокручиваю изображение, меняя положение изображения или повторяю, я не знаю, почему я немного запутался, я использую более 50-100 изображений, но теперь только 19 изображений, которые я тестировал, как это будет, я получил эту проблему, и она не будет придерживаться ее позиции. Спасибо.Android GridView Repeating Images
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;
}
}
Поскольку похоже, что вы отправляете асинхронные запросы для захвата изображений, и из того, что я вижу в приведенном выше коде, вы не отменяете запросы при повторном просмотре, это может вызвать проблему, которую вы видите. Однако, так как некоторые из кода являются // seudo-кодом, трудно сказать наверняка. – Submersed