Search code examples
androidxmlandroid-recyclerviewscrollview

Lagging issue of Recycler View inside Scroll View


Multiple RecyclerView inside a ScrollView and RecyclerView has been lagging and sometimes freezing. Need guidance for it? What I missed or should be the improvement of this design.

 <Scrollview>
           <LinearLayout> 
                       <Recyclerview>  
                     
                       <Recyclerview>
        
           </LinearLayout>
    </Scrollview>

Solution

  • 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.