1, a brief introduction in the development of Android certainly can not avoid dealing with adapter, is generally inherited in Baseadapter rewrite inside a few methods, and then a ListView corresponding to a adapter, that natural project appears a lot of adapter, Given that there is a lot of duplication of code in this adapter, is there a way to simplify it? The answer is yes. 2, the general wording only paste adapter part, layout is not affixed, because is not the focus.
public class Simpleadapter extends Baseadapter {public static class Viewholder {public TextView tvname; } private list<student> studentlist; Private Layoutinflater Inflater; Public Simpleadapter (context context, list<student> students) {inflater = Layoutinflater.from (context); This.studentlist = students; } @Override public int getcount () {return studentlist.size (); } @Override public Student getItem (int position) {return studentlist.get (position); } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder; View view = Convertview; if (view = = null) {view = Inflater.inflate (Android. R.layout.simple_list_item_1, parent, false); Holder = new Viewholder (); Holder.tvname = (TextView) View.findviewbyid (Android. R.ID.TEXT1); View.settag (holder); } else {holder = (Viewholder) view.gettag (); } Student Student = GetItem (position); Holder.tvName.setText (STUDENT.NAEM); return view; }}
Call in activity
Setlistadapter (New Simpleadapter (GetContext (), datas));
This code should be familiar, when inheriting baseadapter, you must rewrite the 4 methods of GetCount, GetItem, Getitemid, GetView, and then write a constructor method to pass in the entity object. It can be said that you write another baseadapter is the same writing, carefully you should find that only the construction method entity object and GetView in the method body business logic is different, then is not the entity object can be rewritten into generic, GetView method body business logic abstraction out, outside the implementation? That's a must, and then we'll step through it to improve the code of this deadly repetition. 3, the entity object is changed to the generic new one adapter named Cygadapter, the changed code added comments.
public class Cygadapter<t> extends Baseadapter {//change public static class Viewholder {public TextView Tvna Me } private list<t> studentlist;//change private layoutinflater inflater; Public Cygadapter (context context, list<t> students) {inflater = Layoutinflater.from (context); This.studentlist = students; } @Override public int getcount () {return studentlist.size (); } @Override public T getItem (int position) {//Change return studentlist.get (position); } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder; View view = Convertview; if (view = = null) {view = Inflater.inflate (Android. R.layout.simple_list_item_1, parent, false); Holder = new Viewholder (); Holder.tvname = (TextView) View.findviewbyid (Android. R.ID.TEXT1); View.settag (holder); } else {holder = (Viewholder) view.gettag (); } t student = GetItem (position);//Change Holder.tvName.setText (STUDENT.NAEM);//There is a problem here, this T generic class does not have a name variable at all, how to do? return view; }}The above code has successfully changed the entity object to generic, but the business logic in GetView the problem in the above code, cannot fetch the name variable, do you annotate the student entity class? For a student class annotation is the line of the drip, add a @name annotation, but another school school class, class class is not to add n different named annotations? In this way the annotated scheme must not go down. Then we can try to abstract getview business logic out of the external implementation, and now we need an abstract method. 4. Abstract GetView business logic
We re-cygadapter the new one, creating an abstract method Onbinddata (Viewholder viewholder, Final T item, final int position);
@Override public View getView (int position, view Convertview, ViewGroup parent) { Viewholder holder; View view = Convertview; if (view = = null) { view = Inflater.inflate (Android. R.layout.simple_list_item_1, parent, false); Holder = new Viewholder (); Holder.tvname = (TextView) View.findviewbyid (Android. R.ID.TEXT1); View.settag (holder); } else { holder = (viewholder) view.gettag (); } T student = GetItem (position); Holder.tvName.setText (STUDENT.NAEM);//What about the problem here? Onbinddata (holder,student,position);//abstract The above Holder.tvName.setText (STUDENT.NAEM) to the external implementation of return view; } New abstract method public abstract void Onbinddata (Viewholder viewholder, Final T item, final int position);
Then look at the usage in the activity
Setlistadapter (New com.mylhyl.cygadapter.sample.blog.cygadapter<student> (GetContext (), datas) { @ Override public void Onbinddata (Viewholder viewholder, Student item, int position) { ViewHolder.tvName.setText (ITEM.NAEM); } });
Try a different school object.
arraylist<school> datas = new ArrayList (); Datas.add (New School ("Zhuhai Beijing Normal University")); Datas.add (New School ("Changsha Hunan University"); Setlistadapter (New com.mylhyl.cygadapter.sample.blog.cygadapter<school> (GetContext (), datas) { @ Override public void Onbinddata (Viewholder viewholder, School item, int position) { ViewHolder.tvName.setText (item.address); } });
is not cool yy, no matter what entity object to do you still. Student and school two entity classes have only one attribute name, address, and now the requirements change, in the student class, a New Age attribute, displayed on the ListView. That way, we're going to have to add a new TextView to our layout. With the show age, we don't have a new Android that comes with the system. R.layout.simple_list_item_2, so you can dry it, reconstruct the list<student> data set.
arraylist<student> datas = new ArrayList (); Datas.add (New Student (21, "Zhang San")); Datas.add (New Student (22, "John Doe")); Setlistadapter (New com.mylhyl.cygadapter.sample.blog.cygadapter<student> (GetContext (), datas) { @ Override public void Onbinddata (Viewholder viewholder, Student item, int position) { ViewHolder.tvName.setText (ITEM.NAEM); ViewHolder.tvAge.setText (string.valueof (item.age));//There is no viewholder in tvage Yes } );
The above code Viewholder does not have tvage ah, new one does not have, this certainly is not, and just said add annotation scheme same reason, viewholder in the variable will only more and more, how to do? Even if the problem arises in Viewholder, we'll have to start with him, and we'll see the Viewholder code in GetView.
Before adding age
Viewholder Holder; View view = Convertview; if (view = = null) { view = Inflater.inflate (Android. R.layout.simple_list_item_1, parent, false);//a textview holder = new Viewholder (); Holder.tvname = (TextView) View.findviewbyid (Android. R.ID.TEXT1);//Name View.settag (holder); } else { holder = (viewholder) view.gettag (); }
after adding age
Viewholder Holder; View view = Convertview; if (view = = null) { view = Inflater.inflate (Android. R.layout.simple_list_item_2, parent, false);//two x textview holder = new Viewholder (); Holder.tvname = (TextView) View.findviewbyid (Android. R.ID.TEXT1);//name Holder.tvage = (TextView) View.findviewbyid (Android. R.ID.TEXT2);//Age View.settag (holder); } else { holder = (viewholder) view.gettag (); }
Does the above code see something? is not apart from inflate and Findviewbyid part is not the same, that is a little idea? Create a new class to pull out the same part, write a generic viewholder, and do it!
5, General Viewholder This is a reference to the new Hong Yang Cygviewholder, first look at GetView code, this Cygviewholder class is certainly not every new, only if the Convertview is null only new , so a static method is used to obtain cygviewholder, in order to not be the external direct new Cygviewholder, we have to turn the construction method to private. Come on, look at the code after the transformation!
Cygviewholder
Public final class Cygviewholder { private Context mcontext; Private View Mitemview; public static Cygviewholder Get (context context, int resource, View Convertview, ViewGroup parent) { if (Convertview = = null) { View view = Layoutinflater.from (context). Inflate (resource, parent, false); return new Cygviewholder (context, view); } Return (Cygviewholder) Convertview.gettag (); } Private Cygviewholder (context context, View Itemview) { Mcontext = context; Mitemview = Itemview; Mitemview.settag (this); } Public View Getitemview () { return mitemview; }}
Cygadapter
@Override public View getView (int position, view Convertview, ViewGroup parent) { //without a heap of duplicate code Cygviewholder Viewholder = cygviewholder.get (context, resource, convertview, parent); T student = GetItem (position); Onbinddata (Viewholder, student, position); return Viewholder.getitemview (); } Viewholder Change to Cygviewholder public abstract void Onbinddata (Cygviewholder viewholder, Final T item, final int position);
there was a problem with the ListView binding Cygadapter, unable to find the control
arraylist<student> datas = new ArrayList (); Datas.add (New Student (21, "Zhang San")); Datas.add (New Student (22, "John Doe")); Setlistadapter (New com.mylhyl.cygadapter.sample.blog.cygadapter<student> (GetContext (), datas) { @ Override public void Onbinddata (Cygviewholder viewholder, Student item, int position) { //Find TextView, Still no tvname\tvage? How to deal with, think of a way Viewholder.tvname = (TextView) View.findviewbyid (Android. R.ID.TEXT1); Viewholder.tvage = (TextView) View.findviewbyid (Android. R.ID.TEXT2); Set Data ViewHolder.tvName.setText (ITEM.NAEM) for TextView; ViewHolder.tvAge.setText (string.valueof (item.age)); } );
How does the above code onbinddata the problem of binding data snippets? The idea is this, since Cygviewholder already has everyitem's View, then we can write a Findviewbyid method to find the control in Cygviewholder, and cache it in sparsearray<view>, if there is a direct return from the cache, Otherwise, the cache is returned by Itemview.findviewbyid lookup.
public final class Cygviewholder {private Context mcontext; Private sparsearray<view> mviews;//Change private View mitemview; public static Cygviewholder Get (context context, int resource, View Convertview, VIEWGR OUP parent) {if (Convertview = = null) {View view = Layoutinflater.from (context). Inflate (Resource, pare NT, FALSE); return new Cygviewholder (context, view); } return (Cygviewholder) Convertview.gettag (); } Private Cygviewholder (context context, View Itemview) {mcontext = context; Mviews = new sparsearray<> ();//Change mitemview = Itemview; Mitemview.settag (this); } public <t extends view> T findviewbyid (int id) {//new method View view = Mviews.get (ID); if (view = = null) {view = Mitemview.findviewbyid (ID); Mviews.put (ID, view); } return (T) view; }}
Use
arraylist<student> datas = new ArrayList (); Datas.add (New Student (21, "Zhang San")); Datas.add (New Student (22, "John Doe")); Setlistadapter (New com.mylhyl.cygadapter.sample.blog.cygadapter<student> ( getcontext (), Android. R.layout.simple_list_item_2, datas) { @Override public void Onbinddata (Cygviewholder viewholder, Student item , int position) { TextView tvname = Viewholder.findviewbyid (Android. R.ID.TEXT1); TextView tvage = Viewholder.findviewbyid (Android. R.ID.TEXT2); Tvname.settext (ITEM.NAEM); Tvage.settext (string.valueof (item.age)); } );
Now is not much simpler, adapter general, just use when the control in the Onbinddata, to bind the control data, it is so simple. Content compared to write more verbose, but the starting point is good, please read patiently, do not spray Oh!!! Library Address: Https://github.com/mylhyl/Android-CygAdapter
Explain the Cygviewholder I only joined the Textview.settext method, if necessary to add other methods can add Q Group:435173211
You can also go to GitHub first fork and then new pull request can be issues
Android Universal ListView, GridView Adapter