【发布时间】:2015-12-31 12:43:47
【问题描述】:
好吧,我的自定义列表视图非常慢,几乎无法使用,我已经阅读了很多帖子但没有人帮助我修复它,我希望你们中的一个可以,谢谢。
这是我的项目列表视图 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="300px"
android:id="@id/iv_tipo"
android:scaleType="centerCrop"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/material_blue_grey_800"
android:textColor="#ffffff"
android:textAlignment="center"
android:textSize="27px"
android:paddingLeft="10px"
android:id="@id/tv_titulo"
android:paddingTop="10px"
android:paddingBottom="20px"
android:paddingRight="5px"
android:layout_marginLeft="5px"
android:layout_marginRight="5px"/>
</LinearLayout>
这是适配器
public class TrucoListAdapter extends ArrayAdapter {
ArrayList<Truco> trucos;
public TrucoListAdapter(Context context, ArrayList<Truco> trucos) {
super(context, 0, trucos);
trucos = this.trucos;
}
@Override
public Object getItem(int position) {
return super.getItem(super.getCount() - position - 1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.truco_list_item, null);
}
ImageView ivTipo = (ImageView) convertView.findViewById(R.id.iv_tipo);
TextView tvTitulo = (TextView) convertView.findViewById(R.id.tv_titulo);
Truco truco = (Truco)getItem(position);
switch (truco.getTipo()) {
case "m":
ivTipo.setImageResource(R.drawable.coins);
break;
case "c":
ivTipo.setImageResource(R.drawable.cards);
break;
case "a":
ivTipo.setImageResource(R.drawable.guess);
break;
case "d":
ivTipo.setImageResource(R.drawable.desa);
break;
default:
ivTipo.setImageResource(R.drawable.home);
}
tvTitulo.setText(truco.getTitulo());
return convertView;
}
}
编辑:我按照@KishanSoni 的建议修改了我的适配器,但它仍然很慢。
【问题讨论】:
-
那是因为你没有使用
ViewHolder pattern -
@EpicPandaForce 感谢您的快速回答,我尝试了一次但没有解决问题
-
首先使用BaseAdapter而不是ArrayAdapter
-
使用像
Picasso or UIL or Glide这样的图像缓存库加载图像..这是你的列表视图滞后的唯一原因..
标签: android performance listview adapter android-custom-view