First, Introduction navigation pumpDrawer Displayon the left-most side of the screen, hidden by default, a hidden panel appears when the user slides from the edge to the other with his finger, and the drawer navigation disappears when you click outside the Panel or slide in the original direction! Well, this drawer is drawerlayout, which is in the V4 package.
android.support.v4.widget.DrawerLayout.
Second, use
the steps to implement the drawer navigation are very simple. Simply configure the layout with the drawer navigation to achieve a simple planning menu. The layout code is as follows:
<android.support.v4.widget.drawerlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:id= "@+id/id_drawerlayout" android:layout_width= "Match_parent" an droid:layout_height= "Match_parent" > <framelayout android:id= "@+id/id_framelayout" Android:layout_w Idth= "Match_parent" android:layout_height= "match_parent" > <button android:id= "@+id/id_btn1 "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:text=" 1111 "/> </FrameLayout> <linearlayout android:id=" @+id/id_drawer "android:layout_width=" 24 0DP "android:layout_height=" match_parent "android:layout_gravity=" Start "android:background=" #E0EEE0 " android:orientation= "vertical" > <listview android:id= "@+id/id_lv" Android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:divider= "#CD853F" android:dividerheight= "2DP" > </ListView> </LinearLayout></android.support.v4.widget.DrawerLayout>
Description:1. The drawerlayout must be used as a label for the layout. 2. Then add a view that contains content to the tag, which is the layout of the content displayed when the drawer is completely hidden. 3. Add a drawer layout, this layout can be defined according to the requirements, my demo is a ListView. 4. Drawer layout, you need to specify the Android:layout_gravity attribute, the official note is to use start instead of left.
But I tried. The effect of start and left,right and end is the same. Know what the difference is? ( who knows, please leave a message to tell!) Thank you! )5. The width of the drawer layout is best not to exceed 320DP, so that when the drawer is fully displayed, the content layout is not completely obscured.
Well, when you set up the layout file in your activity, you can implement a simple side-slip menu, but the menu is on it.
Next, you will initialize the ListView.
MLv = (ListView) Findviewbyid (r.id.id_lv), str = new string[] {"Item1", "item2", "Item3"}; arrayadapter<string> adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, str); Mlv.setadapter (adapter); Mlv.setonitemclicklistener (this);
below, set the listener event for the drawer navigation. There are three ways to add a listener.
1. Set Drawerlayout.drawerlistener as the Listener class, which contains four callback functions.
The code is as follows:
Mdrawerlayout.setdrawerlistener (New Drawerlistener () {/** * is called when the drawer sliding state is changed * The status value is State_idle (idle--0), State_ Dragging (dragged--1), one of state_settling (fixed--2). * When the drawer is open, click on the drawer, the status of drawer will become state_dragging, and then become state_idle*/@Overridepublic void ondrawerstatechanged (int arg0) { LOG.I ("drawer", "Drawer Status:" + arg0);} /** * This method is called when the drawer is sliding * ARG1 indicates the amplitude of the slide (0-1) */@Overridepublic void Ondrawerslide (View arg0, float arg1) {log.i ("drawer", arg 1 + "");} /** * is called when a drawer is fully opened */@Overridepublic void ondraweropened (View arg0) {log.i ("drawer", "drawer is completely open!") ");} /** * When a drawer is completely closed call this method */@Overridepublic void ondrawerclosed (View arg0) {log.i ("drawer", "drawer is completely closed!") ");}});
2. Set Drawerlistener subclass Simpledrawerlistener, use this class without implementing all the callback functions, you can rewrite the corresponding method according to your own needs.
The code is as follows:
Mdrawerlayout.setdrawerlistener (New Drawerlayout.simpledrawerlistener () {@Overridepublic void ondrawerclosed (View Drawerview) {super.ondrawerclosed (Drawerview);} @Overridepublic void ondraweropened (View drawerview) {super.ondraweropened (Drawerview);}});
3.use the subclass Actionbardrawertoggle of Drawerlistener. Generally used in conjunction with Actionbar.
The code is as follows:
Mdrawertoggle = new Actionbardrawertoggle (this, Mdrawerlayout,r.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {@Overridepublic void ondrawerclosed (View drawerview) {Getactionbar (). Settitle (Mtitle); Invalidateoptionsmenu ();} @Overridepublic void ondraweropened (View drawerview) {Getactionbar (). Settitle (Mdrawertitle); Invalidateoptionsmenu ( );}}; Mdrawerlayout.setdrawerlistener (Mdrawertoggle);
call Closedrawer () and Opendrawer () to close and open the drawer. The other way is to refer to the API bar! Iii. Summary
The
drawer navigation is introduced here, in general drawer is very simple to use. But the downside is that it shows the drawer above the content, not the bottom. I think most of the side-slip menus are usually below. I also found no function in the API to set up the upper and lower drawer relationships.
If you want the menu in the following way, use the slidingmenu above github!
Four, download
Demo click here to download! Welcome advice! If you have any questions, please leave a message!
Drawerlayout of Android Components (drawer navigation)-side-slip menu effect