我的应用程序中有一个GridView,它在几乎所有设备上都非常棒,但是...我给它测试了一些朋友,他们说我的应用程序在GridView上崩溃..应用程序崩溃或者他们必须滚动在它真的很仔细。有些朋友说GridView没问题(也是我的 - HTC Wildfire,三星Galaxy Mini),但我的三星Galaxy S3,S4的朋友说它崩溃了。我的GridView:
public static List<Item> items = new ArrayList<Item>();
...
GridView gridView = (GridView) findViewById(R.id.gridview);
items.clear();
//add all items to list
gridView.setAdapter(new ImageAdapter(this, items));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawable;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = (Item) items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
return v;
}
}
我的SquareImageView类,它将我的GridView元素的大小调整为2列,并在多个屏幕上看起来很好:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
答案 0 :(得分:2)
来自setImageResource()的文档。 (强调是我的)
这会在 UI线程上进行位图读取和解码,这会导致延迟打嗝。如果这是一个问题,请考虑使用setImageDrawable(android.graphics.drawable.Drawable)或setImageBitmap(android.graphics.Bitmap)和BitmapFactory。
我强烈建议您使用Picasso库来显示图片。在getView()
:
Picasso.with(context).load(item.getDrawable()).into(picture);
就是这样。
public class ImageAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
private Context context;
public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
this.context = context;
}
.
.
.
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = (Item) items.get(i);
Picasso.with(context).load(item.getDrawable()).into(picture);
name.setText(item.name);
return v;
}
}