Android BaseAdapter getView

Source: Internet
Author: User

Android BaseAdapter getView

ListView is a commonly used control in Android.

Android uses an Adapter to manage ListView.

However, the Adapter provided by the system cannot meet some complex display conditions. In this case, we need to use BaseAdapter to control the ListView from rows.

Other methods are quite easy to explain. Here we mainly explain the getView method.

First, during the design of ListView, in order to ensure the system performance (after all, it is a mobile terminal and the system resources are limited), The ItemView is not infinitely loaded during the design.

Imagine if there are 1000 records, do you want to create and cache 1000 ItemView objects at the same time? What if it still contains images and other large resources?

Obviously, this is unrealistic. Therefore, Android adopts the following design ideas.

As shown in, the system only creates one screen ItemView. The number of items is calculated based on the display control.

When the first itemView is completely overwritten, the system will move it as the last ItemView. In this way, only seven itemviews are always instantiated.

However, the display content of Item8 and Item1 is obviously different. At this time, we need to use the GetView method.

 

ConvertView in getView is actually a reference to the ItemView displayed on the screen. On the first screen, the data display is that convertView is empty because both of them need to be re-created.

When the first item of the second screen is displayed, convertView is not empty, but the first item of the first screen is moved.

What we need to do at this time is to refill the new data to ensure that it displays the first item of data on the second screen.

Reference link: http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html

As for the PlaceHolder that we often see, it is actually a common method, in order not to re-getViewByID every time to obtain the object reference.

In short, it is easier to use. In fact, you do not have to use this item. It is neither a waste of Code nor resources that can be retrieved from convertView every time.

 

Next, let's talk about the repeated call of getView, most of which are caused by re-painting. Generally, the width and height of the prize Item are set to fill_parent, so try not to use match. This will be much better.

If you need to redraw the viewlist, you can use notifyDataSetChanged to refresh the interface.

 

 

In the end, I felt that many Android controls have high coupling design, and it is very difficult to separate variables by myself.

The author wants to write a general Adapter. Let's abstract and open variable factors in GetView. As a result, it is found that many interface objects are generated due to this implementation, it seems that it is not as convenient as a page Adapter, so it is not ugly here. This also shows why Android does not have some well-developed quick development frameworks.

 

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.