Basic getView Writing Method Java code Public View getView (int position, View convertView, ViewGroup parent ){ View view = new View (); // Locate the layout through inflate, and set items for each display, such as findViewById. Return view; } In the process of sliding the ListView, it is easy to find that a new View object will be generated every time the getView is executed, which will consume a lot in the past. Especially when Bitmap and so on exist in items, it may even cause OOM errors and cause program crash. When reading the parameters provided by getView, you may have noticed that the View convertView parameter is actually the most critical part. When the ListView slides, an item will be slid out of the screen instead of being used. At this time, Android recycles the view of this item. This view is the convertView here. In the above practice, when item1 is removed from the screen, we will create a new View for the newly displayed item_new. If this convertView is used, we can actually reuse it. This saves a lot of new View overhead. The following is how convertView is used. Java code Public View getView (int position, View convertView, ViewGroup parent ){ View view = null; If (convertView! = Null ){ View = convertView; // Reuse the recycled view. You only need to modify the content filling directly. } Else { View = new Xxx (...); // Create a view without reuse. } Return view; } This avoids the issue of repeatedly creating a large number of views. However, there are still defects in the above. When the items in our ListView are filled in multiple forms, for example, some items in Weibo contain images, some items contain videos, which are inevitable. We need two item la S. In this case, if you only determine whether convert exists, the recycled view will not meet your current layout, and the conversion will fail and exit. The other two methods in the Adapter are described as follows: Public int getItemViewType (int position ){} Public int getViewTypeCount (){} From the method name, we can clearly understand the role of these two methods. The following is a demo code. Java code Class MyAdapter extends BaseAdapter { Context mContext; LinearLayout linearLayout = null; LayoutInflater inflater; TextView tex; Final int VIEW_TYPE = 2; Final int TYPE_1 = 0; Final int TYPE_2 = 1; Public MyAdapter (Context context ){ MContext = context; Inflater = LayoutInflater. from (mContext ); } @ Override Public int getCount (){ Return listString. size (); } // This method is called for each convert view to obtain the current view style. @ Override Public int getItemViewType (int position ){ Int p = position % 6; If (p = 0) Return TYPE_1; Else if (p <3) Return TYPE_2; Else Return TYPE_1; } @ Override Public int getViewTypeCount (){ Return 2; } @ Override Public Object getItem (int arg0 ){ Return listString. get (arg0 ); } @ Override Public long getItemId (int position ){ Return position; } @ Override Public View getView (int position, View convertView, ViewGroup parent ){ ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; Int type = getItemViewType (position ); // No convertView. You need to generate new controls. If (convertView = null) { // Determine the new layout based on the current style Switch (type) { Case TYPE_1: ConvertView = inflater. inflate (R. layout. listitem1, parent, false ); Holder1 = new viewHolder1 (); Holder1.textView = (TextView) convertView. findViewById (R. id. textview1 ); Holder1.checkBox = (CheckBox) convertView. findViewById (R. id. checkbox ); ConvertView. setTag (holder1 ); Break; Case TYPE_2: ConvertView = inflater. inflate (R. layout. listitem2, parent, false ); Holder2 = new viewHolder2 (); Holder2.textView = (TextView) convertView. findViewById (R. id. textview2 ); Holder2.imageView = (ImageView) convertView. findViewById (R. id. imageview ); ConvertView. setTag (holder2 ); Break; } } Else { // ConvertView is available and layout is not needed by style Switch (type) { Case TYPE_1: Holder1 = (viewHolder1) convertView. getTag (); Break; Case TYPE_2: Holder2 = (viewHolder2) convertView. getTag (); Break; } // Set Resources Switch (type) { Case TYPE_1: Holder1.textView. setText (Integer. toString (position )); Holder1.checkBox. setChecked (true ); Break; Case TYPE_2: Holder2.textView. setText (Integer. toString (position )); Holder2.imageView. setBackgroundResource (R. drawable. icon ); Break; } } Return convertView; } } // Control resources of various la s Class viewHolder1 { CheckBox checkBox; TextView textView; } Class viewHolder2 { ImageView imageView; TextView textView; } A viewHolder is used for each View to control its internal sub-item. Another note is that using the setTag and getTag methods to bind holder to the view is also a skill. The above is basically the main content. Below we will add some Tips in the actual operation. * If convertView uses Type to distinguish between complex and non-complex data, but rarely in different situations, you can use Instanceof is used to determine whether a View can be created if it cannot be converted. However, this practice is not standardized, so we recommend the above practice. * The second one is about the fact that ListView can directly set BackgroundColor for a solid color item background, rather than using images. In fact, this part can be greatly improved. Set the RGB color for any solid background instead of using an image as the background. |