gpt4 book ai didi

Android Studio 自定义 ListView 滚动缓慢并导致应用停止

转载 作者:太空狗 更新时间:2023-10-29 16:28:34 25 4
gpt4 key购买 nike

我有一个自定义适配器,只有一个 ImageView 和一个 TextView。滚动时,速度非常慢,如果滚动次数足够多,应用程序最终会停止。我在想我处理这个问题的方式效率不高?

public class CustomAdapter extends ArrayAdapter<String> {

CustomAdapter(Context context, String[] breakfast) {
super(context, R.layout.custom_row, breakfast);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_row, parent, false);
}
String breakfastElement = getItem(position);
TextView customListTextView = (TextView) convertView.findViewById(R.id.customListTextView);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);

customListTextView.setText(breakfastElement);

switch(breakfastElement) {
case "Bacon":
imageView.setImageResource(R.drawable.bacon);
break;
case "Eggs":
imageView.setImageResource(R.drawable.eggs);
break;
case "Toast":
imageView.setImageResource(R.drawable.toast);
break;
case "Ham":
imageView.setImageResource(R.drawable.ham);
break;
case "Hashbrown":
imageView.setImageResource(R.drawable.hashbrowns);
break;
case "Biscuits":
imageView.setImageResource(R.drawable.biscuit);
break;
case "Burrito":
imageView.setImageResource(R.drawable.burrito);
break;
case "Cereal":
imageView.setImageResource(R.drawable.cereal);
break;
case "Oatmeal":
imageView.setImageResource(R.drawable.oatmeal);
break;
case "Sausage":
imageView.setImageResource(R.drawable.sausage);
break;
case "Bagel":
imageView.setImageResource(R.drawable.bagel);
break;
case "Croissant":
imageView.setImageResource(R.drawable.croissant);
break;
case "Orange Juice":
imageView.setImageResource(R.drawable.orangejuice);
break;
case "Milk":
imageView.setImageResource(R.drawable.milk);
break;
}
return convertView;
}
}

如果我只对每个列表元素使用相同的图像,我就不会得到缓慢的滚动。感谢任何帮助

编辑:

  CustomAdapter(Context context, ArrayList<Breakfast> breakfastArrayList) {
super(context, R.layout.custom_row, breakfastArrayList);
breakfastArray = breakfastArrayList;
}

它不允许我将 breakfastArrayList 作为第三个参数传递

最佳答案

有这样的早餐数据课

public class Breakfast {

private String name;
private Drawable drawable;

public Breakfast(String name, Drawable drawable) {
this.name = name;
this.drawable = drawable;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Drawable getDrawable() {
return drawable;
}

public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
}

像这样创建 Breakfast 对象的数组列表

ArrayList<Breakfast> breakfastList = new ArrayList<>();
breakfastList.add(new Breakfast("Bacon",R.drawable.bacon);
...
..
.

将此数组列表传递给您的自定义适配器

public class CustomAdapter extends ArrayAdapter<Breakfast> {

public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

public CustomAdapter(Context context, int resource, List<Breakfast> items) {
super(context, resource, items);
}
...
...
...


customListTextView.setText(breakfastList.get(position).getName());
imageView.setImageDrawable(breakfastList.get(position).getDrawable());

}

PS : 鉴于你刚开始学习Android,建议你看看RecyclerView而不是 ListView

关于Android Studio 自定义 ListView 滚动缓慢并导致应用停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44510720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com