Get right to the point……

Problem 1: Adapter nested Adapters

** if a listView has already been used, then a listView is loaded into the bindData of a ViewHolder of a type, depending on the type of the listView. **** Test, in Huawei Android 7 and lower versions of the slide will be stuck, ** solution: try to use a ListView, a adapter, by adding type type to meet the load of different items

Problem two: XML layouts are too deeply nested

From the drawing process of Android View,

  • Measure: measures the view size. The measure method is recursively called from the top-level parent View to the child View, and the measure method calls back to the OnMeasure.

  • Layout: Determines the position of the View and carries out the page Layout. The process of recursively calling the view.layout method from the top-level parent View to the child View, that is, the parent View puts the child View in the appropriate position according to the layout size and layout parameters obtained by measure child View in the previous step.

  • Draw: Draws a view. ViewRoot creates a Canvas object and calls OnDraw(). Six steps: 1. Draw the background of the view; ② save the Layer of the canvas; ③ Draw the content of the View; (4) Draw a sub-view of the View, if there is no use; ⑤, restore Layer; ⑥ draw the scroll bar.

If one of the above processes takes too much time, more than 16ms will cause significant lag

1. Try to use MeasureSpec.EXACTLY exact values like match_parent or xx DP.

Reduce hierarchy nesting and use the ConstraintLayout ConstraintLayout provided by androidx

3. Use proportion to dynamically adapt width-to-height ratio

Problem three: accurate guidance, controllability

1. It is very rude for Adapter to call notifyDataSetChanged. If there are too many entries, the performance is wasted and the experience is not good. If you modify only one item in the ListView, refresh that item to save performance.

2. When sending messages between modules and components, it is recommended to add Type to distinguish message types and narrow the range of message recipients. It is possible that the message has many listeners, and if you do not differentiate, those listeners will execute the logic, resulting in wasted performance and unexpected problems

Problem four: coding defects

1. The code is repeatedly nulled, leading to the code to perform some useless operations, affecting the elegance, and at the same time resulting in code redundancy, reducing maintenance, increasing coding costs. When the redundant code reaches a certain level, it will also slow down the operation efficiency of the code

It is suggested to design the code reasonably. Kotlin’s closure mode is very good, and the outer layer is judged to be non-null, so a lot of null-detection code processing can be saved inside

2. Memory leakage caused by coding problems;

1) Do not abuse anonymous inner classes, it is easy to cause memory leakage, when leakage reaches a certain level, will OOM;

It is recommended to use static inner classes, or to use weak references, soft references, hold references to be destroyed in onDestory, or hold the application directly

2) If resources are not released in time when SVGA is in use, memory jitter or even overflow will occur;

SVGA increases the local cache, and SVGA clears the canvas in time for detach()

3) Memory overflow caused by Bitmap loading is recommended to use Glide mature framework;

Problem five: the code is not standard, poor structure, resulting in low maintainability, easy to make mistakes coder

Ps: First of all, it is recommended to use Kotlin to improve development efficiency, improve security, and reduce a lot of code compared to the original Java writing

1. It is important to design a good architecture. No matter MVC,MVP or MVVM, each of them has its advantages and disadvantages

2, code naming specifications, whether variables or functions or class names, resource names, module names need to have specifications, do see the name know meaning, eliminate Chinese or symbols!

3. Control the size of each class and function. If the function function is too complex, several more can be extracted

4, each class or function to achieve a single function, pay attention to code reuse, extract common components, public lib libraries to provide capabilities

5, make good use of inheritance and polymorphism characteristics, extract common ability to the parent class, to the base class; Each base class level should be functionally single

6, the use of design patterns can greatly optimize the readability of the code, improve the elegance of the code; Factories, builders, observers, singletons, etc

Reduce the coupling of code, modularity and componentization is a must. (Arouter Ali open Source routing framework)

Instead of using getActivity() to get a reference to the host Activity, you should register the incoming reference that you listen to, so that the reference can be released when the Fragment is being destroyed. Avoiding memory leaks

8, common ability unity; For example, audio and video capabilities, audio and video can share a set of logic to control ExoPlayer(An open source player provided by Google), by determining whether it is audio or video, to determine whether it needs to be passed into the SurfaceView, by whether it needs to use multiple audio sources, to open multiple ExoPlayers

But the gateway to management control is unique;

9. Control the size of resources;

1) Such as rational use of HDPI, XHDPI, XXHDPI, XXXHDPI; For the insignificant placeholder map, as long as it does not affect the user recognition of key information, does not affect the appearance, you can only put HDPI, XHDPI

2) Pictures need to be compressed before use. We need to control the volume of APK package from the source. If APK is too large, low-end mobile phones in overseas countries such as Southeast Asia and Africa may only have 512M in the future.

It is recommended to use SVG vector pictures, if it is not possible to use PNG, JPG, UI students to improve skills ah!!

3) So, third-party AAR, JAR introduction package to control the size, if the dependence is small, it is suggested to introduce part of the source code

10. Increase the means of monitoring problems

For example, Bugly, Leakcary, find the problem and solve it in time