1

How can i improve my gridView performance? Scrolling is not smooth.

Here's my code:

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

        View MyView;

        // Inflate the layout
        LayoutInflater li = ((Activity) MyContext).getLayoutInflater();

        MyView = li.inflate(R.layout.gallery_adapter, null);
        ImageButton imageFolders = (ImageButton) MyView.findViewById(R.id.folder);


        try {

            imageFolders.setImageBitmap(bMap);
            imageFolders.setOnClickListener(this);
            imageFolders.setId(products.get(position).getId());
        } catch (Exception e) {

        }

        MyView.setLayoutParams(70, 70);
        return MyView;
    }

I have something like 200 images on this gridView, but the performance is very, very bad.

hdoria
  • 624
  • 2
  • 14
  • 29
  • 2
    What @K-ballo said. Plus you don't need a try/catch for setting values on your imageButton. And you should move getting your layout inflator to the constructor of your adapter so you only have to get it once instead of for every view. 1 time vs 200. – dymmeh May 14 '12 at 20:37
  • Thanks for the help. I have a problem when i try to recycle my views. Some images are duplicating. Any tips? – hdoria May 14 '12 at 21:00

6 Answers6

4

you can modify your code like this:

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

    View MyView;

    if(convertView==null) 
    {
        // Inflate the layout
        LayoutInflater li = ((Activity) MyContext).getLayoutInflater();

        MyView = li.inflate(R.layout.gallery_adapter, null);
        ImageButton imageFolders = (ImageButton) MyView.findViewById(R.id.folder);


        try {

            imageFolders.setImageBitmap(bMap);
            imageFolders.setOnClickListener(this);
            imageFolders.setId(products.get(position).getId());
        } catch (Exception e) {

        }

        MyView.setLayoutParams(70, 70);
    }
    else
        MyView = convertView;
    return MyView;
}

images already loaded will not be reloaded. have you tried this possibility ?

julien dumortier
  • 1,303
  • 1
  • 13
  • 28
  • Worked way better, but i got a problem with this code. The images are getting duplicated. Seems like the images are being replaced when the gridView scrolls. All images should be different. What's wrong? – hdoria May 15 '12 at 18:48
  • Have you implemented this feature: "public int getItemPosition(Object object)" – julien dumortier May 16 '12 at 06:38
  • 1
    you should read this: [viewpager-pageradapter](http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view) – julien dumortier May 16 '12 at 06:39
0
public View getView(final int position, View convertView, ViewGroup parent){

    View MyView;
    ImageButton imageFolders;

    if(convertView==null){
        LayoutInflater li = ((Activity) MyContext).getLayoutInflater();
        MyView = li.inflate(R.layout.gallery_adapter, null);
     }else{
        MyView = convertView;
     }

     imageFolders = (ImageButton) MyView.findViewById(R.id.folder);

     try {
        imageFolders.setImageBitmap(bMap);
        imageFolders.setOnClickListener(this);
        imageFolders.setId(products.get(position).getId());
     } catch (Exception e) {}

     MyView.setLayoutParams(70, 70);
     return MyView;
}

i didnt test it, but give it a try

Ahmed Adel Ismail
  • 2,168
  • 16
  • 23
  • im not sure about this part : MyView.setLayoutParams(70, 70); if it caused a problem, try putting it in the if/else block after the MyView = ... – Ahmed Adel Ismail Jun 22 '13 at 12:19
0

doing all the layout modifications like textView.setText("text"); inside the if(convertView == null) improved the performance extremely for me.

Also if you need to call super.getView(); call it right after the if(convertView == null) Block.

For the else part return convertView.

Roland
  • 49
  • 4
0
  1. Recycle your Views. Inflate the layout and set the layout params only once.
  2. Make sure you don't load the Bitmap from disk/network on the main thread.
  3. Simplify the view hierarchy in your grid item layout (R.layout.gallery_adapter).
  4. Make sure hardware acceleration is not disabled in your app's manifest.
  5. Make sure you don't have overdraw (drawing multiple backgrounds on top of each other).
BladeCoder
  • 12,779
  • 3
  • 59
  • 51
0

The best way is:

  1. Initialize the Context, LayoutInflayer and Bitmaps (or Drawables) in the constructor of your CustomAdapter

     private Context ctx;
     private int resource;
     private Bitmap[] bmps;
    
     private LayoutInflater inflater;
    
     MyCustomAdapter(Context ctx, Bitmap[] bmps) {
         this.ctx = ctx;
         this.bmps = bmps;
    
         this.resource = R.layout.gallery_adapter;
         this.inflater = LayoutInflater.from(ctx);
     }
    
  2. Recycle your convertView if is not null

     public View getView(final int position, View convertView, ViewGroup parent) {
         final ImageButton imgBtn;
    
         // Recycle convertView
         if (convertView == null) {
             imgBtn = (ImageButton) inflater.inflate(resource, null, false);
         } else {
             imgBtn = (ImageButton) convertView;
         }
    
         // Set Attributes
         imgBtn.setImageBitmap(bmps[position]);
         // imgBtn.setLayoutParams(70, 70); // Remove this and put it in your xml resource
    
         ...
    
         return imgBtn;
    

This code asume that your xml resource (R.layout.myImageButton) looks like:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/someId"
    android:layout_width="70dp"
    android:layout_height="70dp"
    ...
 />
-3

Get the images in lower detail, or get a faster Phone.
Either that, or program the gridview in such a way that the images are loaded only when you start to see them.

Wampie Driessen
  • 1,654
  • 1
  • 13
  • 19
  • 2
    A faster phone is not a solution to this problem. Fix the problem instead of working around it. Additionally, GridView only calls getView right before views come on screen. There's no reason to program for that condition. – dymmeh May 14 '12 at 20:39
  • faster phone? Then how you become a developer. – Ashish Rawat Aug 12 '15 at 12:16