"This is a clue." Adapter June never willing clues, same design pattern, adapter mode is not today's theme, today's topic is how to use the delegate mode in the adapter to clear the design of the elements of the Division of labor.
An interface with adapter view, activity as a controller in the MVC pattern, the data control should be done by it. So, in order to let adapter only responsible for data interface binding, the activity is responsible for controlling the data changes, the client model should be brave to stand up, so that the adapter may be involved in the control entrusted to the activity to deal with.
I'm going to give you another example, the code can't hold it, it's under ↓↓↓↓↓↓.
1 PackageCom.change.delegateforadapterdemo;2 3 Importjava.util.List;4 5 ImportAndroid.content.Context;6 ImportAndroid.view.LayoutInflater;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.view.ViewGroup;Ten ImportAndroid.widget.BaseAdapter; One ImportAndroid.widget.Button; A ImportAndroid.widget.TextView; - - /** the * The principal model is used in adapter - * @author Change - * - */ + Public classDelegateforadapterextendsBaseadapter { - PrivateList<string>names; + PrivateLayoutinflater Minflater; A Privatedatacontroldelegate mdelegate; at PublicDelegateforadapter (Context ctx,list<string>_names) { - This. Names =_names; -Minflater =Layoutinflater.from (CTX); - } - /** - * Delegate interface for manipulating data in adapter, which provides only one way to delete a single piece of data. in * @author Change - * to */ + Public Static Interface datacontroldelegate{33/**34 * Delete data * @param name36 */3 7 public void RemoveItem (String name); the + @Override A Public intGetCount () { the returnnames.size (); + } - $ @Override $ PublicObject GetItem (intarg0) { - returnNames.get (arg0); - } the - @OverrideWuyi Public LongGetitemid (intarg0) { the returnarg0; - } Wu - @Override About PublicView GetView (intarg0, View arg1, ViewGroup arg2) { $Viewholder holder =NULL; - if(NULL==arg1) { -Arg1 = Minflater.inflate (R.layout.item_name,NULL); -Holder =NewViewholder (); AHolder.name =(TextView) Arg1.findviewbyid (r.id.name); +Holder.remove =(Button) Arg1.findviewbyid (r.id.remove); the Arg1.settag (holder); -}Else{ $Holder =(Viewholder) Arg1.gettag (); the } the FinalString name =Names.get (arg0); the Holder.remove.setOnClickListener (New Onclicklistener () {@Override70 public void OnClick (Vie W arg0) {null!=mdelegate) Mdelegate.removeitem (name);//delete behavior 73}74 }); the Holder.name.setText (name); the returnarg1; the } + Static classviewholder{ - PublicTextView name; the PublicButton Remove;Bayi } the /** the * Set up delegates - * @param_delegate - */ the Public voidsetdatacontroldelegate (datacontroldelegate _delegate) { the This. mdelegate =_delegate; the } the -}
"Client interface" most of the design patterns are led by the interface, so, have to mention it, the above code is labeled red is our client interface, the implementation of its module will be reduced to the entrusted object. The purple part above is the need to delegate the content, originally clicked Delete behavior is done in the list item, but in order to clear the Division of labor, we entrust it out, it is time to look at the poor entrusted object.
The "entrusted object" tragic activity is also to help adapter complete the work, it implements the client interface, delete data item behavior for it, easy.
1 PackageCom.change.delegateforadapterdemo;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importandroid.app.Activity;7 ImportAndroid.os.Bundle;8 ImportAndroid.view.Menu;9 ImportAndroid.widget.ListView;Ten ImportAndroid.widget.Toast; One A Importcom.change.delegateforadapterdemo.DelegateForAdapter.DataControlDelegate; - - /** the * Implement the data delegation interface in adapter, and let the activity control all the behaviors. - * @author Change - * - */ + Public classMainactivityextendsActivityImplementsdatacontroldelegate{ - Privatelist<string> names =NewArraylist<string>(); + PrivateDelegateforadapter Adapter; A @Override at protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - Initdatas (); -ListView LV =(ListView) Findviewbyid (r.id.lv); -adapter =NewDelegateforadapter ( This, names); inAdapter.setdatacontroldelegate ( This); - Lv.setadapter (adapter); to } + - @Override the Public BooleanOncreateoptionsmenu (Menu menu) { * //inflate the menu; This adds items to the action bar if it is present. $ getmenuinflater (). Inflate (R.menu.main, menu);Panax Notoginseng return true; - } the + Private voidInitdatas () { A for(inti=0;i<10;i++){ theNames.add ("name" +i); + } - } $ $ @Override - Public void RemoveItem (String name) {names.remove (name); adapter.notifydatasetchanged (); 50 Toast.maketext (This, "deleted" +name, Toast.length_short). Show (); the -}
"It's still so simple." RemoveItem (), a method represents a behavior that easily implements the deletion requirement, and the interface changes after the notifydatasetchanged () notification. So far, everything has happened as we had imagined.
"Full Code" GitHub address:Https://github.com/ChangeWu/SomePoject/tree/master/DelegateForAdapterDemo
What you do: Use the delegate mode in your Android adapter