List View Scroll Not Smooth

Robert Ray picture Robert Ray · Nov 18, 2013 · Viewed 14.5k times · Source

i have a custom list view, which displays users, and there photos i retrieve the data from API, Which gives JSON Output,

My Issue is that the list view is not scrolling smoothly, it hangs for a sec and scrolls, it repeats the same till we reach the end.

i thought it might me because i am running network related operation on the UI thread, but it continues to do that even after it completes loading?

the structure of my custom Listview is

 <TextView  style="@style/photo_post_text"
             android:id="@+id/photo_post_text"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="demotext"
           />


        <ImageView
            android:id="@+id/userimage"
           android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"

            android:adjustViewBounds="true"
           android:src="@drawable/pi" 
           />

Answer

Blacklight picture Blacklight · Nov 18, 2013

Use an AsyncTask for the loading of your pictures. The scrolling will not be smooth as long as you do such tasks in the UI thread.

Have a look at this tutorial. It will help you understand what you need to implement without the need of additional libraries. Also please keep in mind that the rows are redrawn all the time while you scroll, there's no real "finishing" of loading. You can additionally consider an image-cache, e.g. with a ConcurrentHashMap in which you could put your loaded pictures.