0

I am new in programming ad I'm on my first app, so excuse me for my ignorance... and also for my english.

I have a GridView with images, and i'm trying to implement a ViewHolder to improve performance.

Here's the code. xml for imageView dimensions:

 <ImageView
        android:layout_width="wrap_content"
        android:layout_height="@dimen/grid_view_item_height"
        android:id="@+id/immagineGriglia" />

and the java code of the imageAdapter:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ImageAdapterAdulti15 extends ArrayAdapter {
    private Context context;
    private int layoutResourceId;
    private ArrayList data = new ArrayList();

        // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.image1, R.drawabla.image2........
    };

    // Constructor
    public ImageAdapterAdulti15(Context context, int layoutResourceId, ArrayList data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {
        View cella = convertView;
        ViewHolder holder = null;

        if (cella == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            cella = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.immagineGriglia = (ImageView) cella.findViewById(R.id.immagineGriglia);
            cella.setTag(holder);
        } else {
            holder = (ViewHolder) cella.getTag();
        }

        ImageItem item = (ImageItem) data.get(position);
        holder.immagineGriglia.setImageBitmap(item.getImage());
        return cella;
    }

    static class ViewHolder {
        ImageView immagineGriglia;     
    }    
    }

But with this code the gridView is slower than before, without ViewHolder.

I think this code in incorrect or incomplete. Someone can help to fix it? Thanks...

Marcellino247
  • 57
  • 2
  • 6
  • Try to load bitamp in async manner check out : http://www.coderzheaven.com/2013/09/01/faster-loading-images-gridviews-listviews-android-menory-caching-complete-implemenation-sample-code/ or http://developer.android.com/training/displaying-bitmaps/process-bitmap.html – Haresh Chhelana May 28 '15 at 13:08

3 Answers3

1

Your drawable array

 Integer[] mThumbIds = {R.drawable.image_one, R.drawable.image_two, R.drawable.image_three};

Your getView of ArrayAdapter

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

 View cella = convertView;
 ViewHolder holder = null;

  if (cella == null) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    cella = inflater.inflate(layoutResourceId, parent, false);
    holder = new ViewHolder();
    holder.immagineGriglia = (ImageView) cella.findViewById(R.id.immagineGriglia);
    cella.setTag(holder);
  } else {
    holder = (ViewHolder) cella.getTag();
  }

 ImageItem item = (ImageItem) data.get(position);
 holder.immagineGriglia.setImageDrawable(context.getResources().getDrawable(mThumbIds[position]));
 return cella;
 }

Done

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • Thanks, work but not well (I'm still testing on emulators with genymotion). In a device with Android 4.2.2, API 17, 768x1280, it work but causes memory leak (after some scrolling and some click on the images the application crash). In devices with android 4.1.1-API 16-2560x1600, android 4.1.1-API 16-480x800 and 4.4.4-API 19-1080x1920 the application crash when i try to open the activity with the gridview. The error is OutOfMemoryError. – Marcellino247 May 29 '15 at 08:40
  • Can you please add android:largeHeap="true" in manifest file in tag ? – Hiren Patel May 31 '15 at 08:48
  • Whit this is better. I Still have problems in tablets with high resolution, but is better. – Marcellino247 Jun 01 '15 at 08:28
0

Scrollview become slower because you are using bitmap. You should use image path as a string and then you can load image using any imageloader library like Universal image loader or picasso.

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
0

Recyclerview accelarted rather than ListView.This will definitely helps you :)

public class HomeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {


private List<String> mItemList;

RecyclerView mRecyclerView;

Context context;
HomeItemViewHolder mHomeItemholder;

public HomeRecyclerAdapter(List<String> itemList) {
    mItemList = itemList;
}


public class HomeItemViewHolder extends RecyclerView.ViewHolder {

    ImageView mImage;


    public HomeItemViewHolder(View parent) {
        super(parent);

        mImage = (ImageView) parent.findViewById(R.id.Image); 

    }

}

public HomeRecyclerAdapter(List<String> itemList, Context context, RecyclerView mRecyclerView) {
    this.mItemList = itemList;
    this.context = context;
    this.mRecyclerView = mRecyclerView;


}
udayatom
  • 126
  • 1
  • 7