我有GridView
一个BaseAdapter
附在它上面的。我尽量保持getView()
笔直,我正在使用这种ViewHolder
模式。GridView
纵向模式有 1 列,横向模式有 2 列,性能差异很大。使用 1 列时,列表滚动非常顺畅,而使用 2 列时,列表会滞后和卡顿。
是否有任何额外的性能提示GridView
?
至于 getView 方法,除了在列表项上设置一些文本之外,我尝试不做任何事情,即使没有设置图像,它也会在 2 列模式下结结巴巴。顺便说一句,使用 Nexus 10,所以硬件不是问题。
编辑:这是我的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;
}