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 );
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // The action bar home/up action should open or close the drawer.
- // ActionBarDrawerToggle will take care of this.
- Log.v("haha",""+item.getItemId());
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action buttons
- switch(item.getItemId()) {
- case R.id.action_settings:
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
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.
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- mDrawerToggle.syncState();
- }
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:
- package com.example.dolby.drawernavi;
- import android.app.ActionBar;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.content.res.Configuration;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- public class MainActivity extends ActionBarActivity {
- private String[] mPlanetTitles = {"list1","list2","list3"};
- private DrawerLayout mDrawerLayout;
- private ListView mDrawerList;
- private Button drawbtn;
- private ActionBarDrawerToggle mDrawerToggle;
- @Override
- protected 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);
- mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
- // Set the drawer toggle as the DrawerListener
- mDrawerLayout.setDrawerListener(mDrawerToggle);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setHomeButtonEnabled(true);
- // enable ActionBar app icon to behave as action to toggle nav drawer
- //getActionBar().setDisplayHomeAsUpEnabled(true);
- // Set the adapter for the list view
- mDrawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1
- , mPlanetTitles));
- // Set the list's click listener
- mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
- }
- private class DrawerItemClickListener implements ListView.OnItemClickListener
- {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- selectItem(position);
- }
- }
- private void selectItem(int position)
- {
- Fragment fragment = new PlanetFragment();
- // Insert the fragment by replacing any existing fragment
- FragmentManager fragmentManager = getFragmentManager();
- fragmentManager.beginTransaction()
- .replace(R.id.content_frame, fragment)
- .commit();
- // Highlight the selected item, update the title, and close the drawer
- mDrawerList.setItemChecked(position, true);
- setTitle(mPlanetTitles[position]);
- mDrawerLayout.closeDrawer(mDrawerList);
- }
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- mDrawerToggle.syncState();
- }
- // @Override
- // public void onConfigurationChanged(Configuration newConfig) {
- // super.onConfigurationChanged(newConfig);
- // mDrawerToggle.onConfigurationChanged(newConfig);
- // }
- // @Override
- // public boolean onCreateOptionsMenu(Menu menu) {
- // MenuInflater inflater = getMenuInflater();
- // inflater.inflate(R.menu.menu_main,menu);
- // return super.onCreateOptionsMenu(menu);
- // }
- // @Override
- // public boolean onPrepareOptionsMenu(Menu menu) {
- //// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
- //// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
- //// return super.onPrepareOptionsMenu(menu);
- // return true;
- // }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // The action bar home/up action should open or close the drawer.
- // ActionBarDrawerToggle will take care of this.
- Log.v("haha",""+item.getItemId());
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action buttons
- switch(item.getItemId()) {
- case R.id.action_settings:
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- }
- <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">
- <!--<Button-->
- <!--android:id="@+id/btn"-->
- <!--android:layout_width="match_parent"-->
- <!--android:layout_height="wrap_content"-->
- <!--android:text="open"-->
- <!--/>-->
- </FrameLayout>
- <!-- The navigation drawer -->
- <ListView android:id="@+id/left_drawer"
- android:layout_width="240dp"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- android:choiceMode="singleChoice"
- android:divider="@android:color/transparent"
- android:dividerHeight="0dp"
- android:background="#FFFFFF"/>
- </android.support.v4.widget.DrawerLayout>