Implementation case of Android drawer menu DrawerLayout, android custom layout
(1) project layout File
Activity_main.xml
<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" > </FrameLayout> <!-- The navigation view --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffcc" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" > </ListView></android.support.v4.widget.DrawerLayout>
Fragment_content.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" /></LinearLayout>
(2) Main class files
Package com. xuliugen. drawerlayout; import java. util. arrayList; import android. app. activity; import android. app. fragment; import android. app. fragmentManager; import android. content. intent; import android. content. res. configuration; import android.net. uri; import android. OS. bundle; import android. support. v4.app. actionBarDrawerToggle; import android. support. v4.widget. drawerLayout; import android. view. menu; import Android. view. menuItem; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listView; public class MainActivity extends Activity implements OnItemClickListener {private DrawerLayout mDrawerLayout; // set the left-side drawer menu private ListView mDrawerList; private ArrayList <String> menuLists; priva Te ArrayAdapter <String> adapter; private ActionBarDrawerToggle mDrawerToggle; // actionBar open closed private String mTitle; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTitle = (String) getTitle (); mDrawerLayout = (DrawerLayout) findViewById (R. id. drawer_layout); mDrawerList = (ListView) findViewById (R. id. left _ Drawer); menuLists = new ArrayList <String> (); for (int I = 0; I <5; I ++) {menuLists. add ("item" + I);} // initialize adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, menuLists); // set the data mDrawerList for the left-side drawer. setAdapter (adapter); // The listening event mDrawerList in the left-side sliding menu. setOnItemClickListener (this); // set the mDrawerToggle = new ActionBarDrawerToggle (this, // mDrawerLayout, // R. draw Able. ic_drawer, // R. string. drawer_open, // R. string. drawer_close) {// @ Override public void onDrawerOpened (View drawerView) {super. onDrawerOpened (drawerView); getActionBar (). setTitle ("select"); // set the invalidateOptionsMenu () of the actionBar text; // Call onPrepareOptionsMenu ()} // @ Override public void onDrawerClosed (View drawerView) when it is disabled) {super. onDrawerClosed (drawerView); getActionBar (). setTitle (m Title); invalidateOptionsMenu (); // re-draw menu items on the top of the actionBar }}; // set the opening and closing event mDrawerLayout of the sliding menu. setDrawerListener (mDrawerToggle); // enable the app icon function on the ActionBar: Click Open and click Close 7 getActionBar (). setDisplayHomeAsUpEnabled (true); getActionBar (). setHomeButtonEnabled (true) ;}@ Override public boolean onPrepareOptionsMenu (Menu menu) {boolean isDrawerOpen = mDrawerLayout. isDrawerOpen (mDrawerList); menu. findItem (R. id. a Ction_websearch). setVisible (! IsDrawerOpen); return super. onPrepareOptionsMenu (menu);}/*** Menu item setting */@ Override public boolean onCreateOptionsMenu (menu) {getMenuInflater (). inflate (R. menu. main, menu); return true;}/*** set the Click Event of the icon above the actionBar */@ Override public boolean onOptionsItemSelected (MenuItem item) {// combine the icon on the ActionBar with the Drawer if (mDrawerToggle. onOptionsItemSelected (item) {return true;} switch (item. getItemI D () {case R. id. action_websearch: Intent intent = new Intent (); intent. setAction ("android. intent. action. VIEW "); Uri uri = Uri. parse ("http://blog.csdn.net/xlgen157387"); intent. setData (uri); startActivity (intent); break;} return super. onOptionsItemSelected (item);}/*** according to the information prompted in the official documentation. syncState (); Put It In onPostCreate */@ Override protected void onPostCreate (Bundle savedInstanceState) {Super. onPostCreate (savedInstanceState); // synchronize the status of ActionDrawerToggle with DrawerLayout // set the drawer Icon in ActionBarDrawerToggle to the Icon mDrawerToggle of Home-Button in ActionBar. syncState ();}/*** when the screen is selected, you also need to set the corresponding */@ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); mDrawerToggle. onConfigurationChanged (newConfig);}/*** listener event implementation *** Switch the corresponding fragment interface */@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3) {// insert a Fragment to FrameLayout. Fragment contentFragment = new ContentFragment (); Bundle bundle = new Bundle (); bundle. putString ("text", menuLists. get (position); contentFragment. setArguments (bundle); // After fragment is created, it must be handed over to fragmentManager to replace it with the corresponding view. FragmentManager fm = getFragmentManager (); fm. beginTransaction (). replace (R. id. content_frame, contentFragment ). commit (); mDrawerLayout. closeDrawer (mDrawerList );}}
ContentFragment. java
Package com. xuliugen. drawerlayout; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. textView;/*** fragment used to fill the interface ** @ author xuliugen **/public class ContentFragment extends Fragment {private TextView textView; @ Override public View onCreateView (LayoutInflater inflater, viewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. fragment_content, container, false); textView = (TextView) view. findViewById (R. id. textView); // obtain the input parameter String text = getArguments (). getString ("text"); textView. setText (text); return view ;}}
(3) project demonstration effect
(4) project source code and Google reference documentation download: http://yunpan.cn/cZZ7RVRY96yWe (extraction code: 2981)