Lsitview and Adapter
Reference: http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html working principle: 1.ListView for each item in the list, Ask adapter to give me a view (GetView) 2. A new view is returned and displayed if we have billions of item to show what to do? Create a new view for each project? No! It's impossible to ~~~android actually cache the view Android has a component called recycler (Recurrent circulator), which is how it works: 1. If you have 1 billion items (item), where only visible items exist in memory, Other in Recycler 2.ListView first requests a type1 view (GetView) and then requests other visible items. Convertview NULL in GetView 3. When item1 rolls out of the screen and a new item comes up from the screen lot, the ListView requests a type1 view. Convertview is not a null value at this time, its value is item1. You only need to set new data back Convertview, you don't have to recreate a view. This reduces the creation of very unnecessary view by directly using CONVERTVIEW!!!!!! The quicker way is to define a viewholder, set Convertview's tag to Viewholder, and not empty is re-use
Viewholder Only the view packages that need to be cached, Convertview's settag is to cache them for the next call .
when you have a variety of layouts in your ListView, the role of Viewholder is more obvious. Of course, the single-mode layout has the same performance-optimized effect as it is just not intuitive. If you have 2 modes of layout when recycling occurs you will use Settag separately to record which two of these modes will be encapsulated in the Viewholder to be saved conveniently for your next use. VH is a static class that is not cache-independent
[Java]View Plaincopyprint?
- <span style="Font-family:microsoft yahei;font-size:18px;color: #3366ff;" ><strong>public class Multipleitemslist extends Listactivity {
- private Mycustomadapter Madapter;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Madapter = new Mycustomadapter ();
- For (int i = 0; i < i++) {
- Madapter.additem ("item" + i);
- }
- Setlistadapter (Madapter);
- }
- private class Mycustomadapter extends Baseadapter {
- private ArrayList mdata = new ArrayList ();
- private Layoutinflater Minflater;
- Public Mycustomadapter () {
- Minflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
- }
- public void AddItem (final String Item) {
- Mdata.add (item);
- Notifydatasetchanged ();
- }
- @Override
- public int GetCount () {
- return mdata.size ();
- }
- @Override
- Public String GetItem (int position) {
- return Mdata.get (position);
- }
- @Override
- public long getitemid (int position) {
- return position;
- }
- @Override
- Public View GetView (int position, View Convertview, ViewGroup parent) {
- System.out.println ("GetView" + position + "" + Convertview);
- Viewholder holder = null;
- if (Convertview = = null) {
- Convertview = Minflater.inflate (r.layout.item1, null);
- Holder = new Viewholder ();
- Holder.textview = (TextView) Convertview.findviewbyid (R.id.text);
- Convertview.settag (holder);
- } Else {
- Holder = (viewholder) convertview.gettag ();
- }
- Holder.textView.setText (Mdata.get (position));
- return convertview;
- }
- }
- public static class Viewholder {
- Public TextView TextView;
- }
- }</strong></span>
Resources:
Http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html
Http://www.eoeandroid.com/thread-72369-1-1.html
Http://www.cnblogs.com/felix-hua/archive/2012/01/06/2314436.html
http://blog.csdn.net/jacman/article/details/7087995
Http://fatkun.com/2012/01/android-viewholder.html
How Convertview and Viewholder work in the ListView