Search code examples
androidgridviewuniversal-image-loader

Android - Universal Image Loader is laggy when scroll on GridView


I use Universal Image Loader for show images in my GridView. I put my images in drawable folder. but it's very laggy when I scroll it! I try PauseOnScrollListener but it's still slow on scrolling...

This is my GridView fragment and adapter:

    public class SubCategoryFragment extends Fragment {

    TypedArray imgIDs;
    int[] imgResIds;
    String subCatTitle[];
    DisplayImageOptions options;
    ImageLoaderConfiguration config;
    ImageLoader imageLoader;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        options = new DisplayImageOptions.Builder()
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .imageScaleType(ImageScaleType.EXACTLY)
                    .build();
        config = new ImageLoaderConfiguration.Builder(getActivity())
                    .memoryCacheSize(41943040)
                    .discCacheSize(104857600)
                    .threadPoolSize(5)
                    .build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sub_category_fragment, container, false);
        ImageLoader.getInstance().init(config);
        //if(getArguments().getString("cat") == "choose"){
            subCatTitle = getResources().getStringArray(R.array.choose);
            imgIDs = getResources().obtainTypedArray(R.array.chooseImgIDs);
        //}
            imgResIds = new int[imgIDs.length()];
            for(int i = 0; i < imgIDs.length(); i++)
                imgResIds[i] = imgIDs.getResourceId(i, -1);
            imgIDs.recycle();
        ((GridView) view.findViewById(R.id.sub_category_gridview)).setOnScrollListener(new PauseOnScrollListener(imageLoader, false, true));
        ((GridView) view.findViewById(R.id.sub_category_gridview)).setAdapter(new CustomGrid(getActivity()));
        return view;
    }

    public class CustomGrid extends BaseAdapter{
        private Context mContext;

          public CustomGrid(Context c) {
              mContext = c;
          }
        @Override
        public int getCount() {
          // TODO Auto-generated method stub
          return subCatTitle.length;
        }
        @Override
        public Object getItem(int position) {
          // TODO Auto-generated method stub
          return null;
        }
        @Override
        public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.sub_category_view, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.subCatView = (LinearLayout) convertView.findViewById(R.id.sub_category_view);
                viewHolder.subCatImg = (ImageView) convertView.findViewById(R.id.sub_category_img);
                viewHolder.subCatTitle = (TextView) convertView.findViewById(R.id.sub_category_name);
                convertView.setTag(viewHolder);

            }
            else{
                //imgIDs.recycle();
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.subCatTitle.setText(subCatTitle[position]);
            imageLoader.displayImage("drawable://"+imgResIds[position], viewHolder.subCatImg, options);
            return convertView;
        }
    }
    public class ViewHolder{
        public TextView subCatTitle;
        public ImageView subCatImg;
        public LinearLayout subCatView;
    }

}

And this is My GridView item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center"
    android:id="@+id/sub_category_view">
    <ir.motorel.carbuyinstruduction.SquareLinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/button">
        <ImageView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxWidth="250dp"
            android:maxHeight="250dp"
            android:src="@drawable/picture1"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_margin="10dp"
            android:id="@+id/sub_category_img"/>
    </ir.motorel.carbuyinstruduction.SquareLinearLayout>
    <TextView
        android:layout_marginTop="5dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:gravity="center"
        android:textColor="#C36518"
        android:id="@+id/sub_category_name"/>
</LinearLayout>

What is the problem?


Solution

  • Probably this is not the reason but you should load your drawables with the native way look at this note (on the library git page): NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables - ImageView.setImageResource(...) instead of using of ImageLoader.

    Secondly i would try to remove the wrapping layout(SquareLinearLayout) and see how it goes. Its also can be caused by your images size, be sure you are using thumbnail size images.