Android Development uses andbase framework to implement multi-function title bar

Source: Internet
Author: User
Tags static class stub

What to learn in this tutorial:

1. Use Andbase to implement the multi-function title bar ...

The Andbase framework provides many ways to enable us to set up a better title bar for dynamic change rather than statically writing the title bar interface to death ... Can make the title bar more beautiful ... The overall thing is to get the layout dynamically 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, same or activity ...

public class Mainactivity 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 ... mabtitlebar is an inner title bar variable ... An activity that inherits Abactivity can use this variable as a title bar variable in the current activity. We can directly make some of the relevant properties of the setting. So that we can write our own title bar ...

1.1 Set the title bar ...

The title bar has no setting is actually set the current Mabtitlebar whether to display ... By calling the Setvisiable function to make the related settings ...

public void Setvisiable (Boolean b) {
if (b) {
Titlebar.setvisibility (view.visible); Set the title bar to appear on the view ...
}else{
Titlebar.setvisibility (View.gone);/Set the title bar does not appear on the view ...
}
}

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

Whether you want to add a control dynamically in the title bar or in an activity, it's definitely a process of adding a view ... 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 ... The view is actually just an empty shelf, more image is a not playing the same balloon. 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 no use.

The process of inflating is to use Layoutinflater.inflater (r.layout.xxx,root) function to complete ... Familiar with the use of ListView then presumably also very clear this process ... When using ListView, we just write the layout of the item in ListView, but never write the items in the layout in the dead item layout. Because things in the item are generally dynamically added ... So we generally get the ID of the ListView and then dynamically load each item ... This is generally the operation of the ListView ... In fact, all the same ... Add views are 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 of my custom ... arg0 and arg1 are the views obtained through inflater.inflate ...

public void Setrightview (int resid0,int resid1) {
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 related listening to do more ...
Moreview.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Toast.maketext (Context, "click", Toast.length_long). Show ();
}
});

Moreapp.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Toast.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 exactly what the function is, how to set the relevant properties for the title bar, such as background color, title bar text, title bar logo, and some related properties of the settings, in fact, is to put XML that layout way through Java code to more flexible to 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 Mabtitlebar which functions can dynamically set view 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 finally give the ...

Btn5.setonclicklistener (New View.onclicklistener () {

@Override
The problem of the function order is not present in the call of the public void OnClick (View v) {///following function ... The results of the different call sequences show the same,,,
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 views ...
Mabtitlebar.addrightview (Rightviewok); Add the currently inflated view to the right of the title bar ...
Mabtitlebar.settitletext ("being modified"); Set the Text property of the title bar ...
Mabtitlebar.settitlebarbackground (R.DRAWABLE.TOP_BG2);//Set the background of the text ...
Mabtitlebar.settitlebargravity (Gravity.left,gravity.center),//Set the title bar to its way ...
Mabtitlebar.setlogoonclicklistener (New Onclicklistener () {
Set up monitor for logo picture ...
@Override
public void OnClick (View v) {
Change related properties ... mabtitlebar.settitlebarbackground (R.DRAWABLE.TOP_BG);
Mabtitlebar.settitletext ("Multifunctional 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 (New Onclicklistener () {

@Override
public void OnClick (View v) {
Finish ();
}
});
}
});
}
});



1.4 Drop-down Effect of the title bar ...



The bottom of the title bar is actually clicking the title to form a 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, every such similar menu is a ListView. By initializing each item of the ListView, it is also natural to form this effect ... In fact, the overall or dynamic layout ...

Btn7.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (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 = new arraylist<abmenuitem> (); Define a list item ... This class saves only the ID and Text properties ...
List.add (New Abmenuitem ("Wenji"));
List.add (New Abmenuitem ("Diao cicada"));
List.add (New Abmenuitem ("Purple poppy"));
List.add (New Abmenuitem ("Sun Shangxiang"));
Listpopadapter mlistpopadapter = new Listpopadapter (titlebaractivity.this, List,r.layout.list_pop_item); Define an adapter ...
Poplistview.setadapter (Mlistpopadapter); Set an adapter for ListView ...

Mabtitlebar.settitletextdropdown (Popview); Set the TextView on the titlebar to be tapped.
}
});



