Solve the problem of multiple call to the getview of listview and gridview. gridviewgetview
During development, we often encounter some strange problems, but we still have to find a solution. There are many online solutions, but we still have to solve them, then we must test it first.
You need to load the custom content of the two controls twice, but when you print the content, it is found that it runs more than twice, the log in getView and getCount is called frantically, and each LOG in getVIew is the total number of records, and the position value of the total number of data records is 0. GetView of a GridView is called repeatedly. Especially when you need to load images in ItemView, it is easy to cause excessive GC and ANR. Why?
2.-Non-custom controls (native)
This is a number of calls that occur when the android mechanism is to calculate the height. Therefore, if you are not a custom control, you 'd better change the width and height attribute of this control: the connection property is changed to android: layout_width = "fill_parent" android: layout_height = "fill_parent. It is best to set its parent layout as well.
- Custom Controls (onmeasure and onlayout need to be rewritten)
These controls are customized, and their item layout is often complicated. Then we need to dynamically calculate the height of the control, and then the layout should be calculated at which position, if you try it out, it may be misplaced.
According to Google's explanation, View is divided into two phases during Draw: measure and layout. In the measure stage, two parameters are calculated: height and width. Note that this is a recursive process. From top to bottom, DecorView calls the measure of its sub-elements in turn. After the two parameters are calculated, layout is started and draw is called. For ListView, of course, each Item will be called the measure method. In this process, getView and getCount will be called.
Then we usually use baseAdapter and getView () for multiple calls on the display interface. Therefore, reuse of converview is critical. (Judge whether it is in onmeasure. If so, only mInflater. inflate (R. layout. XXX), and then immediately return this convertView. If it is not in onmeasure, then go to the real onlayout.) Here is the reference [Export.
Because listView does not expose the interface to let us know whether the onMesure () method is executed, we only need to add a Boolean value to judge:
The code implementation is:
Public boolean isMeasure; @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {Log. d ("onMeasure", "onMeasure"); isMeasure = true; // calculate the width and height of itemView ......}
@ Override protected void onLayout (boolean changed, int l, int t, int r, int B) {Log. d ("onLayout", "onLayout"); isMeasure = false; // set layout ......} next we modified the getView () method of the adapter: public View getView (int position, View convertView, ViewGroup parent ){... // determine whether the parent control object is the view object you want to display if (parent instanceof MyGridView) {if (MyGridView) parent ). isMeasure () {return convertView ;}}...}
Then, if you want to implement the Click Event of the control, you need to distribute the event on your own and pass the non-consumption event of the parent control to the Child control, then, the Child item may have a control that preemptible the focus. You can set it to "false" in the Child control setFocus = "false"
Or the parent control android: descendantFocusability = "afterDescendants"
I believe that you should know how to do it. This article also serves as a reference to others' experience. We need to have a spirit to take the initiative. There are also a lot of things about reaching out to the party in the tianchao dynasty, but there is nothing to do with it, there is nothing wrong with stepping on others' shoulders. As long as you can make progress every day and learn what you want