Often do develop, how can without framework. The framework can not only speed up the development of our developers, but also improve the maintainability of the program so take a moment to learn some of the better framework is still very necessary, this can save a lot of us to write the tedious repetition of the code time, focus on the realization of functional logic, fast quasi-ruthless development of excellent products.
I more time is in the development of Android, here the introduction of the framework is andbase, the advantages here I do not introduce each, bad words I will not spend time to write a blog to introduce it, Project address: Https://github.com/zhaoqp2010/andbase, Official website http://www.amsoft.cn/post-47.html, there is a picture of the effect, the document, there is an apk, can be on the phone to see if the effect is what you want,
I think the effect is good, more in line with the mainstream trend, on GitHub downloaded after the decompression will get two items, one is the source code, one is the demo, where the demo project to reference the source project, so use when you want to import two, the demo project will automatically find the source project this dependence, If you can't find it, you'll see the Red Fork on the project, OK, now it's ready, let's start analyzing the source------
Abactivity is the base class for the entire framework activity, the official is to recommend activity to inherit this class, so that you can have a unified structure of the project, we will find out when we analyze the source code, this class provides us with a lot of common views, such as the title bar TitleBar, the bottom bar, some progress bar, Toast,dialog,handler, in short, can let us save a lot of trouble, of course, you can expand the base class according to your own needs, then as a priority, we analyze this is abactivity class, Let ' s go!!!
This class has about 900 lines of code, a little bit of analysis is not very realistic, I just pick the key points to publish my views, I believe to understand these, this class for you is a small case.
In order to be compatible with the lower API, this class inherits the Fragmentactivity, so we can naturally use the properties and methods of fragmentactivity later.
Then look at the properties, as we said earlier, since this class provides us with a lot of convenient views, then naturally have to have attributes to support them, these properties are mainly debug tag tag, log switch D, loading progress bar progressdialog Mprogressdialog;, from the bottom, Middle, the upper popup dialog and the corresponding content view, Application Object Abapplication, various layoutparams, then the structure of the interface,
The first is Relativelayout ab_base as the overall layout, Abtitlebar Mabtitlebar as the title bar layout, Abbottombar mabbottombar do the bottom column layout, relativelayout Contentlayout do the content layout,
There is also a frame test of Abmonitorview Mabmonitorview and Mmonitorhandler, these two we do not say for the moment, stay behind to talk, and then the Window management Object WindowManager Mwindowmanager and its params:mmonitorparams, and finally a basehandler, is used to handle the asynchronous messages sent over.
Private Handler Basehandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case ABCO Nstant. Show_toast:showtoast (Msg.getdata (). getString ("MSG")); Break;case AbConstant.SHOW_PROGRESS:showProgressDialog ( mprogressmessage); break;case AbConstant.REMOVE_PROGRESS:removeProgressDialog (); Break;case abconstant.remove_ Dialogbottom:removedialog (abconstant.dialogbottom); case AbConstant.REMOVE_DIALOGCENTER:removeDialog ( Abconstant.dialogcenter); case AbConstant.REMOVE_DIALOGTOP:removeDialog (abconstant.dialogtop);d efault:break;}};
Then look at the OnCreate function, pick the important words,
Main title bar Mabtitlebar = new Abtitlebar (this);//Outermost layout ab_base = new Relativelayout (this); Ab_base.setbackgroundcolor ( Color.rgb (255, 255, 255));//Content layout contentlayout = new Relativelayout (this); contentlayout.setpadding (0, 0, 0, 0);// Subtitle Bar Mabbottombar = new Abbottombar (this); Fill in Viewab_base.addview (MABTITLEBAR,LAYOUTPARAMSFW); Relativelayout.layoutparams layoutParamsFW2 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); Layoutparamsfw2.addrule (Relativelayout.align_parent_bottom, Relativelayout.true); Ab_base.addview (Mabbottombar, layoutParamsFW2); Relativelayout.layoutparams layoutParamsFW1 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); Layoutparamsfw1.addrule (Relativelayout.below, Mabtitlebar.getid ()); As we set android:layout_below= "@id/..." in XML Layoutparamsfw1.addrule (Relativelayout.above, Mabbottombar.getid ()) ; Ab_base.addview (Contentlayout, layoutParamsFW1);
Here's what we just said. Add title bar, bottom bar, and main content to the entire layout, because the relative layout, and is set in the code, may seem laborious here, especially the Addrule method, not known before, Jump to the source to see the fact is in the Layoutparams has an array of shapes, attributes as an int, we set the value of the property corresponding to this method
public void addRule (int verb, int anchor) { Mrules[verb] = anchor; }
And then look down, we usually use the function is here, including the popup toast, set the content interface, this has several overloaded functions, let's see one to understand
public void Setabcontentview (View contentview) {contentlayout.removeallviews ();////////First clear the original content Contentlayout.addview ( CONTENTVIEW,LAYOUTPARAMSFF);//Add the new layout to the content,//iocinitiocview ();}
We see here called a Initiocview method, so let's go into it,
private void Initiocview () {field[] fields = GetClass (). Getdeclaredfields (); if (Fields!=null && fields.length >0) {for (Field field:fields) {try {field.setaccessible (true); if (Field.get (this) = null) continue; Abiocview viewinject = field.getannotation (Abiocview.class); if (viewinject!=null) {int viewId = Viewinject.id (); Field.set (This,findviewbyid (viewId)); Setlistener (Field,viewinject.click (), Method.click); Setlistener (field, Viewinject.longclick (), Method.longclick); Setlistener (Field,viewinject.itemclick (), Method.itemclick); Setlistener (Field,viewinject.itemlongclick (), Method.itemlongclick); Abiocselect select = Viewinject.select (); if (! Textutils.isempty (select.selected ())) {Setviewselectlistener (field,select.selected (), select.noselected ());}}} catch (Exception e) {e.printstacktrace ();}}}}
In fact, it is through reflection that we can use annotations to declare the view control, eliminating tedious findviewbyid, such as we used to use the ID tv_name textview, to write this
TextView name;name = (TextView) (Findviewbyid (r.id.tv_name));
And now we just need to use annotations to declare it, OK.
@AbIocView (id=r.id.tv_name) TextView name;
This and a lot of annotation framework is the same reason, but here we see the annotation underlying implementation principle, is not a sudden epiphany, of course, here to use the reflection mechanism and custom annotations, so for some beginners or suggestions first to see this knowledge, we look at the source of Abiocview, is a custom annotation,
@Target (Elementtype.field)//For attribute @retention (retentionpolicy.runtime)//compiler to store annotation in class <span style= " Color:rgb (103, 102, 21); font-family: ' Hiragino Sans GB W3 ', ' Hiragino Sans GB ', Arial, Helvetica, SimSun, u5b8bu4f53; font-size:16px; line-height:28px; "> </span>public @interface abiocview {public int id (); The property must be the other can not be specified because there is a default value of public string click () Default "";p ublic string Longclick () default "";p ublic string ItemClick () Defa Ult "";p ublic String itemlongclick () Default "";p ublic abiocselect Select () Default @AbIocSelect (selected= "");}
Abiocselect similar, we go to see it is to consolidate the above learned things, and then in the Initiocview just now there is such a method Setlistener, is based on the Abiocview that several methods to set up the listener, I am sure you can read, I'm not going to say it here.
Openmonitor and Closemonitor first not to say, generally also use not (actually I have not seen there), then then there is no then, OK, today's analysis to here, if there are errors, please put forward, we exchange learning, common progress---)
Sleep Qu, exhausted.
Android andbase Framework Source code interpretation (a)