我有一个GridView
附加BaseAdapter
。我尽量保持getView()
尽可能直,我正在使用ViewHolder
模式。
GridView
在纵向模式下有一列,在横向模式下有两列,性能差异很大。使用1列,列表滚动非常平滑,而有2列,它滞后和断断续续。
GridView
还有其他性能提示吗?
对于getView方法,我尝试不做任何事情,除了在列表项上设置一些文本,即使没有设置图像,它也会在2列模式下断断续续。 使用Nexus 10 btw,因此硬件不是问题。
编辑:这是我的getView
方法:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
checkLoadMore(position);
final Item item = combinedGsr.getItems().get(position);
if (convertView == null) {
convertView = myInflater.inflate(R.layout.item_result, null);
holder = new ViewHolder();
holder.Tv1 = (TextView) convertView.findViewById(R.id.textView1);
holder.Tv2 = (TextView) convertView.findViewById(R.id.textView2);
holder.Tv3 = (TextView) convertView.findViewById(R.id.textView3);
holder.Tv4 = (TextView) convertView.findViewById(R.id.textView4);
holder.Btn = (CheckableImageButton) convertView
.findViewById(R.id.favbtn);
holder.Iv = (ImageView) convertView.findViewById(R.id.imageView1);
holder.Root = (RelativeLayout) convertView.findViewById(R.id.root);
holder.ImageRoot = (RelativeLayout) convertView
.findViewById(R.id.imageroot);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.Tv1.setText(item.getTitle());
String preis = item.getPrice();
if (preis.contains("Kaufpreis") && preis.length() > 10) {
String ps = preis.substring(10, preis.length());
Log.d("LAY", "String " + ps);
String ptext = "Kaufpreis: " + "<font color='#000000\'>" + ps
+ "</font>";
holder.Tv2.setText(Html.fromHtml(ptext));
}
else {
holder.Tv2.setText(preis);
}
String land = item.getCountry();
if (land.length() > 15) {
String es = land.substring(15, land.length());
String ltext = "Land: " + "<font color='#000000\'>" + es // tried not using it, makes no difference
+ "</font>";
holder.Tv3.setText(Html.fromHtml(ltext));
}
else {
holder.Tv3.setText("");
}
holder.Tv4.setText(item.getLocation());
String nurl = Tools.convertImageUrl(item.getThumbnailUrl()); // this is just some regex, tried not using it, makes no difference
displayImage(nurl, holder.Iv, holder.ImageRoot); // This uses the UniversalImageLoader library to display an Image
int pad = holder.Root.getPaddingBottom();
if (GlobalApp.favItemsContain(item.getGuid())) { // I had this in suspicion, tried not using it, makes no difference
holder.Btn.setChecked(true);
holder.Root.setBackgroundResource(R.drawable.listitem_bg_green);
}
else {
holder.Btn.setChecked(false);
holder.Root.setBackgroundResource(R.drawable.listitem_bg_gray);
}
holder.Root.setPadding(pad, pad, pad, pad);
holder.Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String guid = item.getGuid();
int[] location = new int[2];
v.getLocationOnScreen(location);
if (GlobalApp.favItems(guid)) {
GlobalApp.removeFromFavItems(guid);
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(
ctx,
ctx.getResources().getString(
R.string.item_entfernt),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.LEFT, location[0],
location[1]);
toast.show();
}
else {
GlobalApp.addToFavItems(guid, item);
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(
ctx,
ctx.getResources().getString(
R.string.item_gesetzt),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.LEFT, location[0],
location[1]);
toast.show();
}
}
});
return convertView;
}