--base-adapter-helper parsing for Universal adapter in Android

Source: Internet
Author: User

in the development of Android, we often use the ListView, GridView, each time the code needs to write their corresponding adapter, write a lot of feeling very irritable, because the basic programming ideas are the same, but each time to repeat to write, So can we abstract them into a generic template, so that you do not have to repeat the same code every time, the direct re-use, this is not better, we will introduce an open source project Base-adapter-helper.

The traditional adapter coding idea, mainly looks at the GetView method in the adapter.

Public View getView (int pos, View Convertview, ViewGroup parent) {     Viewholder holder;     if (Convertview = = null) {         Convertview = minflater.inflate (R.layout.list_item, null);         Holder = new Viewholder ();          Holder.text = (TextView) Convertview.findviewbyid (R.id.text));         Holder.icon = (ImageView) convertview.findviewbutid (R.id.icon));         Convertview.settag (holder);     } else {         holder = (viewholder) convertview.gettag ();     }      Holder.text.setText (Data[pos]);     Holder.icon.setImageBitmap (pos & 1) = = 1 micon1:micon2);     return convertview; }static class Viewholder {     TextView text;     ImageView icon; }

it uses a viewholder to cache the view in the corresponding item and reuse the removed item, which corresponds to Convertview. This attention is paid to conserve resources and improve efficiency. Everyone should be familiar with this kind of writing.
Let's look at how the Base-adapter-helper is encapsulated in an abstract way. First, let's look at its class inheritance diagram.

GitHub Link: base-adapter-helper


You can see that basequickadapter inherits from Baseadapter, and we also focus on its getview function.

@Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {    if (getitemviewtype (position) = = 0) {        Final H helper = getadapterhelper (position, convertview, parent);        T item = getItem (position);        Helper.setassociatedobject (item);        Convert (helper, item);        return Helper.getview ();    }    Return Createindeterminateprogressview (Convertview, parent);} Private View Createindeterminateprogressview (view Convertview, ViewGroup parent) {    if (Convertview = = null) {        Framelayout container = new Framelayout (context);        Container.setforegroundgravity (gravity.center);        ProgressBar progress = new ProgressBar (context);        Container.addview (progress);        Convertview = container;    }    return Convertview;}

Below we analyze and analyze GetView code:

    • The 3rd line of code is to get the item type of the postion, which defines two types of item, one that we need to display for the view item, and one that is loaded at the bottom of the view item. When item type 0 is the item that needs to be displayed, when item type is 1 is the item loaded for the bottom, the Createindeterminateprogressview of the 11th line above is to create the loaded item at the bottom, Can see it is a ProgressBar. You can also show or hide this item by Showindeterminateprogress (Boolean).

    • The 4th line of code Getadapterhelper gets a Baseadapterhelper object, and we can see that in the Basequickadapter class, Getadapterhelper is an abstract function, So let's take a look at Basequickadapter's subclass Quickadapter, which implements the Getadapterhelper method in this class, and the essence of execution is the static Get method of Baseadapterhelper. Another thing to mention is that the constructor for the Quickadapter class has a parameter of Layoutresid, which is the layout file that we want to display the item on.
Static Baseadapterhelper Get (context context, View Convertview, viewgroup parent, int layoutid, int position) {    if (CO Nvertview = = null) {        return new Baseadapterhelper (context, parent, layoutid, position);    }    Retrieve the existing helper and update its position    baseadapterhelper Existinghelper = (baseadapterhelper) Convert View.gettag ();    Existinghelper.position = position;    return existinghelper;}

As you can see from the above, it also reuses convertview, and when Convertview is null, we need to create a baseadapterhelper, which is the location position and the LayoutID layout file to display, This position is the position parameter in the previous GetView, LayoutID is the layout file of the item to create the Quickadapter passed in parameter.
Protected Baseadapterhelper (Context context, viewgroup parent, int layoutid, int position) {    This.context = context;< C1/>this.position = position;    This.views = new sparsearray<view> ();    Convertview = Layoutinflater.from (context)//            . Inflate (LayoutID, parent, false);    Convertview.settag (this);}
in the Baseadapterhelper constructor, define a view, which is actually the traditional adapter inside the Viewholder used to store the item inside the various views. Convertview is the view of the item that we want to display, and then the Baseadapterhelper object itself is associated to convertview through the Settag function. So we know that each item object is associated with a Baseadapterhelper object.
when Convertview is not NULL, we can directly reuse the Convertview, first extracting its associated Baseadapterhelper object from Convertview, This allows the Convertview corresponding Baseadapterhelper object to be reused.
    • Then analyze the top code, the 5th line of code GetItem to get the data that needs to be displayed, here is to explain that we should put the incoming data in a list, we can look at the Quickadapter constructor
Public Quickadapter (context context, int layoutresid, list<t> data) {    super (context, layoutresid, data);}
you can see the layoutresid that we passed in the item layout and the data to be displayed Data,data is the list type.
    • The 6th line of code is to associate the data item we display with the Baseadapterhelper object.
    • The 7th line of code, the CONVERT function, is also an abstract function that needs to be implemented to specifically set the contents of the corresponding item.
    • The 8th line of code is when the Baseadapterhelper object is set to the contents of item, and then you can get the item's view object to return.
Let's take a detailed picture to illustrate. If we're going to show a ListView.
When initially displayed, the Baseadapterhelper is created because the corresponding Convertview is null, and the core code is as follows:
Protected Baseadapterhelper (Context context, viewgroup parent, int layoutid, int position) {    This.context = context;< C2/>this.position = position;    This.views = new sparsearray<view> ();    Convertview = Layoutinflater.from (context)//            . Inflate (LayoutID, parent, false);    Convertview.settag (this);}

Because the Baseadapterhelper object contains the corresponding item corresponding to the Convertview and corresponding position andConvertview A collection of sub-view inside, so we can directly through the Baseadapterhelper object to complete the operation .

when the list is sliding up, the first item moves out of the list, and the bottom needs to display an item again, and this time getview inside the Convertview is the first one to move out of view, we can directly reuse it to display the next item, The core code is:
Baseadapterhelper Existinghelper = (baseadapterhelper) convertview.gettag (); existinghelper.position = position;
He got it straight.The Baseadapterhelper object is then reset to its corresponding position postion because the Baseadapterhelper object references the Convertview of Reuse , This allows you to use the view item directly.
Here 's a simple example:
public class Mainactivity extends Appcompatactivity {    private listview listview;    Private list<string> data = new arraylist<> ();    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Data.add ("Text1");        Data.add ("Text2");        Data.add ("Text3");        Data.add ("Text4");        Data.add ("Text5");        Data.add ("Text6");        ListView = (ListView) Findviewbyid (R.id.listview);        Quickadapter adapter = new Quickadapter<string> (this, R.layout.item, data) {            @Override            protected void Convert (Baseadapterhelper helper, String item) {                Helper.settext (R.id.textview, item);            }        };        Listview.setadapter (adapter);    }    }

That means we just need to define a quickadapter rewrite the Convert method, and in the Convert method we use theThe Baseadapterhelper object finishes setting the data content item to the right.


--base-adapter-helper parsing for Universal adapter in Android

Related Article

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.