Android Open Source Project Slidingmenu This study note (two)

Source: Internet
Author: User

We have introduced Slidingmenu use: Android Open source project Slidingmenu This study note (one), then go deep into the study. Switch to the appropriate page based on the menu of the slide out

Folder structure:


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgvuzzb6agfvdgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Click Bluetooth to switch to the corresponding interface

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgvuzzb6agfvdgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Key code

Mainactivity.java

Package Com.dzt.slidingmenudemo;import Android.app.fragment;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.util.log;import Com.dzt.slidingmenudemo.fragment.bluetoothfragment;import com.dzt.slidingmenudemo.fragment.DisplayFragment; Import Com.dzt.slidingmenudemo.fragment.homefragment;import Com.dzt.slidingmenudemo.fragment.menufragment;import Com.dzt.slidingmenudemo.fragment.wififragment;import Com.dzt.slidingmenudemo.fragment.menufragment.onmenulistonitemclicklistener;import Com.slidingmenu.lib.slidingmenu;import com.slidingmenu.lib.app.slidingactivity;/** * Switch to the corresponding fragment based on different menu * @ Author Administrator * */public class Mainactivity extends Slidingactivity implementsonmenulistonitemclicklistener { Private Slidingmenu mchannelmenu; @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate);//Set title bar title Settitle ("Test Slidingmenu"); Setcontentview (r.layout.cOntent_frame); Initchannelmenu ();} private void Initchannelmenu () {Setbehindcontentview (r.layout.menu_frame);//Create Slidingmenu Object Mchannelmenu = Getslidingmenu ();//Set the side slider menu position, here on the left. When the menu is pulled. Will eject Mchannelmenu.setmode (Slidingmenu.left) from the left,///Set the range of the touch, here set full screen mchannelmenu.settouchmodeabove ( Slidingmenu.touchmode_fullscreen);//Set the width of the shadow, look at the second one above, on the right, there is a shadow transition. This is the thing Mchannelmenu.setshadowwidthres (r.dimen.shadow_width);//Here is the shadow effect, Ability to set picture or a color transition mchannelmenu.setshadowdrawable (R.drawable.shadow);//Set back spacing, The side slide bar and the original interface spacing Mchannelmenu.setbehindoffsetres (r.dimen.slidingmenu_offset);//Border angle, This refers to the border place Mchannelmenu.setfadedegree (0.35f);//sets the mode mchannelmenu.settouchmodeabove of the touch screen (slidingmenu.touchmode_ fullscreen);//Set slidingmenu content fragmenttransaction fragmenttransaction = Getfragmentmanager (). BeginTransaction (); Menufragment menufragment = new Menufragment (); Menufragment.setonmenulistonitemclicklistener (this); Fragmenttransaction.replace (R.id.menu_frame, menufragment); Fragmenttransaction.replace (R.id.content_frame, new HomefRagment ()); Fragmenttransaction.commit ();} @Overridepublic void onbackpressed () {if (mchannelmenu.ismenushowing ()) {//Hide slidingmenu, The content here is our Lord Activitymchannelmenu.showcontent ();} else {super.onbackpressed ();}} @Overridepublic void Onselectitem (int position, String title) {//TODO auto-generated method StubSystem.out.println (" Onselectitem title = "+ title"; Fragment Fragment = Null;switch (position) {Case 0:fragment = new Homefragment (); Break;case 1:fragment = new Wififragment ( ); Break;case 2:fragment = new Bluetoothfragment (); Break;case 3:fragment = new Displayfragment (); break;default:break;} if (fragment! = null) {Fragmentmanager Fragmentmanager = Getfragmentmanager (); Fragmentmanager.begintransaction (). Replace (R.id.content_frame, fragment). commit ();//Update selected item and title, then close the Drawersettitle (title); Mchannelmenu.showcontent ();} else {//Error in creating FRAGMENTLOG.E ("Mainactivity", "Error in creating Fragment");}}}
Slide out of the fragment Menufragment.java

Package Com.dzt.slidingmenudemo.fragment;import Java.util.arraylist;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import Com.dzt.slidingmenudemo.menulistadapter;import Com.dzt.slidingmenudemo.R;import Com.dzt.slidingmenudemo.domain.menuitem;public class Menufragment extends Fragment implements Onitemclicklistener { Private ListView mlv;private string[] mmenutitles;private arraylist<menuitem> mmenuitem;private MenuListAdapter Madapter;private onmenulistonitemclicklistener mlistener;public View oncreateview (layoutinflater inflater, ViewGroup Container,bundle savedinstancestate) {View Rootview = inflater.inflate (r.layout.menu_fragment, Container,false); Initwidgets (Rootview); return rootview;} private void Initwidgets (View rootview) {mLv = (ListView) Rootview.findviewbyid (R.Id.left_menu); mmenutitles = Getresources (). Getstringarray (r.array.menu_title); mmenuitem = new ArrayList<MenuItem > (); for (int i = 0; i < mmenutitles.length; i++) {MenuItem item = null;if (i = = 2) {item = new MenuItem (mmenutitles[ I], r.drawable.ic_launcher, "+", True);} else if (i = = 4) {item = new MenuItem (Mmenutitles[i], R.drawable.ic_launcher, "Max", True);} else {item = new MenuItem (mmenu Titles[i], r.drawable.ic_launcher);} Mmenuitem.add (item);} Madapter = new Menulistadapter (getactivity (), Mmenuitem); Mlv.setadapter (Madapter); Mlv.setonitemclicklistener (This) ;} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {//TODO auto-generated met Hod stubmlv.setitemchecked (position, true); mlv.setselection (position); if (mlistener! = null) {Mlistener.onselectitem (position, mmenutitles[position]);}} public void Setonmenulistonitemclicklistener (Onmenulistonitemclicklistener listener) {Mlistener = listener;} public interface OnmenulistonitemclicklisteneR {public void Onselectitem (int position, String title);}} 
After you click menu. A click Listener is implemented in the mainactivity. It will switch to the corresponding page.

Related code: http://download.csdn.net/detail/deng0zhaotai/7862727

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android Open Source Project Slidingmenu This study note (two)

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.