Why does android ListView call getView () multiple times during initialization?

Source: Internet
Author: User

Today, we made a function: When initializing the ListView, we set the background of the first line to yellow, and saved the first row of objects, which is used to reset the row to white when other rows are clicked. If (position = 0) {convertView. setBackgroundColor (Color. YELLOW); lastconvertView = convertView;} when running the result, the Color of the first line is always YELLOW and cannot be changed. After debugging, it is found that if (position = 0) in getView will be accessed multiple times. The final result is that the lastconvertView obtained last time is not the first row in the listview. After checking the information on the Internet, the reason is that the height of the listview is not fixed, but the root cause cannot find the description. So I went through the source code + a lot of debugging, probably figured out the cause, and recorded it here. The first step is to describe the display mechanism of the ListView. The listview mechanism is like this: if you have 1000 pieces of data but only 10 pieces of data can be displayed on the screen, When you load the display for the first time, 10 views will be created first, 1-10. When you drag the Listview to hide 1 and display 11, the system will automatically pass the View filled with 1, pay attention to the getView method @ Overridepublic View getView (final int position, View convertView, ViewGroup parent) of the Code Adapter. converView here is the view of 1. Generally, this view will be reused, as the 11 view. When the height of the listview is fixed (fill_parent or directly fixed), The listview can easily calculate how many rows can be displayed in the container. However, if we use "wrap_content", the ListView itself performs some trial calculation only when the on-screen control is fully loaded to know how many rows of data can be displayed. Some methods called onMeasure can be found in the source code. This is the purpose of Visual Testing (the source code is slightly complicated and not fully read ). After the listview calculates the total number of rows required on the screen, if the height of the listview itself remains unchanged, the number of rows it can accommodate will not change. You can use getChildCount () to get its maximum number of rows. Back to the original question, why is the last result not the first row of listview? Set listview to "wrap_content" and use the following test code to view the output. // Obtain the number of the current listview. The number of outputs is equal to the number of site names. The number of outputs and the "NONE" if (listView. getChildCount () = position) {// Number of Children current position + site name Log. I ("", listView. getChildCount () + "" + position + "" + coordInfo. stationname);} else {// Number of Children Current position + no Log. I ("", listView. getChildCount () + "" + position + "" + "");} in my test application, listview can just put 11 Views and check the output, at the beginning, listview instantiated 11 views for filling, that is, the view ending with the first 10 "NONE" + the first "Passenger center, the listview capacity is measured. In other words, these 11 views are only temporary views for measurement. In addition, after filling, listview creates 11 temporary views again to check whether the height is correct. Due to the logic design error of my code, when this step is performed, a temporary view is assigned to lastconvertView because position is equal to 0 again. The problem is solved after the height of the listview is set to fill_parent. In addition, let's talk about another solution, if (parent. getchildcount () = position) {code to be executed under normal circumstances} else {here is the problem of loading multiple times. You can ignore the code here ,} this method is not feasible, because, without changing the height of the listview, The getchildcount () of the listview is fixed after loading, and the position refers to the position in the adapter, when the number of adapters is greater than the size of the listview, the judgment condition is not true, that is, sliding listview is not true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.