GridView太慢了

时间:2015-10-11 11:44:02

标签: android gridview baseadapter

我正在使用BaseAdapter填充GridView,但滚动时速度很慢。可能是什么问题?它填充了两个imageViews +一个textView

提前谢谢你。

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

    RowHolder rowHolder = null;
    View itemView = convertView;
    final ThemeModel curTheme = getItem(position);

    if (itemView == null) {

        rowHolder = new RowHolder();

        itemView = mInflater.inflate(R.layout.view_themes_grid_row, parent, false);
        rowHolder.imageView = (ImageView) itemView.findViewById(R.id.imageView);
        rowHolder.imageView2 = (ImageView) itemView.findViewById(R.id.imageView2);
        rowHolder.textView = (TextView) itemView.findViewById(R.id.textView);

        itemView.setTag(rowHolder);

    }  else {
        rowHolder = (RowHolder) itemView.getTag();
    }

    rowHolder.textView.setText(curTheme.getThemeName());

    //Log("curTheme name = " + curTheme.getThemeName() + " / mainBg = " + curTheme.mainBg);



    if(curTheme.hasBgImage()) {

     // grid view item background image
      rowHolder.imageView.setBackgroundResource(curTheme.getMainBgImageId(Themes.this)); 

        //Picasso.with(Themes.this).load(drawable).into(rowHolder.imageView);
    } else {

    // grid view item background color
                  rowHolder.imageView.setBackgroundColor(Color.parseColor(curTheme.mainBg));
        //Picasso.with(Themes.this).load(Color.parseColor(curTheme.mainBg)).into(rowHolder.imageView);
    }

    if(curTheme.isUsing()) { //tick icon
        rowHolder.imageView2.setVisibility(View.VISIBLE);
    }

    else {
        rowHolder.imageView2.setVisibility(View.INVISIBLE);
    }

    rowHolder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(!curTheme.isUsing()) //can cause error..
                chooseTheme(curTheme);
        }
    });

    return itemView;
}

3 个答案:

答案 0 :(得分:2)

我们没有足够的关于您为imageView设置的图片的信息,但问题可能在于它们的大小。您可以在申请查看之前缩放它们。

Drawable bgDrawable = null;

if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    bgDrawable = getResources().getDrawable(curTheme.getMainBgImageId(Themes.this), null);
} else {
    bgDrawable = getResources().getDrawable(curTheme.getMainBgImageId(Themes.this));
}

//Scale the images
Bitmap bitmap = ((BitmapDrawable) bgDrawable).getBitmap();
Drawable drawable = new BitmapDrawable(
               getResources(), Bitmap.createScaledBitmap(bitmap,
               bitmap.getWidth() / 4,
               bitmap.getHeight() / 4,
               true)
          );

rowHolder.imageView.setBackground(drawable);
//You can use picasso

答案 1 :(得分:1)

您可以将RecyclerViewGridLayoutManager一起使用,它具有平滑滚动和比GridView更好的响应。

答案 2 :(得分:0)

可能会有不同的事情......

  1. 图片视图的大小是否在布局中固定?父级的大小取决于在imageview中加载的图像的大小吗?
  2. 如果是,这将减慢您的视图速度,因为每次加载新图像时都必须调整整个布局。

    1. 如果图像大于图像视图,则应在后台线程中对图片进行采样。