Implementation of Navigationdrawer navigation drawer function in Android Development (source code sharing)

Source: Internet
Author: User

Navigationdrawer is a sliding panel from the left of the screen to display the main navigation items of the application. You can open the navigation drawer by sliding into the left edge of the screen or touching the application icon in the action bar. The navigation drawer overwrites the content but does not overwrites the operation bar. When the navigation drawer is fully open, the title of the Operation bar should be replaced with the application name, instead of displaying the name of the current view, and all operation buttons related to the current view should be closed. The "more operations" menu button in the Operation bar does not need to be closed, so that users can access "Settings" and "help" at any time ". Next we will implement the navigation drawer function.

Layout File Code

    
     <frameLayout        android:id="@+id/content_frame"        android:layout_width="match_parent"        android:layout_height="match_parent"                     />    
     
 
Note:

(1) The main view (FrameLayout above) must be the first child in DrawerLayout, Because XML means the z sequence and the content required by the drawer.

(2) The main content view must be set to match the width and height of the parent view, because it represents hidden throughout the UI navigation drawer.

(3) The drawer view (the ListView above) must specify its horizontal gravity and the android: layout_gravity attribute. Supports the right-to-left language (RTL), specifying the value and "start" instead of "Left" (the layout is RTL in the right drawer ).

(4) The drawer view specifies the width of the dp unit and the height of the parent view. The drawer width should not exceed 320 dp so the user can always see the main part of the content.

(5) DrawerLayout must be the root node of the layout.

MainActivity 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 in listView is private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; // used to listen to the 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); // Add the adapter mDrawerList for ListView. setAdapter (ListAdapter) new ArrayAdapter
 
  
(This, android. r. layout. simple_list_item_1, mPlanetTitles); // listens to the ListView Click Event mDrawerList. setOnItemClickListener (new DrawerItemClickListener (); mDrawerLayout = (DrawerLayout) findViewById (R. id. drawer_layout); // listens to the DrawerLayout listening event 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, "the drawer is closed", Toast. LENGTH_SHORT ). show ();} public void onDrawerOpened (View drawerView) {super. onDrawerOpened (drawerView); Toast. makeText (MainActivity. this, "the drawer is open", Toast. LENGTH_SHORT ). show () ;}}; mDrawerLayout. setDrawerListener (mDrawerToggle);} private class DrawerItemClickListener implementsListView. onItemClickListener {@ Overridepublic void onItemClick (AdapterView
  Parent, View view, int position, long id) {// switch the Fragment fragment = new Fragment (); FragmentManager fragmentManager = getFragmentManager () based on the selected options of ListView (); fragmentManager. beginTransaction (). replace (R. id. content_frame, fragment ). commit (); mDrawerList. setItemChecked (position, true); // set the title getActionBar () for the Operation bar (). setTitle (mPlanetTitles [position]); mDrawerLayout. closeDrawer (mDrawerList );}}}
 



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.