0

I have a fragment that contains a GridView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridView
    android:padding="20dp"
    android:id="@+id/sub_category_gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:horizontalSpacing="20dp"
    android:numColumns="4"
    android:verticalSpacing="10dp"
    android:scrollingCache="true"
    android:smoothScrollbar="true"
    android:fastScrollEnabled="true"
    tools:listitem="@layout/sub_category_view" />
</LinearLayout>

Every child of this GridView contains one TextView and one ImageView

<?xml version="1.0" encoding="utf-8"?>
<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: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>

When I fill GridView with Texts and Images, it's become very Laggy and slow when I scrolling. This is My adapter Class:

    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);
            //subCatImg.setImageResource(imgIDs.getResourceId(position, -1));
            convertView.setTag(viewHolder);

        }
        else{
            //imgIDs.recycle();
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.subCatTitle.setText(subCatTitle[position]);
        return convertView;
    }
}
public class ViewHolder{
    public TextView subCatTitle;
    public ImageView subCatImg;
    public LinearLayout subCatView;
}

How can i optimize it to scroll more smoothly?

PS. My images are in drawable folder and their format is PNG.

Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
  • Create a holder class in your `adapterClass` and use it in your `getView`. Just use google baseadapter with holder class. Must give you some good results – Mike Dec 08 '14 at 13:54
  • @Mike Thanks for commenting... I found something about `ASyncTask` but most of codes that I found, was about loading images from SDcard or internet... Is it possible to use `AsyncTask` for images that stored in drawable folder? – Saman Salehi Dec 08 '14 at 14:04
  • Depends on how long the `doInBackground` of your `AsyncTask` is running. Generally asynctask are just for short term processes in the background! – Mike Dec 08 '14 at 14:09
  • Have a look at this http://stackoverflow.com/questions/12797550/android-asynctask-for-long-running-operations .... to get the know-how you want ;)... – Mike Dec 08 '14 at 14:14
  • @Mike I used ViewHolder and it get smoother but not smooth enough... And I found this http://developer.android.com/training/improving-layouts/smooth-scrolling.html ... But I don't know which part of my code, i should add `AsyncTask`! :( – Saman Salehi Dec 08 '14 at 14:41
  • If you could add `AsyncTask` to my adapter code, plz send it as answer, so I can accept it :) – Saman Salehi Dec 08 '14 at 14:44
  • I don't think they mean to use the asynctask in the getview... I think they mean you should provide your data from a seperate thread. – Mike Dec 08 '14 at 14:48
  • Don't you have an asynctask right now? Which provides data for your adapter? – Mike Dec 08 '14 at 14:49
  • @Mike So in my case that data stored in drawable folder,it doesn't matter if i use `AsyncTask` or not... Is that lags because of size of my images that i used in items? – Saman Salehi Dec 08 '14 at 14:52
  • I don't think so! Your drawable are coming with the app. you are not downloading it or getting it from the storage of the device. The Problem must be somewhere else but i can't say anything without your code. even that you Adapter you have posted wouldn't inflate anything.. – Mike Dec 08 '14 at 14:55
  • K2evil this is not good what you are doing right now... you wanted to make your Adapter scroll smoother. I provided you the solution to use a Holder-Class. Your getView method isn't performance-killing. That means your Problem must be somewhere else. Provide a new specific question and post your whole code to get the bestFeedback to your issue.. – Mike Dec 08 '14 at 14:59

1 Answers1

0

I had a ImageView in my Activity's Header that I set a really big image to it. It was killing performance. There is no problem in GridView.

Saman Salehi
  • 1,995
  • 1
  • 22
  • 36