Official website: https://developer.android.com/training/implementing-navigation/nav-drawer.html
Paste the main logic and layout files:
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><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" > <!--Content--<framelayout android:id= "@+id/drawer_container" Android:layout_width= "Match_parent" android:layout_height= "Match_parent"/> <!--drawer--> <L Istview android:id= "@+id/drawer_menu" android:layout_width= "240DP" android:layout_height= "Match_paren T "android:background=" #FFF0F0F0 "android:layout_gravity=" Start "/> <!--Android:choicemode The checked state has no conflict with Onitemclick the None value is 0, which indicates no selection mode, and the Singlechoice value is 1, which indicates up to One is selected; the Multiplechoice value is 2, which means that multiple items can be selected. Android:layout_gravity left or right left or start or end indicates in drawerThe effect is from left to right or right to left--></android.support.v4.widget.drawerlayout>
Note here: The content main interface must be defined in front of the drawer, in addition, android:layout_gravity= "Start" (from left to right) or android:layout_gravity= "End" (from right to left) , this property is required to be defined, otherwise the following exception occurs:
Mainactivity.java
Package Com.jackie.drawerlayoutdemo;import Android.os.bundle;import Android.support.v4.widget.drawerlayout;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.adapterview;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.textview;public Class Mainactivity extends Appcompatactivity {private String mtitle; Private Drawerlayout mdrawerlayout; Private ListView Mdrawermenu; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); InitData (); Default Open drawer Mdrawerlayout.opendrawer (mdrawermenu); private void Initview () {mtitle = Getresources (). getString (R.string.app_name); Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout); Mdrawermenu = (ListView) Findviewbyid (R.id.drawer_menu); } private void InitData () {//array definitionIn the XML file string[] items = getresources (). Getstringarray (R.array.left_drawer_items); arrayadapter<string> adapter = new Arrayadapter<> (this, Android. R.layout.simple_expandable_list_item_1); Adapter.addall (items); Mdrawermenu.setadapter (adapter); Mdrawermenu.setselection (0); Set click on Item Event Mdrawermenu.setonitemclicklistener (new Adapterview.onitemclicklistener () {@Override public void Onitemclick (adapterview<?> parent, view view, int position, long id) {Mtitle = (Te Xtview) view). GetText (). toString (); Switchfragment (); Mdrawerlayout.closedrawer (Mdrawermenu); Mdrawermenu.setselection (position); } }); } private void Switchfragment () {Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.drawer_container, New Itemfragment (Mtitle)). commit (); Getsupportactionbar (). Settitle (Mtitle); }}as follows:
Android Drawerlayout for drawer effect