带有可绘制对象的 ListView 的缓慢滚动性能
Slow scroll performance of ListView with drawables
我正在使用自定义 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
我能够使用 Glide framework 解决 ListView 滚动的问题。它是一个强大的框架,专注于快速流畅地制作包含任何类型图像的滚动列表。
我所要做的就是修改 build.gradle
:
repositories {
mavenCentral()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
然后在我的适配器代码中删除了以下行(因为 setImageresource 不再用于加载可绘制对象):
viewHolder.image.setImageResource(currentLocation.getImageResourceId());
并添加了以下行以使用 Glide 加载可绘制对象:
Glide.with(getContext()).load(currentLocation.getImageResourceId()).into(viewHolder.image);
the documentation 中提供了有关如何使用 Glide 的更多详细信息。
我正在使用自定义 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
我能够使用 Glide framework 解决 ListView 滚动的问题。它是一个强大的框架,专注于快速流畅地制作包含任何类型图像的滚动列表。
我所要做的就是修改 build.gradle
:
repositories {
mavenCentral()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
然后在我的适配器代码中删除了以下行(因为 setImageresource 不再用于加载可绘制对象):
viewHolder.image.setImageResource(currentLocation.getImageResourceId());
并添加了以下行以使用 Glide 加载可绘制对象:
Glide.with(getContext()).load(currentLocation.getImageResourceId()).into(viewHolder.image);
the documentation 中提供了有关如何使用 Glide 的更多详细信息。