Android Learning Note andbase Framework Learning (i) Realization of multi-function title bar

Source: Internet
Author: User

Ps:volley Framework finally by looking at the source of the way to complete all the learning. Start Learning andbase ... The source of Andbase is much too outrageous ... Therefore, all the source code is not analyzed ...

Learning content:

1. Use Andbase to realize multi-function title bar ...

The Andbase framework provides many ways to enable us to set a better title bar for dynamic changes, rather than statically writing the title bar interface to death ... Can make the title bar more beautiful ... The overall is the dynamic get layout and then customize a good title bar by manipulating the layout ...

When using the Andbase framework, our main function is not inherited from Acticity, but inherits from Abactivity, original aim or activity ...

 Public class extends Abactivity

Inherited the abactivity so that their activity has a framework ... We can use some of the variables that are contained inside ... Take our title bar variable For example ... Mabtitlebar is the inside of a title bar variable ... Any activity that inherits Abactivity can use this variable, which will be used as the title bar variable in the current activity. We can directly set up some of the related properties. To write our own title bar ...

1.1 Set the title bar with or without ...

Whether or not the title bar is set is to set the current Mabtitlebar to show ... By calling the Setvisiable function to make the relevant settings ...

 Public void setvisiable (boolean  b) {        if(b) {            //  Set the title bar to appear on the view ...        } Else {            titlebar.setvisibility (view.gone); // Setting the title bar does not appear on the view ...         }
}

1.2 Add a new control to the title bar ...

Whether in the title bar or activity if you want to add a control dynamically, it is necessary to add the view process ... Before adding a view, we need to add the control to the view and then add the view to the activity, which completes the effect of dynamically adding views ... View is really just an empty shelf, more image of a balloon is not playing up the same. Light has a shell, nothing, then if you want to have something, then we need to inflate it, only after the gas-filled view is what we want to load ... Otherwise, it's useless.

The process of inflating is to use Layoutinflater.inflater (r.layout.xxx,root), function to do ... Familiar with the ListView use then presumably also very clear this process ... When using the ListView, we are simply writing the layout of the item in the ListView, but never write something in the layout that is dead in the item layout. Because things in item are generally added dynamically ... So we generally get the ID of the ListView and then load it dynamically for each item ... This is usually done with the ListView ... It's all the same ... Adding views is dynamically added ... After adding the view, we can still do more by getting the control and then binding the listener ...

This function is a function I have customized ... arg0 and arg1 are the views obtained through inflater.inflate ...

 Public voidSetrightview (intRESID0,intresid1)          {Clearrightview ();  Titlebar.addrightview (arg0); //add a view to the title bar ...Titlebar.addrightview (ARG1); Moreview= (Button) Arg0.findviewbyid (RESID0);//get control from current View ...moreapp=(Button) Arg1.findviewbyid (RESID1); //after you get the control, set up the related listener to do more ...Moreview.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (Context, "click", Toast.length_long). Show ();                }        }); Moreapp.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubToast.maketext (Context, "continue clicking", Toast.length_long). Show ();    }        }); }

This completes the addition of the view ... In fact, the operation of the title bar is basically the process ... The most important thing is to know how to use the function, how to set the relevant properties for the title bar, such as the background color, title bar text, title bar logo and some related properties of the settings, in fact, it is the layout of the XML in the form of Java code to more flexible use ... You can make the controls on the layout more flexible ...

1.3 Action Bar Transform ...

Let's take a look at a function to see what functions Mabtitlebar have to dynamically set the view ... Here is to introduce some of the relevant functions of the application ... There is no complete code process ... The source code process I will be given at the end:

Btn5.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//The following function calls when there is no problem with the order of the functions ... Different invocation order shows the same result,,,Mabtitlebar.setlogo (R.drawable.button_selector_delete);//set the logo for the title bar ...Mabtitlebar.setlogo2 (R.drawable.button_selector_app);//set a second logo for the title bar ... This setlogo only these two functions: No Logo3 ... If you want to set more logos. can only write XML layout ...Mabtitlebar.clearrightview ();//clear the view to the right of the title bar ...View Rightviewok = minflater.inflate (R.LAYOUT.OK_BTN,NULL);//inflate for a view view ...Mabtitlebar.addrightview (Rightviewok);//Add the currently inflated view to the right of the title bar ...Mabtitlebar.settitletext ("modifying");//set the Text property of the title bar ...Mabtitlebar.settitlebarbackground (R.DRAWABLE.TOP_BG2);//sets the background of the text ...Mabtitlebar.settitlebargravity (Gravity.left,gravity.center);//set the title bar to its way ...Mabtitlebar.setlogoonclicklistener (NewOnclicklistener () {//set up monitoring for logo images ...@Override Public voidOnClick (View v) {//Change related properties ... mabtitlebar.settitlebarbackground (R.DRAWABLE.TOP_BG);Mabtitlebar.settitletext ("Multi-function title bar");                        Mabtitlebar.clearrightview ();                        Mabtitlebar.setlogo (R.drawable.button_selector_back);                        Mabtitlebar.settitlebargravity (Gravity.left,gravity.right); Mabtitlebar.settitletextmargin (20, 0, 0, 0); Mabtitlebar.getlogoview2 (). setvisibility (View.gone); //Set Visibility ...Mabtitlebar.setlogoonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {finish ();                    }                        });            }                });    }        }); 

1.4 drop-down effect in title bar ...

The dropdown in the title bar is actually clicking on the title to form the drop-down menu. Each drop-down menu is a sub-option ... With these sub-options we can do more things ... Effect as above ... In fact, each such a similar menu is a ListView. By initializing each item of the ListView, it is natural to have this effect ... In fact, the overall dynamic layout ...

Btn7.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {mabtitlebar.settitletextbackgroundresource (R.DRAWABLE.DROP_DOWN_TITLE_BTN); View Popview= Minflater.inflate (R.layout.list_pop,NULL);//get a view first :ListView Poplistview = (ListView) Popview.findviewbyid (R.id.pop_list);//gets the ID of the ListView :list<abmenuitem> list =NewArraylist<abmenuitem> ();//define a list item ... This class only holds the ID and Text properties :List.add (NewAbmenuitem ("Cai Wenji")); List.add (NewAbmenuitem ("Marten Cicada")); List.add (NewAbmenuitem ("Purple Poppy")); List.add (NewAbmenuitem ("Sun Shangxiang")); Listpopadapter Mlistpopadapter=NewListpopadapter (titlebaractivity. This, List,r.layout.list_pop_item);//define an adapter ...Poplistview.setadapter (Mlistpopadapter);//set up an adapter for the ListView ...Mabtitlebar.settitletextdropdown (popview);//set the titlebar on the TextView to be tapped .            }        });

With this setting, it is easy to create such an effect. The Abmenuitem class is used here ... Through the source code we can know that this class has two constructors, one is to the int ID and string text Two properties to save, one is to just save a string Text property ... This makes it easier to compare what the ListView shows ... And generally this small ListView is generally a text value. Therefore, it is more convenient to use abmenuitem ... This also completes a drop-down menu ...

1.5 adapter ...

Simply say the adapter this thing ... The adapter is primarily used to dynamically set up each item in the ListView, allowing the ListView to be more flexible in its layout ... To be blunt is to define a style, and each item in the ListView needs to be displayed in this style ... It's very simple ...

/** * [email protected] by darker 2015-10-30 * multi-function Menu ... **/ PackageCom.example.andbasetitlebar;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.TextView;ImportCom.ab.global.AbMenuItem; Public classAdapterextendsBaseadapter {//Inherit Baseadapter    Privatecontext Context; PrivateList<abmenuitem>list; Private intItemresource;  PublicAdapter (context context, list<abmenuitem> List,intItemresource) {         This. Context =context;  This. List =list;  This. Itemresource =Itemresource; } @Override Public intGetCount () {returnlist.size (); } @Override PublicObject GetItem (intposition) {        returnList.get (position); } @Override Public LongGetitemid (intposition) {        returnposition; }        //because the item in our ListView is displayed in a very simple way, the Viewholder class simply defines a TextView property ...@Override PublicView GetView (intposition, View Convertview, ViewGroup viewgroup)                {Viewholder holder; /*This judgment is actually defined for reuse ...             Because the ListView is loaded dynamically: * If the item in the ListView exceeds the size of the screen, it will need to be pulled down ... * drop-down means that the old ListView will need to be destroyed ... * then you can't always slide every time we create a new view Holder object: We can re-use these destroyed viewholder ... * after re-use, the Viewholder will automatically call those destroyed for reuse when you swipe again ... This allows the resource to be reloaded ... * to be blunt is to improve the reusability of ...*/         if(Convertview = =NULL) {Convertview= Layoutinflater.from (context). Inflate (Itemresource,NULL);//define a View object , view ...Holder =NewViewholder (); Holder.itemtext= (TextView) Convertview.findviewbyid (R.id.pop_item);//gets the ID of the ListView in the current view:Convertview.settag (holder);//Set flag:}Else{Holder= (Viewholder) convertview.gettag ();//Here you can get the flags directly, reuse the holder, reduce the memory allocation and overhead ...} Abmenuitem Item=List.get (position); Holder.itemText.setText (Item.gettext ()); //set the value for TextView in each item ...                returnConvertview; }    Static classViewholder {TextView itemtext; }}    

The implementation of the multi-functional menu is very simple, most of the functions are written function, we just need to call to set the title bar of the relevant style ... Implement a multifunctional menu ... Finally, attach the code I wrote myself. Or the complete code can make us progress more quickly ... So that you can understand the invocation process and principle ...

Source code: Http://files.cnblogs.com/files/RGogoing/AndbaseTitleBar.zip

Android Learning Note andbase Framework Learning (i) Realization of multi-function title bar

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.