Use of Android slide menu DrawerLayout and android slide menu

Source: Internet
Author: User

Use of Android slide menu DrawerLayout and android slide menu


Currently, many slide menus are used, mostly through SlidingMenu. You can also use DrawerLayout

Create drawer Layout


Frament_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>
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>

Create a new class to inherit the Fragment class

/*** ContentFragment. java * copyright (C) 2015 * Creator: cuiran 3:25:44 */package com. cayden. drawerlayoutdemo; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. textView;/*** TODO * @ author cuiran * @ version 1.0.0 */public class ContentFragment extends Fragment {private TextView textView; @ Overridepublic 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); String text = getArguments (). getString ("text"); textView. setText (text); return view ;}}

Complete Activity code

Package com. cayden. drawerlayoutdemo; 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; impor T 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; private ListView mDrawerList; private ArrayList <String> menuLists; private ArrayAdapter <S Tring> adapter; private ActionBarDrawerToggle mDrawerToggle; private String mTitle; @ Overrideprotected 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 ("menu 0" + I); adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, menuLists); mDrawerList. setAdapter (adapter); mDrawerList. setOnItemClickListener (this); mDrawerToggle = new ActionBarDrawerToggle (this, mDrawerLayout, R. drawable. ic_drawer, R. string. drawer_open, R. string. drawer_close) {@ Overridepublic void onDrawerOpened (View drawerView) {sup Er. onDrawerOpened (drawerView); getActionBar (). setTitle ("select"); invalidateOptionsMenu (); // Call onPrepareOptionsMenu ()} @ Overridepublic void onDrawerClosed (View drawerView) {super. onDrawerClosed (drawerView); getActionBar (). setTitle (mTitle); invalidateOptionsMenu () ;}}; mDrawerLayout. setDrawerListener (mDrawerToggle); // enable the getActionBar () function of the app icon on the ActionBar (). setDisplayHomeAsUpEnabled (true); getActionBar (). set HomeButtonEnabled (true) ;}@ Overridepublic boolean invoke (Menu menu) {boolean isDrawerOpen = mDrawerLayout. isDrawerOpen (mDrawerList); menu. findItem (R. id. action_websearch). setVisible (! IsDrawerOpen); return super. onPrepareOptionsMenu (menu) ;}@ Overridepublic 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 ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// combine the icon on the ActionBar with the Drawer if (mDrawerToggle. onOptionsItemSelected (item) {return true;} switch (ite M. getItemId () {case R. id. action_websearch: Intent intent = new Intent (); intent. setAction ("android. intent. action. VIEW "); Uri uri = Uri. parse ("http://www.baidu.com"); intent. setData (uri); startActivity (intent); break;} return super. onOptionsItemSelected (item) ;}@ Overrideprotected void onPostCreate (Bundle savedInstanceState) {super. onPostCreate (savedInstanceState); // you need to synchronize the status of ActionDrawerToggle with DrawerLayout/ /Set the drawer icon in ActionBarDrawerToggle to IconmDrawerToggle of Home-Button in ActionBar. syncState () ;}@ Overridepublic void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); mDrawerToggle. onConfigurationChanged (newConfig) ;}@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3) {// insert a Fragment to FrameLayout. Fragment contentFragment = new ContentFragment (); Bundle args = new Bundle (); args. putString ("text", menuLists. get (position); contentFragment. setArguments (args); FragmentManager fm = getFragmentManager (); fm. beginTransaction (). replace (R. id. content_frame, contentFragment ). commit (); 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.