preface

Slide-list frame rate optimization is one of the most common optimizations in development. The sliding frame rate is a direct reflection of the stalling condition of the sliding list, which is closely related to the user experience.

Several directions of optimization

  1. Overdrawing, overdrawing will lead to the increase of RecyclerView drawing time, when the list item is more complex, it is very easy to lose frames.
  2. Image loading, in the process of sliding, if the image loading is carried out at the same time, the CPU will be taken up too much. If the image decoding happens in the main thread, it will be more likely to stall. Therefore, stopping image loading in the process of sliding list will be very helpful for frame rate optimization.
  3. Time consuming method. You can trace the time consuming method in the sliding process and check whether the call frequency can be reduced. Time consuming method will lead to long drawing time and frame loss. Optimizations can place these time-consuming methods in child threads, with the main thread only keeping the parts related to drawing.
  4. Viewholders are created pre-constructor. often viewholders are created during sliding (before there are not enough viewholers available for reuse) by inflate an XML, At the same time, a series of operations, such as findViewById, are often performed. These operations are time-consuming when the item is complex and often result in frame loss. Optimization method: Pre-create ViewHolder and feed it into RecycledViewPool. Common business is common. Before RecyclerView shows data, there will often be network request operation, so we can make full use of this period of network request. We can pre-create these ViewHolder.
  5. Thread pool reuse, example: Each Item of vertical RecyclerView contains a horizontal RecyclerView. If these horizontal recyclerViews have the same style, they can share a RecycledViewPool. This can greatly reduce the creation of horizontal RecyclerView items, which will be helpful for frame rate optimization. At the same time, these items can also be pre-created.
  6. In the process of RecyclerView sliding, onBindViewHolder will be called for many times, so onBinderViewHolder avoids creating objects for many times and executing time-consuming methods.