How Convertview and Viewholder work in the ListView

Source: Internet
Author: User

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?
  1. <span style="Font-family:microsoft yahei;font-size:18px;color: #3366ff;" ><strong>public class Multipleitemslist extends Listactivity {
  2. private Mycustomadapter Madapter;
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Madapter = new Mycustomadapter ();
  7. For (int i = 0; i < i++) {
  8. Madapter.additem ("item" + i);
  9. }
  10. Setlistadapter (Madapter);
  11. }
  12. private class Mycustomadapter extends Baseadapter {
  13. private ArrayList mdata = new ArrayList ();
  14. private Layoutinflater Minflater;
  15. Public Mycustomadapter () {
  16. Minflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
  17. }
  18. public void AddItem (final String Item) {
  19. Mdata.add (item);
  20. Notifydatasetchanged ();
  21. }
  22. @Override
  23. public int GetCount () {
  24. return mdata.size ();
  25. }
  26. @Override
  27. Public String GetItem (int position) {
  28. return Mdata.get (position);
  29. }
  30. @Override
  31. public long getitemid (int position) {
  32. return position;
  33. }
  34. @Override
  35. Public View GetView (int position, View Convertview, ViewGroup parent) {
  36. System.out.println ("GetView" + position + "" + Convertview);
  37. Viewholder holder = null;
  38. if (Convertview = = null) {
  39. Convertview = Minflater.inflate (r.layout.item1, null);
  40. Holder = new Viewholder ();
  41. Holder.textview = (TextView) Convertview.findviewbyid (R.id.text);
  42. Convertview.settag (holder);
  43. } Else {
  44. Holder = (viewholder) convertview.gettag ();
  45. }
  46. Holder.textView.setText (Mdata.get (position));
  47. return convertview;
  48. }
  49. }
  50. public static class Viewholder {
  51. Public TextView TextView;
  52. }
  53. }</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

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.