1. Create a drawer Layout
UseDrawerLayout
As the interface root control. In drawerlayout, the first view is the main content of the current interface, and the second and third views are the drawer menu content. If the current interface only needs one drawer menu, the third view can be omitted.
In the following example, drawerlayout contains two views. The first framelayout is the display area of the main content of the current interface, and the second listview is the drawer menu content.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"><FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent" ></FrameLayout> <ListView android:id="@+id/listview" android:layout_width="240dp" android:layout_height="match_parent" android:background="#FFFFFF" android:choiceMode="singleChoice" android:layout_gravity="start" /></android.support.v4.widget.DrawerLayout>
Note the following points in the above Code:
3. Enable and disable the event in the listener menu.
You must call
Of the drawerlayout class
setDrawerListener()
Function. The parameter is
Implementation of the drawerlayout. drawerlistener interface.
This interface provides callback functions for events such as opening and closing menus. For example
onDrawerOpened()
AndonDrawerClosed()
.
If your activity uses
Action bar, you can useActionBarDrawerToggle
Class, which implements
Drawerlayout. drawerlistener interface, and you can rewrite related functions as needed. This class implements menu and action bar related operations.
According to
The introduction in the navigation drawer Design Guide. When the menu is displayed, you should hide the functional menu on the actionbar as needed and modify the title of the actionbar.
Key code:
package com.tao.drawerlayout;import android.os.Bundle;import android.support.v4.app.ActionBarDrawerToggle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.support.v4.widget.DrawerLayout;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends FragmentActivity { ListView myListview; DrawerLayout drawerLayout; List<String> list; CharSequence title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myListview = (ListView)findViewById(R.id.listview); drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); list = new ArrayList<String>(); for (int i = 0;i<10;i++){ list.add("item"+(i+1)); } ListAdapter listAdapter = new ListAdapter(this,list); myListview.setAdapter(listAdapter); drawerLayout.setDrawerListener(new ActionBarDrawerToggle(this,drawerLayout,R.drawable.ic_launcher,R.string.open,R.string.close){ @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getActionBar().setTitle(title); invalidateOptionsMenu(); } }); myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { MyFragment myFragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("content",list.get(i)); myFragment.setArguments(bundle); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame_layout,myFragment).commit(); title = list.get(i); myListview.setItemChecked(i,true); drawerLayout.closeDrawer(myListview); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
This article references the article on cloud qiofeng. Leave me a message if you need code.