RecyclerView lagging inside ScrollView

拟墨画扇 提交于 2020-01-03 06:36:13

问题


I have multiple RecyclerView inside a ScrollView and RecyclerView load data from the server. RecyclerView working very slow when I scroll down it lagging. How can I solve it?

 <Scrollview>
           <LinearLayout> 
                       <Recyclerview>  

                       <Recyclerview>

           </LinearLayout>
    </Scrollview>

回答1:


Usually, when a RecyclerView has scrolling performance issues, it's because

a) You're doing too much work in onBindView, which is called every time a new item comes on screen. b) The views you're displaying reference objects which use lots of memory (like large images), so the Android Garbage Collector (GC) has to do lots of work to free up memory. A quick and dirty way to measure (a) is something like:

void onBindView(RecyclerView.ViewHolder holder, int position) {
    long startTime = System.currentTimeMillis();

    //..bind view stuff

    Log.i(TAG, "bindView time: " + (System.currentTimeMillis() - startTime));
}

This will give you a rough idea of how long bindView is taking. If it's ~10ms it's OK. If it's >10ms it's not good. If it's >50ms it's really bad.

For (b), it really depends on how you're loading your data/images. If you're using something like Picasso or Glide, a lot of this stuff is handled for you. You can attach an onViewRecycled listener to your RecyclerView, and then if you are using Glide/Picasso, don't' forget to call the recycle() method (or whatever it's called).

If you're manually loading images here, then maybe just call ImageView.setImage(null) when the view is recycled. Also, make sure you're loading images at only the size you need, and if you are doing any processing to load/downsample images, make sure it's happening in the background.

If you're using custom fonts, make sure you use some sort of caching mechanism. Loading fonts can be expensive.

Anyway, (a) should give you clues as to where the issue is occurring. You can log specific calls inside onBindView() to narrow it down.




回答2:


It is lazy because of the images not the recyclerviews, you need to use Picasso or Glide for efficient loading of images try .fit().centerCrop()



来源:https://stackoverflow.com/questions/41115406/recyclerview-lagging-inside-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!