Getview learning summary in android ListView
Recently, I am working on android-related development. The ListView has a picture dislocation problem. Today I checked some of the things that many people wrote, so it is a kind of deep understanding to record them. ListView is a very common control, which has a wide range of functions and is similar to that of the GridView. Can store a large amount of data. In addition, when we need more complex la S, we generally use SimpleAdapter, or inherit BaseAdapter to rewrite it ourselves. If it inherits the ArrayAdapter and SimpleAdapter, the parent class maintains a List, so when there is data update, try to use the add of the adapter to ensure getCount () the returned value is correct. One of the most important rewriting functions involved here is that getView has three methods in getView to return the view. Method 1: the simplest and easiest to understand is that each view generates a new view through inflate and returns the public View getView (int position, View convertView, ViewGroup parent) {LayoutInflater inflater = LayoutInflater. from (context); View item = inflater. inflate (R. layout. list_item_icon_text, null); (TextView) item. findViewById (R. id. text )). setText (DATA [position]); (ImageView) item. findViewById (R. id. icon )). setImageBitmap (return item;}) is a disadvantage when the data volume is small. Not obvious, but when a listview contains a large number of entries, this method is very wasteful. Because every appearance, disappearance, or update of an item requires a new inflate. Memory is greatly consumed. So what other methods can be used to save costs? Method 2: Let's take a look at what conertview is in the getView function. View The official documentation and find: convertView-The old view to reuse, if possible. note: You shoshould check that this view is non-null and of an appropriate type before using. if it is not possible to convert this view to display the correct data, this method can create a new view. heterogeneous lists can specify their number of view types, so that this View is always of the right type (see Adapter. getViewTypeCount () and Adapter. getItemViewType (int )). note: getViewTypeCount () is used to return the type of an item with getItemViewType when there are different types of items in the listview, such as split lines, the original converView is used to reuse the view. Find other materials. A classic image is shown as follows: This figure clearly describes how listview can reuse views. Listview requests all visible items through getview. Converview is empty. When item1 slides out of the screen, item8 slides out from below and calls getview again. However, the converview is no longer empty, but the item1, in this case, we only need to re-Modify the item1 data, instead of creating a new view. This saves memory. Therefore, we obtain the second public View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {convertView = mInflater. inflate (R. layout. item, parent, false);} (TextView) convertView. findViewById (R. id. text )). setText (DATA [position]); (ImageView) convertView. findViewById (R. id. icon )). setImageBitmap (position & 1) = 1? MIcon1: mIcon2); return convertView;} In this method, we use converview to find the text and ico to be modified, modify the data, and return. Some people will say that the above problem has not been solved for reuse. Is there a third way? Let's take a look at how the third method recommended by google is implemented. Static class ViewHolder {TextView text; ImageView icon;} an internal static class is defined here. public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = mInflater. inflate (R. layout. list_item_icon_text, parent, false); holder = new ViewHolder (); holder. text = (TextView) convertView. findViewById (R. id. text); holder. icon = (ImageView) convertView. findVie WById (R. id. icon); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. text. setText (DATA [position]); holder. icon. setImageBitmap (position & 1) = 1? MIcon1: mIcon2); return convertView;} then, use convertView. setTag (holder) to put the textview and ImageView found through findViewById into the converivew object. In this way, when converview is reused, you do not have to use findViewById to find it again, saving the overhead.