android self-study! Someone finally sorted out all the knowledge points that Android programmers must learn, with a course + question bank

## 数据处理和视图加载分离Separate data processing and view loadingSeparate data processing and view loading

We know that pulling data from the remote must be asynchronous. After we pull the data, we may hurriedly throw the data to the VH for processing. In fact, we should also place the data processing logic in asynchronous processing. After the Adapter notify the change, ViewHolder can do data and view binding logic simply and without pressure, such as:

mTextView.setText(Html.fromHtml(data).toString());

The Html.fromHtml(data) method here may be more time-consuming. If there are multiple TextViews, it will be more time-consuming, which will cause frame drops and freezes. If you put this step together with the network asynchronous thread From the perspective of users, the network refresh time is a little longer at most.

Data optimization

Pull remote data by page, cache the pulled remote data, improve the speed of secondary loading; for newly added or deleted data, use DiffUtil to refresh the data locally instead of blindly refreshing the data globally.

Layout optimization

Reduce transition drawing

To reduce the layout level, you can consider using a custom View to reduce the level, or set the layout more reasonably to reduce the level. It is not recommended to use ConstraintLayout in RecyclerView. Many developers have already reported that using it is worse. Related links are: Is ConstraintLayout that slow?, constraintlayout 1.1.1 not work well in listview.

Reduce xml file inflate time

The xml file here includes not only the xml of the layout, but also the xml of the drawable. The xml file inflate out the ItemView through time-consuming IO operations, especially when the probability of Item reuse is very low, as the Type increases, this The loss caused by inflate is quite large. At this time, we can use code to generate the layout, that is, the new View() method, as long as we figure out the API corresponding to the attribute of each node in the xml.

Reduce the creation of View objects

A slightly complex Item will contain a large number of Views, and the creation of a large number of Views will also consume a lot of time, so the ItemView should be simplified as much as possible; when designing the ItemType, try to design a custom View for the parts that can be shared by multiple ViewTypes to reduce the View The construction and nesting.

other

Others do not mean that they are not important, but I can't classify them. Some operations may greatly optimize your RecyclerView.

  • Upgrade RecycleView version to 25.1.0 and above to use Prefetch function, please refer to RecyclerView data prefetching.
  • If the Item height is fixed, you can use RecyclerView.setHasFixedSize(true); to avoid requestLayout wasting resources;
  • Set RecyclerView.addOnScrollListener(listener); to stop loading during the sliding process.
  • If animation is not required, you can pass ((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false); to turn off the default animation to improve efficiency.
  • Use String.toUpperCase for TextView instead of android:textAllCaps="true".
  • Use StaticLayout or DynamicLayout custom View for TextView instead of it.
  • Recycle resources by overriding RecyclerView.onViewRecycled(holder).
  • Use RecycleView.setItemViewCacheSize(size); to increase the cache of RecyclerView and use space for time to improve the smoothness of scrolling.
  • If the Adapter of multiple RecycledViews is the same, for example, the same Adapter exists in the nested RecyclerView, you can share a RecycledViewPool by setting RecyclerView.setRecycledViewPool(pool);.
  • Set up a listener for the ItemView. Don't call addXxListener for each Item. You should share an XxListener and perform different operations based on the ID to optimize the resource consumption caused by frequent object creation.
  • Increase the extra space reserved by RecyclerView through getExtraLayoutSpace (outside the display range, there should be extra cache space)

At last

May you one day love yourself and be kind to yourself.

This article is in an open source project: What about Android development? How to get a high salary  in an interview has been included, including self-learning programming routes in different directions, interview question collection/face-to-face, and a series of technical articles, etc. The resources are continuously being updated...

Guess you like

Origin blog.csdn.net/Sunbuyi/article/details/112985493