ListView very slow when scrolling (using ViewHolder/recycling)

后端 未结 10 493

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 17:08

    This Might help some one

    If you have an image in your list Item, you have to remember to reduce the quality of that Image. It's allot faster to load in a few Kb's than a few megabytes.

    This helped me

     public Bitmap MakeFileSmaller_ToBitmap(File f) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    
                // The new size we want to scale to
                final int REQUIRED_SIZE=200;
    
                // Find the correct scale value. It should be the power of 2.
                int scale = 1;
                while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                        o.outHeight / scale / 2 >= REQUIRED_SIZE) {
                    scale *= 2;
                }
    
                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
                Log.d(TAG, "FILE NOT FOUND " );
            }
            Log.d(TAG, "OTHER EXCEPTION");
    
            return null;
        }
    
    0 讨论(0)
  • 2021-02-02 17:18

    UPDATE 2011-08-29 If I remove the image in the NodePickup, the lagginess is gone.

    The view has a hard time figuring how the layout should be rendered. The xml you posted don't help much. If you remove the ImageView then the LinearLayout02 will take all the width of the parent. But having the imageView with standar dimentions and the layout to the right will fill_parent confuses the view a lot. Requests the size of the imageView again to "push the margins to the right" (kind of). Take a look at my suggestions below

    Tip1

    use the LinearLayout property weight. Make the imageView fill_parent and the LinearLayout too (for the width) and play with the weight properties. Do that also for the vertical layout with the TextViews. The best Solution whould be to put a fixed size to the height of the TextViews thought. Also consider to change the top view to RelativeLayout. Make the image with standar dimentions , AlignParentLeft and put the LinearLayout02 toRightOf imageView. You will relief the onMeasure of the ViewGroup a lot.

    Tip2

    It seems like when the text changes height the whole view need to be reinflated.A common technic to avoid that it to make list Item fixed in height. So the listview can reuse the recycled views without reinflating.

    Tip3

    Give your LinearLayout02 a fixed height of 64dp or Fill_Parent since you don't have any left space, but the Layout don't know that and try to rearrange it self every time since the text is also Wrap_content.

    Also you said that if you remove the ImageView everything is fast again.If the above don't have any effect can you please try this? Since you know that imageView size is fixed.

    Extend your imageView and override requestLayout() method.

    public class MyImageView extends ImageView {
    
    public PImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    
    }
    
    public PImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
    }
    
    public PImageView(Context context) {
        super(context);
    
    }
    
    @Override
        public void requestLayout() {
            /*
             * Do nothing here
             */
        }
    }
    

    Now include the MyImageView widget to your XML like that.

    <com.package_name.MyImageView 
            android:id="@+id/ImageView01" 
            android:layout_width="40dip" 
            android:layout_height="40dip"
            android:src="@drawable/arrow_up_green"
            android:background="@android:color/transparent">
     </com.package_name.MyImageView >
    
    0 讨论(0)
  • 2021-02-02 17:20

    Try using android:cacheColorHint="#00000000" for your listview. To improve drawing performance during scrolling operations, the Android framework reuses the cache color hint.

    Reference: developer.android.com article.

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

    It took a while! I tried everything. Disabling the scroll cache, viewHolder, cacheColorHint ... but nothing worked!

    After searching many hours I found the root of all evil!

    In my themes.xml I had a scaling background image:

    <item name="android:windowBackground">@drawable/window_bg</item>
    

    After removing the beackground everything was butter smooth.

    I hope this helps someone!

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