How convertView and ViewHolder work in ListView .

LsitView and Adapter
reference: http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html
Working principle:
1.ListView requires the adapter to give me a view (getView) for each item in the List
2. A new view is returned and displayed


What if we have hundreds of millions of items to display? Create a new view for each item? NO! This is impossible~~~ Android actually caches the view for you


. There is a component in Android called Recycler. The following figure is how it works:


1. If you have 1 billion items, of which Only visible items exist in memory, others are in Recycler
2.ListView first requests a type1 view (getView), and then requests other visible items.
3. When item1 scrolls off the screen and a new item comes up from the screen, ListView requests a type1 view again . convertView is not null at this time, its value is item1. You only need to set the new data to return to convertView, without having to recreate a view. In this way, the use of convertView directly reduces the creation of unnecessary views




! ! ! ! ! ! A faster way is to define a ViewHolder, set the tag of convertView to ViewHolder, if it is not empty, reuse


ViewHolder just to encapsulate those views that need to be cached, and setTag of convertView is to cache these for the next call
When the layout in your listview is diversified, the role of the viewholder is more obvious. Of course, a single-mode layout can also be optimized for performance, just not intuitive. If you have two modes of layout, when recycling occurs, you will use setTag to record which two modes are. These two modes will be encapsulated in the viewholder for saving for your next use. VH is a static class that has nothing to do with caching

01.<SPAN style="FONT-FAMILY: Microsoft YaHei; COLOR: #3366ff; FONT-SIZE: 18px"><STRONG>public class MultipleItemsList extends ListActivity { 
02.   
03. private MyCustomAdapter mAdapter; 
04.   
05. @Override 
06. public void onCreate(Bundle savedInstanceState) { 
07. super. onCreate(savedInstanceState); 
08. mAdapter = new MyCustomAdapter(); 
09. for (int i = 0; i < 50; i++ ) { 
10. mAdapter. addItem("item " + i); 
11. } 
12. 
13.    } 
14.   
15.    private class MyCustomAdapter extends BaseAdapter { 
16.   
17.        private ArrayList mData = new ArrayList(); 
18.        private LayoutInflater mInflater; 
19.   
20.        public MyCustomAdapter() { 
21.            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
22.        } 
23.   
24.        public void addItem(final String item) { 
25.            mData.add(item); 
26.            notifyDataSetChanged(); 
27.        } 
28.   
29.        @Override 
30.        public int getCount() { 
31.            return mData.size(); 
32.        } 
33.   
34.        @Override 
35.        public String getItem(int position) { 
36.            return mData.get(position); 
37.        } 
38.   
39.        @Override 
40.        public long getItemId(int position) { 
41.            return position; 
42.        } 
43.   
44.        @Override 
45.        public View getView(int position, View convertView, ViewGroup parent) { 
46.            System.out.println("getView " + position + " " + convertView); 
47.            ViewHolder holder = null; 
48.            if (convertView == null) { 
49.                convertView = mInflater.inflate(R.layout.item1, null); 
50.                holder = new ViewHolder(); 
51.                holder.textView = (TextView)convertView.findViewById(R.id.text); 
52.                convertView.setTag(holder); 
53.            } else { 
54.                holder = (ViewHolder)convertView.getTag(); 
55.            } 
56.            holder.textView.setText(mData.get(position)); 
57.            return convertView; 
58.        } 
59.   
60.    } 
61.   
62.    public static class ViewHolder { 
63.        public TextView textView; 
64.    } 
65.}</STRONG></SPAN> 


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326986926&siteId=291194637