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:
- The ListView requires adapter "Give me a View" (GetView) for each item in the list.
- 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:
- If you have 1 billion items (item), only the visible items exist in memory, others are in recycler.
- The ListView first requests a type1 view (GetView) and then requests other visible items. Convertview is empty (null) in GetView.
- 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
- <span style="Font-family:courier new;font-size:12px;" > PublicView getView (int position, view Convertview, ViewGroup parent)
- {
- //A Viewholder keeps references to children views to avoid unneccessary calls
- //To Findviewbyid () on each row.
- Viewholder Holder;
- //When convertview are NOT NULL, we can reuse it directly, there is no need
- //To reinflate it. We only inflate a new View when the Convertview supplied
- //By the ListView is null.
- if (Convertview = = )
- {
- Convertview = Minflater.inflate (R.layout.list_item_icon_text, );
- LOG.V ("tag", "Positon" +position+"Convertview is null," +"NEW:" +convertview);
- //Creates a viewholder and store references to the other children views
- //We want to bind data.
- Holder = new Viewholder ();
- Holder.text = (TextView) Convertview.findviewbyid (R.id.text);
- Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon);
- Convertview.settag (holder);
- }
- Else
- {
- //Get the Viewholder back-to-get fast access to the TextView
- //And the ImageView.
- Holder = (viewholder) convertview.gettag ();
- LOG.V ("tag", "Positon" +position+"Convertview is not NULL," +convertview);
- }
- //Bind The data efficiently with the holder.
- Holder.text.setText (Data[position]);
- Holder.icon.setImageBitmap ((Position & 1) = = 1? micon1:micon2);
- return convertview;
- }
- Static class Viewholder
- {
- TextView text;
- ImageView icon;
- } </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