Android App Architecture essay five (Actionbar with side-slip menu drawerlayout)

Source: Internet
Author: User

ActionBar (V7) is very simple to add, only need to specify application in Androidmanifest.xml or activity theme is Theme.holo or its subclasses can be, on Android In versions 3.0 and later, the activity contains the Actionbar component by default.

Drawerlayout (V4) is a control that implements the side-by-side menu effect in the Support library package, which can be said to be drawerlayout as a result of the appearance of third-party controls such as Menudrawer. Drawerlayout is divided into the side menu and the main content area, the side menu can be expanded and hidden according to the gesture (drawerlayout own characteristics), the content of the main content area can be changed with the menu click (This requires the user to implement).

<Android.support.v4.widget.DrawerLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/drawer_layout"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <RelativelayoutAndroid:id= "@+id/content_frame"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">        <Com.heima.googleplay.widget.PagerTabAndroid:id= "@+id/tabs"Android:layout_width= "Match_parent"Android:layout_height= "42dip"Android:background= "@drawable/bg_tab"/>        <Android.support.v4.view.ViewPagerAndroid:id= "@+id/pager"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_below= "@+id/tabs"Tools:context=". Mainactivity "/>    </Relativelayout>    <FramelayoutAndroid:id= "@+id/start_drawer"Android:layout_width= "250DP"Android:layout_height= "Match_parent"android:layout_gravity= "Start"Android:background= "@drawable/bg_tab"></Framelayout></Android.support.v4.widget.DrawerLayout>
Activity_main.xml

Main Page and slide two page, slide page:

<framelayout
Android:id= "@+id/start_drawer"
Android:layout_width= "250DP"
android:layout_height= "Match_parent"
Android:layout_gravity= "Start"
android:background= "@drawable/bg_tab" ></FrameLayout>

Start swipe from left to right, end swipe from right to left

Inherit actionbaractivity

Importandroid.app.Application;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity; Public Abstract classBaseactivityextendsactionbaractivity {Private Staticbaseactivity mforegroundactivity; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);    Initview (); }        protected Abstract voidInitview (); @Overrideprotected voidOnresume () {//TODO auto-generated Method Stub        Super. Onresume ();  This. mforegroundactivity = This; }             Public Staticbaseactivity getforegroundactivity () {returnmforegroundactivity; }}
baseactivity

Setcontentview (R.layout.activity_main);

Importandroid.support.v4.app.Fragment;ImportAndroid.support.v4.app.FragmentManager;ImportAndroid.support.v4.app.FragmentPagerAdapter;ImportAndroid.support.v4.app.FragmentStatePagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.view.Menu;Importcom.heima.googleplay.fragment.AppFragment;Importcom.heima.googleplay.fragment.BaseFragment;Importcom.heima.googleplay.fragment.FragmentFactory;Importcom.heima.googleplay.fragment.GameFragment;Importcom.heima.googleplay.fragment.HomeFragment;Importcom.heima.googleplay.utils.UIUtils;ImportCom.heima.googleplay.widget.PagerTab; Public classMainactivityextendsBaseactivityImplementsOnpagechangelistener {PrivatePagertab Mtabs; PrivateViewpager Mpager; @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }     Private classMainadapterextendsfragmentstatepageradapter{Privatestring[] Tab_names; Privatebasefragment fragment;  PublicMainadapter (fragmentmanager FM) {Super(FM); Tab_names=Uiutils.getstringarray (r.array.tab_names); } @Override PublicCharsequence Getpagetitle (intposition) {            //TODO auto-generated Method Stub            returnTab_names[position]; } @Override PublicFragment GetItem (intposition) {                        returnfragmentfactory.createfragment (position); } @Override Public intGetCount () {//TODO auto-generated Method Stub            returntab_names.length; }} @Overrideprotected voidInitview () {Setcontentview (r.layout.activity_main); Mtabs=(Pagertab) Findviewbyid (r.id.tabs); Mpager=(Viewpager) Findviewbyid (R.id.pager); Mainadapter Adapter=NewMainadapter (Getsupportfragmentmanager ());        Mpager.setadapter (adapter); //bind to togetherMtabs.setviewpager (Mpager); Mtabs.setonpagechangelistener ( This); } @Override Public voidOnpagescrollstatechanged (intarg0) {        //TODO auto-generated Method Stub} @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {        //TODO auto-generated Method Stub} @Override Public voidOnpageselected (intarg0) {Basefragment Fragment=fragmentfactory.createfragment (arg0);    Fragment.show (); }}
mainactivity

Android App Architecture essay five (Actionbar and slide-out menu drawerlayout)

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.