Solve the problem of nested ListView or RecycleView stuck in ScrollView

Solve the problem: <listview slides up and down and freezes>

As long as the listview slides up and down, the main reason is when the listview fills the adapter. In the rewritten class BaseAdapter method getView(); is called in an infinite loop; this consumes a lot of memory and causes the listview to slide up and down, and there is a freeze. The reason lies in the measurement process, ListView generally loads multiple Items according to the screen, and also displays several groups of Items that have been loaded at the same time, and the parent elements of these Items are the ListView.

According to Google's explanation: View is divided into two stages during Draw: measure and layout. In the measure stage, it is mainly to calculate two parameters: height and width. And it should be noted that this is a recursive process. From top to bottom, DecorView starts to call the measure of its own child elements in turn. After the calculation of these two parameters is completed, the layout starts, and finally the draw is called. For ListView, of course, each Item will be called Measure method, and during this process getView and getCount will be called, and depending on the user's needs, there may be many calls. The problem lies in the definition of the height and width properties of the ListView or its parent element in the layout. According to my experience, it is best to set both height and width to fill_parent, which can avoid crazy calls to getView from listview. The solution is to avoid adaptation as much as possible, unless it is a last resort, the effect of fixed size or padding will be better. Another point to note is that if you use the above dynamic measurement to set the display height of the listview, and use fill_parent to set the properties of the listview, you still can't stop the crazy calls of the getView() method in the adapter; or it is accompanied by the listview The content display is still incomplete; according to my own practical experience, you can do this: place the listview in the LinearLayout, and there is only one listview in the <LinearLayout>; I infer that when you load it by dynamic measurement, ta needs a separate space.

In this way, it is basically solved. When the listview is placed in the ScrollView, the slide is stuck and the display is incomplete.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327062266&siteId=291194637