Android Drawer Menu Drawerlayout Implementation case

Source: Internet
Author: User

(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 -    <framelayoutandroid: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" >                <TextViewandroid:id= "@+id/textview"android:layout_width="Match_ Parent "android:layout_height=" Wrap_content "android:textsize=" 25SP " / >                                </linearlayout>

(2) Main class files

 PackageCom.xuliugen.drawerlayout;ImportJava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.app.Fragment;ImportAndroid.app.FragmentManager;ImportAndroid.content.Intent;ImportAndroid.content.res.Configuration;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.support.v4.app.ActionBarDrawerToggle;ImportAndroid.support.v4.widget.DrawerLayout;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public  class mainactivity extends Activity  implements  Onitemclicklistener {    PrivateDrawerlayout mdrawerlayout;//Set the left side of the drawer menu    PrivateListView mdrawerlist;PrivateArraylist<string> menulists;PrivateArrayadapter<string> adapter;PrivateActionbardrawertoggle Mdrawertoggle;//Actionbar turn off the    PrivateString 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 =NewArraylist<string> (); for(inti =0; I <5; i++) {Menulists.add ("Item"+ i); }//Initialize adapteradapter =NewArrayadapter<string> ( ThisAndroid. R.layout.simple_list_item_1, menulists);//Set the data for the left drawerMdrawerlist.setadapter (adapter);///left swipe menu to listen for eventsMdrawerlist.setonitemclicklistener ( This);//Set drawer to be opened closed objectMdrawertoggle =NewActionbardrawertoggle ( This,//Mdrawerlayout,//R.drawable.ic_drawer,//R.string.drawer_open,//R.string.drawer_close) {//When it is opened            @Override             Public void ondraweropened(View Drawerview) {Super. ondraweropened (Drawerview); Getactionbar (). Settitle ("Please select");//Set Actionbar textInvalidateoptionsmenu ();//Call Onprepareoptionsmenu ()}//was closed when            @Override             Public void ondrawerclosed(View Drawerview) {Super. ondrawerclosed (Drawerview);                Getactionbar (). Settitle (Mtitle); Invalidateoptionsmenu ();//Redraw the menu item above the Actionbar}        };//Set open Close event for sliding menuMdrawerlayout.setdrawerlistener (Mdrawertoggle);//Turn on the app icon on Actionbar: Click Open and click Close 7Getactionbar (). setdisplayhomeasupenabled (true); Getactionbar (). sethomebuttonenabled (true); }@Override     Public Boolean Onprepareoptionsmenu(Menu menu) {BooleanIsdraweropen = Mdrawerlayout.isdraweropen (mdrawerlist); Menu.finditem (R.id.action_websearch). setvisible (!isdraweropen);return Super. Onprepareoptionsmenu (menu); }/** * menu item Settings * /    @Override     Public Boolean Oncreateoptionsmenu(Menu menu) {Getmenuinflater (). Inflate (R.menu.main, menu);return true; }/** * Set the Click event for Actionbar top icon * /    @Override     Public Boolean onoptionsitemselected(MenuItem Item) {//Combine the icons on the Actionbar with the drawer .        if(mdrawertoggle.onoptionsitemselected (item)) {return true; }Switch(Item.getitemid ()) { CaseR.id.action_websearch:intent Intent =NewIntent (); 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); }/** * Information according to official documentation * * will be mdrawertoggle.syncstate (); put into onpostcreate * *    @Override    protected void onpostcreate(Bundle savedinstancestate) {Super. Onpostcreate (Savedinstancestate);//need to synchronize the status of Actiondrawertoggle with Drawerlayout        //Set the drawer icon in the Actionbardrawertoggle to Home-button in ActionbarMdrawertoggle.syncstate (); }/** * The corresponding settings are required when the screen is selected.    @Override     Public void onconfigurationchanged(Configuration newconfig) {Super. onconfigurationchanged (Newconfig);    Mdrawertoggle.onconfigurationchanged (Newconfig); }/** * Monitor Event Implementation * * When clicking on item in the menu bar, toggle the corresponding fragment interface */    @Override     Public void Onitemclick(adapterview<?> arg0, View arg1,intPositionLongARG3) {//Dynamically insert a fragment into the FramelayoutFragment contentfragment =NewContentfragment (); Bundle bundle =NewBundle (); Bundle.putstring ("Text", Menulists.get (position)); Contentfragment.setarguments (bundle);///fragment need to be handed to Fragmentmanager to replace the corresponding view after it is createdFragmentmanager fm = Getfragmentmanager ();        Fm.begintransaction (). replace (R.id.content_frame, contentfragment). commit ();    Mdrawerlayout.closedrawer (mdrawerlist); }}

Contentfragment.java

 PackageCom.xuliugen.drawerlayout;ImportAndroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;/** * For filling the interface fragment * * @author Xuliugen * */ Public  class contentfragment extends Fragment {    PrivateTextView TextView;@Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_content, container,false); TextView = (TextView) View.findviewbyid (R.id.textview);//Get the parameters passed inString text = Getarguments (). GetString ("Text"); Textview.settext (text);returnView }}

(3) Project Presentation effect

(4) Project source code and Google Reference document download: Http://yunpan.cn/cZZ7RVRY96yWe (extract code: 2981)

Android Drawer Menu Drawerlayout Implementation case

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.