l33t
l33t

Reputation: 19937

ListView with 20 items lags when scrolling

Android 4.1.2. I have a LinearLayout with a ListView child. I add 20 rows to this listview, with a custom row layout with three TextView controls. Half of these rows are visible, so I need to scroll to see the rest. The problem is that the app seems to hang for a couple of seconds when I start scrolling from the top or from the bottom (in the middle it seems ok).

I understand there are several optimizations that can be used. So far, I have tried this:

  1. In my custom ArrayAdapter I'm using a ViewHolder to avoid calling findViewById.
  2. In getView(), the LayoutInflater is only used when convertView is null.
  3. getViewTypeCount() returns 1, getItemViewType(...) returns 0.
  4. Removing all calls to setText() and setColor() on my textviews.
  5. Disconnecting the debugger.

What other possible bottlenecks are there for listviews?

My row layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/my_text1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/my_text2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:ellipsize="end"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/my_text3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="right"
        android:textColor="@android:color/white" />
</LinearLayout>

I did suspect the layout_weight attribute, but removing them did not help! In another attempt to fix this lagging problem, my ListView has these styles:

<ListView
        android:id="@+id/my_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dip"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:persistentDrawingCache="scrolling"
        android:scrollingCache="false" />

As you can see, I have tried almost everything. Still, when I scroll through these 20 rows the UI hangs for 2-4 seconds when reaching the top/bottom. Hence, scrolling back and forth makes the app freeze!

What am I missing here?

Upvotes: 2

Views: 3097

Answers (1)

saschoar
saschoar

Reputation: 8230

Another thing to consider is the possible overdraw in your list items, which can be made visible by an option in the developer settings on your device.

For general performance issues, especially in a ListView, I can only recommend Romain Guy's "epic" article: Android Performance Case Study. Overdraw is also referenced there as one major issue.

Upvotes: 3

Related Questions