那麼我的自定義listview是非常非常慢,幾乎無法使用,我一直在閱讀很多帖子,但沒有人幫助我解決它,我希望你們中的一個人可以,謝謝。自定義listview太慢
這裏是我的項目的ListView 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
@EpicPandaForce感謝您的快速答覆,我試過一次,但沒有解決的麻煩 – Rorschach
首先是使用BaseAdapter代替ArrayAdapter –