Android - 优化GridView滚动

时间:2014-12-08 13:38:58

标签: android gridview android-asynctask

我有一个包含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>

此GridView的每个子节点都包含一个TextView和一个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>

当我使用文本和图像填充GridView时,滚动时它变得非常滞后和缓慢。这是我的适配器类:

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

如何优化它以更顺畅地滚动?

PS。我的图片在drawable文件夹中,格式为PNG。

1 个答案:

答案 0 :(得分:0)

我的ImageView标题中有一个Activity,我为它设置了一个非常大的图像。这是在扼杀表现。 GridView没有问题。