Android Open Source project Slidingmenu's learning Notes (ii)

Source: Internet
Author: User

The use of Slidingmenu has been described in the previous section: Android Open source project Slidingmenu's learning Notes (a), then further study, according to the menu of the slide show to switch to the corresponding page

Directory structure:




Click Bluetooth to switch to the appropriate interface


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 according to the 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 of title bar Settitle ("Test Slidingmenu"); Setcontentview (r.layout.cOntent_frame); Initchannelmenu ();} private void Initchannelmenu () {Setbehindcontentview (r.layout.menu_frame);//Create Slidingmenu Object Mchannelmenu = Getslidingmenu ();//Set the slide-bar menu position, here on the left. When the menu is pulled, the Mchannelmenu.setmode (Slidingmenu.left) is ejected from the left, or the range of the touch is set, where the full screen mchannelmenu.settouchmodeabove is set ( 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, You can set the picture or a color transition mchannelmenu.setshadowdrawable (R.drawable.shadow);//Set the back spacing, Slide bar and 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 clicking on menu, the listener that implements the click in mainactivity will switch to the corresponding page.

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

Android Open Source project Slidingmenu's learning Notes (ii)

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.