7

Repeat images Android GridView - codesd.com

 2 years ago
source link: https://www.codesd.com/item/repeat-images-android-gridview.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Repeat images Android GridView

advertisements

Im having a little problem with gridview the iamges shows normally but when i scroll down then i scroll up the images positions change or repeat i dont know why iam little bit confused, i use over 50 to 100 images but now only 19 images i was testing how it will be so i got that problem and it wont stick on its position . Thanks .

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;
}


In getView(), when re-use ImageView that already exists you still need to set the image of that view. What you do actually is to set the image when it is a new ImageView only.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK