java - 带有可绘制对象的 ListView 滚动性能缓慢
问题描述
我正在使用自定义 ArrayAdapter 来填充 ListView。
我的适配器代码:
public class LocationAdapter extends ArrayAdapter<Location> {
public LocationAdapter(@NonNull Context context, ArrayList<Location> locationArrayList) {
super(context, 0, locationArrayList);
}
static class ViewHolder {
public TextView nameText;
public TextView descriptionText;
public ImageView image;
}
@NonNull
@Override
public View getView(int position, @Nullable View listItemView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
// check if the existing view is being reused
if (listItemView == null) {
// if not, inflate the view using location_list_itemlist_item.xml
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.location_list_item, parent, false);
// configure view holder
viewHolder = new ViewHolder();
viewHolder.nameText = listItemView.findViewById(R.id.name_text_view);
viewHolder.image = listItemView.findViewById(R.id.image_view);
viewHolder.descriptionText = listItemView.findViewById(R.id.description_text_view);
listItemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) listItemView.getTag();
}
// getting Location object in this position in ArrayList
Location currentLocation = getItem(position);
// using ViewHolder to set text/images to views
viewHolder.nameText.setText(currentLocation.getName());
viewHolder.descriptionText.setText(currentLocation.getDescription());
viewHolder.image.setImageResource(currentLocation.getImageResourceId());
return listItemView;
}
}
我的列表项 XML 代码,非常简单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name_text_view"
android:layout_width="368dp"
android:layout_height="22dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="@+id/image_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:text="Oleviste church"
android:elevation="2dp" />
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/image_content_description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/description_text_view"
android:layout_width="0dp"
android:layout_height="51dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_view"
tools:text="This modern museum is located inside a former seaplane hangar and boasts a submarine and an ice breaker among its exhibits." />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
另外,我的 ListView XML 代码以防万一:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollingCache="false"
android:animationCache="false"/>
在应用程序中滚动视图时,我遇到了一些口吃。请注意,我使用的图像是本地可绘制的。
到目前为止,我已经尝试了以下方法:
- 在 Photoshop 中裁剪可绘制对象以适合所需的纵横比 -速度显着提高,但仍然有点慢。
- 使用 ViewHolder - 速度可能略有提高。
- 向 ListView添加属性
android:scrollingCache="false"
-android:animationCache="false"
可能会略微提高速度。 - 使用 ConstraintLayout 而不是 RelativeLayout - 可能会略微提高速度。
有什么想法我接下来应该尝试什么?
解决方案
只需使用支持库中的 RecyclerView 而不是 ListView。那应该会提高性能。有关更多信息,请参阅此 SO 问题。 Android Recyclerview vs ListView with Viewholder
推荐阅读
- url - 如何使用完全限定的服务器名称地址访问 Rabbitmq 管理 UI?
- python - Python 日期格式
- quaternions - 妙手镯;如何从四元数到前臂方向单位向量?
- nginx - Unirest 连接超时
- postgresql - 我在批量创建操作期间遇到了以下死锁。调试此问题的可能方法是什么?
- c# - 如何将对象列表转换为一维数组?
- android - 为什么 QBDialog 的收件人 ID 始终为空,但在 Quickblox 中有 2 个不同的对手 ID
- c++ - 使用 SFINAE 的 C++ 构造函数特化
- angular - 在表格上显示从自动完成中选择的值
- c++ - 检查失败:GetOpGradFactory()->insert({op, func}).second MapAccumulate 的重复梯度