Android -- DrawLayout navigation drawer

Source: Internet
Author: User

Android -- DrawLayout navigation drawer
The implementation of DrawLayout is detailed in the official documentation. However, during the development process, it is still a bit problematic. The official documentation is a bit old. mDrawerToggle = new ActionBarDrawerToggle (this, mDrawerLayout, r. string. open, R. string. close); the original api of this method can set the icon image of the app. Now this parameter is missing.
For the ActionBarDrawerToggle class, the actionbar and drawlayout can be combined well. By using ActionBarDrawerToggle, you can click the app icon to open the drawer.
Mainly needs to be implemented:
Getsuppactionactionbar (). setDisplayHomeAsUpEnabled (true );
Getsuppactionactionbar (). setHomeButtonEnabled (true );

 
 
  1. @Override
  2. public boolean onOptionsItemSelected(MenuItem item) {
  3. // The action bar home/up action should open or close the drawer.
  4. // ActionBarDrawerToggle will take care of this.
  5. Log.v("haha",""+item.getItemId());
  6. if (mDrawerToggle.onOptionsItemSelected(item)) {
  7. return true;
  8. }
  9. // Handle action buttons
  10. switch(item.getItemId()) {
  11. case R.id.action_settings:

  12. return true;
  13. default:
  14. return super.onOptionsItemSelected(item);
  15. }
  16. }
This method has been reloaded in the main activity. The operation on the actionbar item can be passed to ActionBarDrawerToggle for processing. what I know now is to process the click action of the actionbar appicon.


 
 
  1. @Override
  2. protected void onPostCreate(Bundle savedInstanceState) {
  3. super.onPostCreate(savedInstanceState);
  4. mDrawerToggle.syncState();
  5. }
The syncState of the overload call of this method is to keep the actionBar and ActionBarDrawerToggle in the same State. This function is a method in the Activity life cycle and will only be called once.

Main Code:

 
 
  1. package com.example.dolby.drawernavi;

  2. import android.app.ActionBar;
  3. import android.app.Fragment;
  4. import android.app.FragmentManager;
  5. import android.content.res.Configuration;
  6. import android.support.v4.widget.DrawerLayout;
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.os.Bundle;
  9. import android.support.v7.app.ActionBarDrawerToggle;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.Button;
  18. import android.widget.ListView;


  19. public class MainActivity extends ActionBarActivity {
  20. private String[] mPlanetTitles = {"list1","list2","list3"};
  21. private DrawerLayout mDrawerLayout;
  22. private ListView mDrawerList;
  23. private Button drawbtn;
  24. private ActionBarDrawerToggle mDrawerToggle;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. //mPlanetTitles = getResources().getStringArray(R.array.planets_array);
  30. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  31. mDrawerList = (ListView) findViewById(R.id.left_drawer);
  32. mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
  33. // Set the drawer toggle as the DrawerListener
  34. mDrawerLayout.setDrawerListener(mDrawerToggle);
  35. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  36. getSupportActionBar().setHomeButtonEnabled(true);

  37. // enable ActionBar app icon to behave as action to toggle nav drawer
  38. //getActionBar().setDisplayHomeAsUpEnabled(true);
  39. // Set the adapter for the list view
  40. mDrawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1
  41. , mPlanetTitles));
  42. // Set the list's click listener
  43. mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  44. }

  45. private class DrawerItemClickListener implements ListView.OnItemClickListener
  46. {

  47. @Override
  48. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  49. selectItem(position);
  50. }
  51. }

  52. private void selectItem(int position)
  53. {
  54. Fragment fragment = new PlanetFragment();

  55. // Insert the fragment by replacing any existing fragment
  56. FragmentManager fragmentManager = getFragmentManager();
  57. fragmentManager.beginTransaction()
  58. .replace(R.id.content_frame, fragment)
  59. .commit();

  60. // Highlight the selected item, update the title, and close the drawer
  61. mDrawerList.setItemChecked(position, true);
  62. setTitle(mPlanetTitles[position]);
  63. mDrawerLayout.closeDrawer(mDrawerList);
  64. }


  65. @Override
  66. protected void onPostCreate(Bundle savedInstanceState) {
  67. super.onPostCreate(savedInstanceState);
  68. mDrawerToggle.syncState();
  69. }

  70. // @Override
  71. // public void onConfigurationChanged(Configuration newConfig) {
  72. // super.onConfigurationChanged(newConfig);
  73. // mDrawerToggle.onConfigurationChanged(newConfig);
  74. // }

  75. // @Override
  76. // public boolean onCreateOptionsMenu(Menu menu) {
  77. // MenuInflater inflater = getMenuInflater();
  78. // inflater.inflate(R.menu.menu_main,menu);
  79. // return super.onCreateOptionsMenu(menu);
  80. // }

  81. // @Override
  82. // public boolean onPrepareOptionsMenu(Menu menu) {
  83. //// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
  84. //// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
  85. //// return super.onPrepareOptionsMenu(menu);
  86. // return true;
  87. // }

  88. @Override
  89. public boolean onOptionsItemSelected(MenuItem item) {
  90. // The action bar home/up action should open or close the drawer.
  91. // ActionBarDrawerToggle will take care of this.
  92. Log.v("haha",""+item.getItemId());
  93. if (mDrawerToggle.onOptionsItemSelected(item)) {
  94. return true;
  95. }
  96. // Handle action buttons
  97. switch(item.getItemId()) {
  98. case R.id.action_settings:

  99. return true;
  100. default:
  101. return super.onOptionsItemSelected(item);
  102. }
  103. }


  104. }

 
 
  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/drawer_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <!-- The main content view -->
  7. <FrameLayout
  8. android:id="@+id/content_frame"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent">
  11. <!--<Button-->
  12. <!--android:id="@+id/btn"-->
  13. <!--android:layout_width="match_parent"-->
  14. <!--android:layout_height="wrap_content"-->
  15. <!--android:text="open"-->
  16. <!--/>-->
  17. </FrameLayout>

  18. <!-- The navigation drawer -->
  19. <ListView android:id="@+id/left_drawer"
  20. android:layout_width="240dp"
  21. android:layout_height="match_parent"
  22. android:layout_gravity="start"
  23. android:choiceMode="singleChoice"
  24. android:divider="@android:color/transparent"
  25. android:dividerHeight="0dp"
  26. android:background="#FFFFFF"/>
  27. </android.support.v4.widget.DrawerLayout>





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.