ListView very slow when scrolling (using ViewHolder/recycling)

后端 未结 10 492

I\'m back at trying out some Android dev again. I have an \"old\" HTC Hero phone lying around, so I booted that one up, did some updates and are now up n running again with Ecli

相关标签:
10条回答
  • 2021-02-02 16:56

    I had the same issue before while i was using different layout like LinearLayout, RelativeLayout, CardView as parent of different child in same list view item. I solved that issue by changing all view inside RelativeLayout.

    Using RelativeLayout as main and it's child layout may increase the speed of loading each item. So scrolling will be smooth.

    <RelativeLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="64dip"
    android:orientation="horizontal"
    android:background="@drawable/stateful_background"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/relativeLayout">
        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:src="@drawable/arrow_up_green"
            android:background="@android:color/transparent">
        </ImageView>
    </RelativeLayout>
    <TextView
        android:text="14:46 (15 min)"
        android:id="@+id/time"
        android:textSize="12dip"
        android:textColor = "#000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/relativeLayout"
        android:layout_toEndOf="@+id/relativeLayout" />
    <TextView
        android:text="test"
        android:id="@+id/road"
        android:textSize="12dip"
        android:textColor = "#000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_below="@+id/time"
        android:layout_toRightOf="@+id/relativeLayout"
        android:layout_toEndOf="@+id/relativeLayout" />
    <TextView
        android:text="test test"
        android:id="@+id/name"
        android:textSize="12dip"
        android:textColor = "#000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_below="@+id/road"
        android:layout_toRightOf="@+id/relativeLayout"
        android:layout_toEndOf="@+id/relativeLayout"/></RelativeLayout>
    
    0 讨论(0)
  • 2021-02-02 16:59

    After optimizing my getView() method to use a holder and to reuse convertView if it's not null, my listview was still pretty laggy. Then, I've tried

    listView.setScrollingCacheEnabled(false);
    

    and it solved it at once.

    Hope this helps someone.

    0 讨论(0)
  • 2021-02-02 16:59

    I just discovered this and I wanna share it with you guys.

    I tried ALL the solutions provided but nothing worked. What was causing the problem is the length of the text I am feeding one of my TextView views because i'm using

    mTextView.SetText(theLongString)
    

    in my adapter in my getView method. Now that I shrinked my String, the listview is VERY smooth :)

    0 讨论(0)
  • 2021-02-02 17:05

    Load the image once as Bitmap and apply it to the ImageView programmatically after inflating it. If you need different images per item you should create the Bitmaps asynchronously like described in Android: How to optimize ListView with ImageView + 3 TextViews?

    0 讨论(0)
  • 2021-02-02 17:06

    To improve performance of listview use both or any one of the two - (Simple implementation)

    • ViewHolder
    • AsyncTask (a separate thread)

    If you have moderately long lists I recommend ViewHolder otherwise for large lists (like in the case of a music player) I recommend using ViewHolder along with AsyncTask. The key to smooth ListView scrolling is to reduce memory consumption as much as possible.

    To implement ViewHolder, is simple. Just create a static class in your custom Adapter that holds all the views that you find via findViewById. So this is how it should look -

    static class ViewHolder
    {
    TextView text;
    TextView timestamp;
    ImageView icon;
    ProgressBar progress;
    int position;
    }
    

    Then, the next time you need to findViewById any of these views, just reference the ViewHolder like this-

    ViewHolder holder = new ViewHolder();
    holder.icon = (ImageView) yourView.findViewById(R.id.listitem_image);
    holder.text = (TextView) yourView.findViewById(R.id.listitem_text);
    holder.timestamp = (TextView) yourView.findViewById(R.id.listitem_timestamp);
    holder.progress = (ProgressBar) yourView.findViewById(R.id.progress_spinner);
    

    This is tested code and taken from one of my projects. However, the original source is here - http://developer.android.com/training/improving-layouts/smooth-scrolling.html

    The lists become smoother using ViewHolder. Using AsyncTask is a little more complex, but do check out the link if you wish to implement the AsyncTask along with ViewHolder for a much smoother scrolling experience. Good Luck!

    0 讨论(0)
  • 2021-02-02 17:06

    You can use listview.setScrollingCacheEnabled(false).When scrolling listview application hold area then throwing Out Of Memory(OOM) exception.My solution is worked for me.

    0 讨论(0)
提交回复
热议问题