Go Understanding of the Android adapter and GetView () methods

Source: Internet
Author: User

Android Adapter Basic Understanding:

My understanding is:

1. A class with lots of Getter (GetView (), GetCount () .... These methods)

2. How many get methods are there? What's all this?

These getters are specific and you can make a copy of them, the whole method is as follows

In general, we only use the four methods of carbon getcount (), GetView (), Getitemid (), GetItem ().

3. Who are those calls?

These getters are self-invoking by the Android system (specifically how to call, as a novice like me to do a little understanding on the good)

4. Why are these get methods to be replicated?

The replication of these getters is to return the results to the Android system that you want to give

5. What can the Android system get with these getters?

Get some information on this view itself

Eg: take the adapter in the ListView as an example, where

public int GetCount ()

Returns the number of child view to display in the ListView, which is the total number of item

Public Object getItem (int position)

Returns a child view, which is a sub-entry in the ListView

public long getitemid (int position)

Returns the ID of an item, which is determined by the parameter position

Public View GetView (int position, view Convertview, viewgroupparent)

After doing a detailed

....

6. What do the Android systems do to get these?

Get this information to draw out the view and how to draw it ...

Understanding of view GetView (int position, view Convertview, ViewGroup parent):

Referenced from: http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html There is also a case to help understand

First, the understanding of the principle

Working principle:

    1. The ListView requires adapter "Give me a View" (GetView) for each item in the list.
    2. A new view is returned and displayed

What if we have billions of items to show? Create a new view for each project? No!, this is impossible!

Android actually caches the view for you.

There's a widget called Recycler in Android, and that's how he works:

    1. If you have 1 billion items (item), only the visible items exist in memory, others are in recycler.
    2. The ListView first requests a type1 view (GetView) and then requests other visible items. Convertview is empty (null) in GetView.
    3. When item1 rolls out of the screen and a new item comes up from the low end of the screen, the ListView requests a type1 view. Convertview is not a null value at this time, its value is item1. You just need to set up new data and return to Convertview, without having to recreate a view.

Second, the Code understanding

Google's official Demo code:

[Java]View PlainCopy
  1. <span style="Font-family:courier new;font-size:12px;" > PublicView getView (int position, view Convertview, ViewGroup parent)
  2. {
  3. //A Viewholder keeps references to children views to avoid unneccessary calls
  4. //To Findviewbyid () on each row.
  5. Viewholder Holder;
  6. //When convertview are NOT NULL, we can reuse it directly, there is no need
  7. //To reinflate it. We only inflate a new View when the Convertview supplied
  8. //By the ListView is null.
  9. if (Convertview = = )
  10. {
  11. Convertview = Minflater.inflate (R.layout.list_item_icon_text, );
  12. LOG.V ("tag", "Positon" +position+"Convertview is null," +"NEW:" +convertview);
  13. //Creates a viewholder and store references to the other children views
  14. //We want to bind data.
  15. Holder = new Viewholder ();
  16. Holder.text = (TextView) Convertview.findviewbyid (R.id.text);
  17. Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon);
  18. Convertview.settag (holder);
  19. }
  20. Else
  21. {
  22. //Get the Viewholder back-to-get fast access to the TextView
  23. //And the ImageView.
  24. Holder = (viewholder) convertview.gettag ();
  25. LOG.V ("tag", "Positon" +position+"Convertview is not NULL," +convertview);
  26. }
  27. //Bind The data efficiently with the holder.
  28. Holder.text.setText (Data[position]);
  29. Holder.icon.setImageBitmap ((Position & 1) = = 1? micon1:micon2);
  30. return convertview;
  31. }
  32. Static class Viewholder
  33. {
  34. TextView text;
  35. ImageView icon;
  36. } </span>

The next two paragraphs are quoted from: http://blog.csdn.net/pkxiuluo01/article/details/7380974

Understanding of Convertview Parameters:

Each time an item is displayed, the GetView method is called once but the Covertview is empty each time it is called (because there is no old view) when the display is finished. If the screen moves and causes some item (or view) to run outside the screen, if there is a new item that needs to be generated, then the Convertview parameter in the GetView method that is called when the item is displayed is not NULL. Instead of the view (old view) that moved out of the screen, all we have to do is populate the recycled view with the item that needs to be displayed, and finally note that Convertview is not just the initial display of the item, There are those item that have already started to move into the screen but have not yet been recovered by the view.

Understanding of Settag () and Gettag ():

The Settag and Gettag methods of view are very simple, when actually writing code, a view is not just to display some strings, pictures, and sometimes we need them to carry some other data so that we can recognize the view or other operations. So the Android designers created the Settag (Object) method to hold some data and view bindings, and we can understand that this is the label of the view as well as the view as a container to hold some data. We can also use the Gettag () method to extract the data.

Here Settag and Gettag people should have understood. Returning to the above topic, we bind the data we want to display on Convertview by Convertview's Settag method and Gettag method. If Convertview is the first to show we create a new holder object with which to bind, and return it at the end through return Convertview, to display, if Convertview is recycled then we don't have to create a new holder object, Just take the original bound holder out and add the new data to the line.

I understand the process of reading the article:

Understanding adapter at the source, and a case

http://blog.csdn.net/chunqiuwei/article/details/39934169

Help understand

http://blog.csdn.net/primer_programer/article/details/23064767

Go Understanding of the Android adapter and GetView () methods

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.