Android development of the Navigationdrawer navigation drawer function implementation (source sharing)

Source: Internet
Author: User

The navigation Drawer (navigationdrawer) is a panel that slides in from the left side of the screen to display the main navigation items for your app. Users can open the navigation drawer by sliding into the left edge of the screen or by touching the app icon on the action bar.

The navigation drawer overrides the content, but does not overwrite the action bar. When the navigation drawer is completely open, the title of the action bar needs to be replaced with the name of the app. Instead of displaying the name of the current view. and closes all operation buttons associated with the current view. The "Many other actions" menu button on the action bar does not need to be closed to ensure that users are able to access the settings and help at any time. Below we will implement the function of the navigation drawer.


Layout file Code

<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/content_frame"        Android:layout_width= "Match_parent"        android:layout_height= "match_parent"                     />    <!--the Navigation Drawer-    <listview android:id= "@+id/left_drawer"        android:layout_width= "100DP"        android:layout_height= "Match_parent"        android:layout_gravity= "Start"        android:choicemode= "SingleChoice "        android:dividerheight=" 1DP "       android:background=" #ffffff "/></ Android.support.v4.widget.drawerlayout>
Note The main points:

(1) The main view (above framelayout) must be the first child in Drawerlayout, since XML implies the z-order and the contents of the drawer must.

(2) The primary content view must be set to match the width and height of the parent view, as it hides when it represents the entire UI navigation drawer.

(3) The drawer view (above listview) must specify its horizontal gravity and android:layout_gravity properties. Supports right-to-left language (RTL), specifying values with "Start" instead of "left" (the layout is RTL when appearing in the right drawer).

(4) The drawer view specifies the width of the DP units and the height of the parent view matches. The width of the drawer should not exceed the size of the DP so the user is always able to see the main content.

(5) Drawerlayout must be the root node of the layout

Mainactivity's Code

Package Com.example.g07_navigationdrawer;import Android.app.activity;import Android.app.fragment;import Android.app.fragmentmanager;import Android.os.bundle;import Android.support.v4.app.actionbardrawertoggle;import Android.support.v4.widget.drawerlayout;import Android.view.view;import Android.widget.adapterview;import Android.widget.arrayadapter;import Android.widget.listadapter;import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Activity {private string[] mplanettitles;// The name of each item of the ListView private Drawerlayout mdrawerlayout;private ListView mdrawerlist;private Actionbardrawertoggle Mdrawertoggle; Used to listen for drawerlayout event @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mplanettitles = Getresources (). Getstringarray (R.array.planets_array); Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout); mdrawerlist = (ListView) Findviewbyid (R.id.left_ drawer);//For LISTVIEW joins the adapter Mdrawerlist.setadapter ((listadapter) New arrayadapter<string> (this,android. R.layout.simple_list_item_1, Mplanettitles));//Monitor the ListView's Click event Mdrawerlist.setonitemclicklistener (new Draweritemclicklistener ()); mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout);// Listen for Drawerlayout listener events Mdrawertoggle = new Actionbardrawertoggle (this, Mdrawerlayout,r.drawable.ic_launcher, R.string.app_name, r.string.app_name) {public void ondrawerclosed (view view) {super.ondrawerclosed (view); Toast.maketext (mainactivity.this, "drawer closed", Toast.length_short). Show (); public void ondraweropened (View drawerview) {super.ondraweropened (Drawerview); Toast.maketext (mainactivity.this, "drawer open", Toast.length_short). Show ();}}; Mdrawerlayout.setdrawerlistener (Mdrawertoggle);} Private class Draweritemclicklistener Implementslistview.onitemclicklistener {@Overridepublic void Onitemclick ( adapterview<?

> Parent, view view, int Position,long ID) {//Toggle interface layout According to the selected option of the ListView fragment fragment = new fragment (); Fragmentmanager Fragmentmanager = Getfragmentmanager (); Fragmentmanager.begintransaction (). Replace (R.id.content_ frame, fragment). commit (); mdrawerlist.setitemchecked (position, true);//Set the caption Getactionbar () to the Action Bar. Settitle ( Mplanettitles[position]); Mdrawerlayout.closedrawer (mdrawerlist);}}




Android development of the Navigationdrawer navigation drawer function implementation (source sharing)

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.