Android's Custom implementation Baseadapter (Universal adapter one) _android

Source: Internet
Author: User
Tags static class

With the previous optimized layout, we go on to build a generic adapter, so what can the universal adapter do? It's easy to reduce the writing of our code, which starts with the code below.

Myadapter.java

public class Myadapter extends Baseadapter {private list<student> data;
 Public Myadapter (list<student> data) {this.data = data;
 @Override public int GetCount () {return data = null 0:data.size ();
 @Override public Object getitem (int position) {return data.get (position);
 @Override public long getitemid (int position) {return position; /** * * @param position * @param convertview * @param parent * @return/@Override public View GetView (i
  NT position, View Convertview, ViewGroup parent) {Viewholder holder = null; if (Convertview = = null) {//Parse layout Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.list_item,nu
   ll);
   Create Viewholder hold class holder = new Viewholder ();
   Saves the object of each control to the owning class Holder.tvname = (TextView) Convertview.findviewbyid (R.ID.MTV1);
   Holder.tvsex = (TextView) Convertview.findviewbyid (R.ID.MTV2);
  Set this holding class object Convertview.settag (Holder) in each Convertview object; I'll get it every time I need to use it.A holding class Holder = (Viewholder) convertview.gettag ();
  You can then use the controls in this class directly to manipulate the controls without having to repeat the Findviewbyid Holder.tvName.setText (data.get (position). GetName ());
  Holder.tvSex.setText (Data.get (position). Getsex ());
 return convertview;
  /** * Through this class to save all current control IDs/static class viewholder{TextView tvname;
 TextView Tvsex;
 }
}

In the code above, let's take a look at the format or form of the code that is used repeatedly, first it's easy to see, public int getcount (), Public long getitemid (int position), and public long Getitemid (int position) These three methods are not to be implemented every time, so we can first extract the code, put it into the mybaseadapter, because each of our important part is to implement the GetView method, So this method we do not need to write here, directly set the Mybaseadapter to abstract class on it, it is necessary to implement GetView class to inherit him, so Myadapter can inherit mybaseadapter and then implement the GetView method can be

Mybaseadapter.java

Public abstract class Mybaseadapter extends Baseadapter {
 protected list<student> data;
 Public Mybaseadapter (list<student> data) {
  this.data = data;
 }
 @Override public
 int GetCount () {return
  data = null 0:data.size ();
 }

 @Override public
 Object getitem (int position) {return
  data.get (position);
 }

 @Override public
 long getitemid (int position) {return
  position;
 }
}

Myadapter.java

 public class Myadapter extends Mybaseadapter {public myadapter (list<student> dat
  A) {super (data);
 This.data = data;
  @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder holder = null;
   if (Convertview = = null) {Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.list_item,null);
   Holder = new Viewholder ();
   Holder.tvname = (TextView) Convertview.findviewbyid (R.ID.MTV1);
   Holder.tvsex = (TextView) Convertview.findviewbyid (R.ID.MTV2);
  Convertview.settag (holder);
  } holder = (Viewholder) convertview.gettag ();
  Holder.tvName.setText (Data.get (position). GetName ());
  Holder.tvSex.setText (Data.get (position). Getsex ());
 return convertview;
  Static class viewholder{TextView tvname;
 TextView Tvsex; }
}

This way, each customization only need to inherit mybaseadapter, but still that sentence, there is no optimal, only better, so we have to continue to optimize, and then encapsulation, then we see from the above GetView method, what other code we often reuse the code? In fact, you will find that each time we need to manipulate the same code:

Viewholder holder = null;
  if (Convertview = = null) {
   Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.list_item, NULL);
   Holder = new Viewholder ();
   Holder.tvname = (TextView) Convertview.findviewbyid (R.ID.MTV1);
   Holder.tvsex = (TextView) Convertview.findviewbyid (R.ID.MTV2);
   Convertview.settag (holder);
  }
  Holder = (viewholder) convertview.gettag ();
  Holder.tvName.setText (Data.get (position). GetName ());
  Holder.tvSex.setText (Data.get (position). Getsex ());
  return convertview;
 }

We can first understand this code again, first of all we have to create a holder holder object, set to the corresponding Converview Settag, and then, each time to get Hoder object, the control in the Hoder object operation, For the above code, we can simply simplify him to the following steps

1.ViewHolder holder = Get Holder//each time you get the corresponding holder object
2.TextView TV = Holder.getview ()//Get the ID of each control
3. Tv.settext ()//manipulating controls
4. Return view//back View

Let's start by writing a generic viewholder generic class that optimizes our implementation with the following code:

Viewholder.java

public class Viewholder {//clicked current position private int position; Using a map collection to hold the ID of each control, this sparsearray is a more efficient one/set than the map using Android, but the limitation is that key is only int, so when the key value pair involves the type of key being int,

 This set of private sparsearray<view> array can be given priority;

 The reusable layout private View Convertview;

 Contextual private context;
 The parsed layout resource ID private int layout; Public Viewholder () {}///With three constructed methods, which will construct the method private, prevent the outside to create, through its own static method to create the object can be private Viewholder (viewgroup parent,int position
  , int layout) {this.position = position;
  This.context = Parent.getcontext ();
  Each time the object is created, the layout is parsed Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (Layout,null);
  The object is then saved to the convertview corresponding Settag to facilitate the acquisition of Convertview.settag (this) each time;
 Array = new sparsearray<> (); //Through this method, you can create the Viewholder object public static Viewholder Getholder (View Convertview, viewgroup parent, int position,int Lay 
  Out) {//each time the Converview is null, if empty, return directly to the created object if (Convertview = null) {returns new Viewholder (Parent,position,layout); }else{//Not emptyThe case, just like our code above, each time through the reuse control to get the corresponding Viewholder object Viewholder holder = (viewholder) convertview.gettag ();
   Here must update the subscript position, although the object is the same, but each time we have to update the existing location, holder.position = position;
  Returns the holder object that has already been created return holder;  }/** * This method is to get the corresponding control through the control ID/public <t extends view> T getview (int viewid) {//Every time through the Viewid key to the corresponding control View
  View = Array.get (Viewid);
   If NULL, indicates that the control is not yet saved in the collection if (view = = null) {//First go through Converview to get control id view = Convertview.findviewbyid (Viewid);
  Save to the collection for the next direct fetch of Array.put (Viewid,view);
 //Return to the view's subclass control, the convenience of the generics is that you do not need to force the conversion of Returns (T) view;
 //Get Converview Layout public View Getconvertview () {return convertview;

 }

Through the above code we have already encapsulated a common Viewholder class, the following our Myadapter.java can be more simple to write the code:

public class Myadapter extends Mybaseadapter {public
 myadapter (list<student> data) {
  super (data);
 }
 @Override public
 View getview (int position, View Convertview, ViewGroup parent) {
  Viewholder holder = viewho Lder.getholder (Convertview,parent,position, r.layout.list_item);
  TextView tvname = Holder.getview (R.ID.MTV1);
  TextView tvsex = Holder.getview (R.ID.MTV2);
  Tvname.settext (Data.get (position). GetName ());
  Tvsex.settext (Data.get (position). Getsex ());
  return Holder.getconvertview ();
 }


Well, the above code is not simpler, in fact, we just encapsulate the Viewholder class Oh, there are more general waiting for us to encapsulate it, the next time we need to encapsulate is how to GetView the code in the package again, has reached a more excellent.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.