Я использую специальный 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>
Кроме того, мой XML-код ListView на всякий случай:
<?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 - возможно незначительное улучшение скорости.
- Добавление атрибутов
android:scrollingCache="false"
и android:animationCache="false"
в ListView - возможно незначительное улучшениеspeed. - Использование ConstraintLayout вместо RelativeLayout - возможно, незначительное улучшение скорости.
Есть идеи, что мне следует попробовать дальше?