With this setting, it's easy to create this effect. The Abmenuitem class is used here ... From the source 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 save only a string Text property ... This also makes the ListView display of things relatively simple ... And generally this small listview is usually 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 ... Adapters are mainly used to dynamically set each item in the ListView, making the layout of the ListView more flexible ... To be blunt is to define a style, each item in ListView need to follow this style to show ... It's very simple ...


/*
*
* copyright@ by Darker 2015-10-30
* Multi-function Menu ...
*
* */

Package Com.example.andbasetitlebar;


Import java.util.List;

Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView;

Import Com.ab.global.AbMenuItem;


public class Adapter extends Baseadapter {//Inheritance Baseadapter

private context;

Private list<abmenuitem> List;

private int itemresource;

Public Adapter (context context, list<abmenuitem> List,int Itemresource) {
This.context = context;
This.list = list;
This.itemresource = Itemresource;
}

@Override
public int GetCount () {
return List.size ();
}

@Override
Public Object getitem (int position) {

return List.get (position);
}

@Override
public long getitemid (int position) {
return position;
}
Because the item in our ListView is displayed in a very simple way, the Viewholder class simply defines a TextView property ...
@Override
Public View GetView (int position, View Convertview, ViewGroup viewgroup) {

Viewholder Holder;
/* This judgment is actually defined for reuse ...             Because the ListView are loaded dynamically. * If the item in the ListView exceeds the size of the screen, then there is bound to be a drop down ... * the dropdown on behalf of the original ListView will need to be destroyed ...
* Then you can't always slide every time we create a new Viewholder object. We can reuse these destroyed viewholder ...
* Once reused, the Viewholder will automatically invoke those destroyed for reuse when sliding again ... So that the resources are reloaded ...
* To put it bluntly is to improve the reuse of ...
*/
if (Convertview = = null) {
Convertview = Layoutinflater.from (context). Inflate (Itemresource, NULL); Define a View Object ...
Holder = new Viewholder ();
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 allocation and overhead of the memory ...
}
Abmenuitem item = list.get (position);
Holder.itemText.setText (Item.gettext ()); Set the value for the TextView in each item ...

return convertview;
}

Static Class Viewholder {
TextView Itemtext;
}

}


Implementation of the Multi-function menu is very simple, most of the functions are written functions, we only need to call to set the relevant style of the title bar ... Implement a versatile menu ... Finally append the code of my own writing. Or the complete code can make us progress more quickly ... So you can understand the calling process and principle ...




The Android Rapid Development Framework Andbase

Andbase is an open source class library product tailored for Android developers, where you can get the latest code, examples, and development documentation.

What can andbase do? The

1.andbase contains a number of common tools for development.

such as network download data methods, multithreading, thread pool management, image cache management, image file download upload, the common tools in the string, date, file processing, image processing, etc. for further encapsulation, to enable your application in the team development to reduce redundant code, Greatly improve the maintenance of the Code and the development of efficient line, can be very good to avoid the development of negligence caused by the common mistakes. The

2.andbase encapsulates a large number of controls that are commonly used and that are not in the system itself.

such as list paging, Drop-down refresh, picture Carousel, multithreading support breakpoint continuation of the download, left right sidebar, picture multiple upload, the iphone more popular wheel selection, on the basis of achart to improve and streamline the chart, calendar selector and so on.

2. Powerful abactivity, you have no reason not to inherit it.

Inherits it you can get a simple and powerful set of title bars, as well as a series of simple calls, such as pop-up boxes, cue boxes, progress boxes, and so on.

3. Provides a high efficiency picture cache management strategy, so that memory savings, utilization and efficiency improved.

Programs to manage a large number of picture resources, but also to download from the network, Andbase provides a simple way to complete the download and display, and support scaling, cropping, caching features.

4. Date processing code, a lot of format in the project, one from the Internet to find, much trouble, andbase can be solved. The

Date handles a variety of functions, you know.

5. Andbase no longer need to handler, line Chengri direct toast, no problem. The

handler generates a lot of code and is not maintainable, andbase the handler, and forget it.

6. Simple Lightweight support annotation Automatic table ORM Framework (1.5.2 New, multiple Xiaixiao provide source).

Whether it feels like writing a SQL write paragraph, it is cumbersome to build a table, with the annotation bar, a sentence to complete the increase and deletion of the check.

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.