1

I am using a custom ArrayAdapter to populate a ListView.

My adapter code:

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;
    }
}

My list item XML code, very simple:

<?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>

Also, my ListView XML code just in case:

<?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"/>

I experience some stuttering when scrolling the view in the app. Note that the images that I use are local drawables.

So far I have tried the following:

  1. Cropping the drawables in Photoshop to fit the desired aspect ratio - significant improvement in speed but still a bit slow.
  2. Using ViewHolder - maybe marginal improvement in speed.
  3. Adding attributes android:scrollingCache="false" and android:animationCache="false" to the ListView - maybe marginal improvement in speed.
  4. Using ConstraintLayout instead of RelativeLayout - maybe marginal improvement in speed.

Any ideas what I should try next?

Igor
  • 991
  • 3
  • 12
  • 27

2 Answers2

1

Just use RecyclerView from the support library instead of a ListView. That should improve performance. Refer to this SO question for more insight. Android Recyclerview vs ListView with Viewholder

argon2008
  • 11
  • 1
0

I was able to solve the issue with ListView scrolling using Glide framework. It is a robust framework focused on making scrolling lists with any kind of images fast and smooth.

All I had to do was to modify build.gradle:

repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.7.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}

Then in my adapter code I removed the following line (as setImageresource is not used anymore to load drawables):

viewHolder.image.setImageResource(currentLocation.getImageResourceId());

And added the following line to load drawables using Glide:

   Glide.with(getContext()).load(currentLocation.getImageResourceId()).into(viewHolder.image);

More detailed information on how to use Glide is provided in the documentation.

Igor
  • 991
  • 3
  • 12
  • 27