Nonsense not much to say, directly on:
In fact, the official Google has given an example of the use of drawerlayout, just in the country can not access Google's official website, not see the detailed documentation, so in this simple record slide Drawer menu instructions for use
1. Since drawerlayout requires support for ANDROID.SUPPORT.V4 packages, do not include this package under your libs.
2. First the layout file is as follows
< Android.support.v4.widget.DrawerLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+ Id/drawer_layout "android:layout_width=" match_parent "android:layout_height=" Match_parent "> <!--the main Content View-<framelayout android:id= "@+id/frame_content" android:layout_width= "Match_parent" android:layout_height= "Match_parent" > </FrameLayout> <!--the navigetion view and <listv Iew android:id= "@+id/drawer_list" android:layout_width= "240DP" android:layout_height= "Match_parent" android:layout_gravity= "Start" android:background= "#9999cc" android:choicemode= "Singlechoice" a Ndroid:divider= "@android: Color/transparent" android:dividerheight= "0DP" > </listview></android.suppo Rt.v4.widget.drawerlayout>
Note: 1. The listview in the layout is the side-by-side menu layout container, where the Android:layout_gravity property must be assigned a value. The purpose is to slide the display position of the menu, the official recommended to use "Start"-----Left "end"----to the right
It is not recommended to use left---to the right of the---.
2.FrameLayout Displays the main content section.
In fact, to this step has realized the side-by-side menu effect, you can run a try, but now the side-slip menu has no elements.
3. The code is written as follows:
Package Com.example.drawlayout1;import Android.os.bundle;import Android.app.activity;import android.app.Fragment; Import Android.app.fragmentmanager;import Android.content.res.configuration;import Android.support.v4.app.actionbardrawertoggle;import Android.support.v4.view.gravitycompat;import Android.support.v4.widget.drawerlayout;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.ListView ;p ublic class Mainactivity extends Activity implements Onitemclicklistener {private Drawerlayout mdrawerlayout;private ListView mdrawerlist;private string[] menulists;private arrayadapter<string> adapter;private Actionbardrawertoggle mdrawertoggle;private String titlestring;private int iselect = -1;private View heandrView;@ overrideprotected void OnCreate (Bundle savedinstancestate) {SUPER.ONCREate (savedinstancestate); Setcontentview (R.layout.activity_main);/*get the application title*/titlestring = (String) GetTitle (); Heandrview = Layoutinflater.from (this). Inflate (R.layout.home_menu_list_header, null); mdrawerlayout = ( Drawerlayout) Findviewbyid (r.id.drawer_layout); mdrawerlist = (ListView) Findviewbyid (r.id.drawer_list); Mdrawerlist.addheaderview (heandrview); menulists = Getresources (). Getstringarray (r.array.menu_content); adapter = New Arrayadapter<string> (this,android. R.layout.simple_list_item_1, menulists); Mdrawerlist.setadapter (adapter); Mdrawerlist.setonitemclicklistener (this );/*set the shadow for drawer at start (left) or end (right) */mdrawerlayout.setdrawershadow (R.drawable.drawer_shadow, Gravitycompat.start);/*show the Home Icon*/getactionbar (). Setdisplayhomeasupenabled (True);/*make sure the home icon Enable Click*/getactionbar (). Sethomebuttonenabled (True);/*set the application ActionBar title Changes*/mdrawertoggle = New Actionbardrawertoggle (This, Mdrawerlayout,r.draWable.ic_drawer, r.string.drawer_open,r.string.drawer_close) {@Overridepublic void ondraweropened (View drawerview) { Super.ondraweropened (Drawerview); Getactionbar (). Settitle (r.string.please);} @Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (Drawerview); if ( -1 = = iselect) { Getactionbar (). Settitle (titlestring);} else {Getactionbar (). Settitle (Menulists[iselect-1]);}}};/ *set the Drawerlayout listener*/mdrawerlayout.setdrawerlistener (mdrawertoggle);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {/*make sure the Home icon enable click and display the Drawe rlayout*/if (mdrawertoggle.onoptionsitemselected (item)) {return true;} return super.onoptionsitemselected (item);} @Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {Fragment conteNtfragment = new Contentfragment (); Bundle args = new bundle (); iselect = arg2;if (0 = = iselect) {iselect = 1;} Args.putstring ("text", Menulists[iselect-1]); contentfragment.setarguments (args); Fragmentmanager fm = Getfragmentmanager (); Fm.begintransaction (). replace (R.id.frame_content, contentfragment). Commit (); Mdrawerlayout.closedrawer (mdrawerlist);} /** * When using the Actionbardrawertoggle, you must call it during * Onpostcreate () and onconfigurationchanged () ... */@Ov errideprotected void Onpostcreate (Bundle savedinstancestate) {super.onpostcreate (savedinstancestate);//Sync the Toggle state after onrestoreinstancestate have occurred.mDrawerToggle.syncState ();} @Overridepublic void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig);// Pass any configuration change to the drawer togglsmdrawertoggle.onconfigurationchanged (newconfig);}
There are annotations in the code, and there is no explanation.
@Overridepublic boolean onoptionsitemselected (MenuItem item) {/*make sure the Home icon enable click and display the Drawe rlayout*/if (mdrawertoggle.onoptionsitemselected (item)) {return true;} return super.onoptionsitemselected (item);}
The purpose of this code: Click the Home button on the Actionbar navigation bar to eject and close the Drawerlayout side-slip menu.
/** * When using the Actionbardrawertoggle, you must call it during * Onpostcreate () and onconfigurationchanged () ... */@Ov errideprotected void Onpostcreate (Bundle savedinstancestate) {super.onpostcreate (savedinstancestate);//Sync the Toggle state after onrestoreinstancestate have occurred.mDrawerToggle.syncState ();} @Overridepublic void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig);// Pass any configuration change to the drawer togglsmdrawertoggle.onconfigurationchanged (newconfig);}
The purpose of this code: Google official strongly recommend rewriting two methods, the first method to achieve Actionbar home key and drawerlayout Drawer Menu Association, and Actionbar home has animated effect.
The second method is called on a horizontal screen. Save the state.
Mdrawerlist.setonitemclicklistener (this);
This code is to add drawerlayout drawer menu elements of the click event, the code to implement the click on different elements, replace the different fragment.
/*set the application ActionBar title Changes*/mdrawertoggle = new Actionbardrawertoggle (this, mdrawerlayout, R.drawable.ic_drawer, r.string.drawer_open,r.string.drawer_close) {@Overridepublic void ondraweropened (View Drawerview) {super.ondraweropened (Drawerview); Getactionbar (). Settitle (r.string.please);} @Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (Drawerview); if ( -1 = = iselect) { Getactionbar (). Settitle (titlestring);} else {Getactionbar (). Settitle (Menulists[iselect-1]);}}};/ *set the Drawerlayout listener*/mdrawerlayout.setdrawerlistener (mdrawertoggle);
This code is to add the slide-in menu Drawerlayout to open the close listener event, overriding the effect that you need to implement in the open Close method.
Finally attach the whole project source code: Source Engineering
Google Original Eco-drawer slide-in menu Android drawerlayout Layout Usage Introduction