Android Slidingmenu Use detailed

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/36677279

Many apps have the ability to slide the menu, some apps are side-slip menu ~slidingmenu This open source project can help us to realize the slide-off function, if the slidingmenu is not very familiar with children's shoes, you can refer to this blog. The way to introduce the side-slip menu to the project many, this blog first through the introduction of various methods of introduction, and then show you an example: the main layout of the Viewpager, left and right each side of the use of a sliding menu, almost has been able to meet the needs of most applications. About common properties, described at the end of the article.

1, in the activity through the Slidingmenu construction method, directly set the side-slip menu

Package Com.zhy.zhy_slidemenu_demo;import Android.app.activity;import Android.os.bundle;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;public class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /Configure the Slidingmenuslidingmenu menu = new Slidingmenu (this); Menu.setmode (slidingmenu.left);// Sets the mode of the touch screen Menu.settouchmodeabove (slidingmenu.touchmode_fullscreen); Menu.setshadowwidthres (R.dimen.shadow_width) ; menu.setshadowdrawable (R.drawable.shadow);//Set the width of the Sliding menu view menu.setbehindoffsetres (r.dimen.slidingmenu_offset);// Sets the value of the fade-out effect Menu.setfadedegree (0.35f);/** * Sliding_window would include the Title/actionbar in the content * section of the Slidingmenu, while Sliding_content does not. */menu.attachtoactivity (this, slidingmenu.sliding_content);//Set Layout Menu.setmenu (R.layout.leftmenu) for the side-slip menu;}}


is not particularly simple ~ a few lines of code to do ~ HA ~

2. By inheriting the activity slidingactivity

A, Inherit slidingactivity

b, then Setbehindcontentview in OnCreate (R.layout.leftmenu); Set the layout of the slide-by menu

C, get the Slidingmenu object through Getslidingmenu (), then set the style

Package Com.zhy.zhy_slidemenu_demo02;import Android.os.bundle;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity; public class Mainactivity extends slidingactivity{@Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Setbehindcontentview ( R.layout.leftmenu);//Configure the Slidingmenuslidingmenu menu = Getslidingmenu (); Menu.setmode (slidingmenu.left);// Sets the mode of the touch screen Menu.settouchmodeabove (slidingmenu.touchmode_fullscreen); Menu.setshadowwidthres (R.dimen.shadow_width) ; menu.setshadowdrawable (R.drawable.shadow);//Set the width of the Sliding menu view menu.setbehindoffsetres (r.dimen.slidingmenu_offset);// Sets the value of the fade-out effect Menu.setfadedegree (0.35f);/** * Sliding_window would include the Title/actionbar in the content * section of the Slidingmenu, while Sliding_content does not. *///menu.attachtoactivity (this, slidingmenu.sliding_content);//Menu.setmenu (R.layout.leftmenu);}}

Same as the first way ~ is also very simple ~ ~

3. Use Slidingmenu as a normal control

Slidingmenu can be used as a normal view, and then declared in the layout, the insane play ~ See an example:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/id_main_ly "android:layout_width=" Match_parent "android:layout_height = "Match_parent" > <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:layout_marginleft=" 30DP "android:layout_margintop=" 30DP "> <com.jeremyfeinstein.s Lidingmenu.lib.SlidingMenu xmlns:sliding= "Http://schemas.android.com/apk/res-auto" android:id= "@+id/ Slidingmenulayout "android:layout_width=" 120DP "android:layout_height=" 170DP "Android:bac Kground= "#ffffffff" sliding:behindoffset= "0DP" sliding:behindscrollscale= "1" sliding:fade Degree= "0.3" sliding:fadeenabled= "true" sliding:touchmodeabove= "fullscreen" Sliding:viewa Bove= "@layout/pic"/> </linearlayout></RelativeLayout> 

We slidingmenu the layout in the layout file as a normal view, and set the Viewabove value to another layout.

Look at the pic layout below:

<?xml version= "1.0" encoding= "Utf-8"? ><imageview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" 120DP "    android:layout_height=" 170DP "    android:src=" @drawable/zhy "/ >

is a picture of a sister.

Last look at the main activity:

Package Com.zhy.zhy_slidemenu_demo03;import Android.app.activity;import Android.os.bundle;import Android.view.View ; Import Android.view.view.onclicklistener;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;public class Mainactivity extends Activity{private slidingmenu mleftmenu; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); MLeftMenu = ( Slidingmenu) Findviewbyid (r.id.slidingmenulayout);//Configure the slidingmenu//slidingmenu menu = new Slidingmenu ( this); Mleftmenu.setmode (slidingmenu.left);//Set Touch screen mode mleftmenu.setshadowwidthres (r.dimen.shadow_width); Mleftmenu.setshadowdrawable (R.drawable.shadow); Mleftmenu.setmenu (R.layout.leftmenu); Mleftmenu.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (mleftmenu.ismenushowing ()) Mleftmenu.toggle ();}); /Set the width of the sliding menu view//Set the value of the fade-out effect/** * Sliding_window would include the Title/actionbar in the content * section of the Slidingmen U, while Sliding_content does not. */}}


You can also swipe to show anything, download, share a button, or whatever, by setting a slide show introduction to the picture by Slidingmenu. Many pictures of the time do not know how efficient, you can play, but do not recommend ha ~


4, Slidingmenu set left and right side slide Menu Example

The 3 methods described above, the events of the controls in the Slidingmenu layout need to be written in the activity, so the code is bloated, usually using fragment as the layout container for the side-slip menu.

Core code:

Fragment leftmenufragment = new Menuleftfragment (); Setbehindcontentview (R.layout.left_menu_frame); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.id_left_menu_frame, leftmenufragment). commit (); Slidingmenu menu = Getslidingmenu (); Menu.setmode (slidingmenu.left_right);//Set Touch screen mode Menu.settouchmodeabove ( Slidingmenu.touchmode_margin);

Let's go through the slide menu Setbehindcontentview (r.layout.left_menu_frame); set up a layout with only one framelayout in the layout, Then use Fragmentmanager to replace the fragment with this fragment so that the fragment is the layout of our slide-off menu, and our event-handling code can be written in fragement instead of in the activity.

Here's a look at the specific example:

The first is the left and right side of the two fragment:

Menuleftfragment

Package Com.zhy.zhy_slidemenu_demo04;import Java.util.arrays;import Java.util.list;import android.os.Bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.listadapter;import Android.widget.listview;public class Menuleftfragment extends Fragment{private View mview;private ListView mCategories ;p rivate list<string> mdatas = arrays.aslist ("chat", "Discovery", "Contacts", "Friends Circle", "subscription number");p rivate listadapter madapter;@ Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {if (MView = = NULL) {Initview (Inflater, container);} return MView;} private void Initview (Layoutinflater inflater, ViewGroup container) {MView = Inflater.inflate (R.layout.left_menu, container, false); mcategories = (ListView) Mview.findviewbyid (r.id.id_listview_categories); madapter = new Arrayadapter<string> (Getactivity (), Android. R.layout.simple_liSt_item_1, Mdatas); Mcategories.setadapter (Madapter);}} 

The left side of the slide layout is a ListView, the code is relatively simple ~

Menurightfragment

Package Com.zhy.zhy_slidemenu_demo04;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class MenuRightFragment Extends Fragment{private view MView; @Overridepublic view Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {if (MView = = null) {MView = Inflater.inflate (R.layout.right_menu, container, false);} return MView;}}
The fragment on the right side of the slide menu, loading a layout without doing any event processing ~

And finally the mainactivity.

Package Com.zhy.zhy_slidemenu_demo04;import Java.util.arraylist;import Java.util.list;import android.os.Bundle; Import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentpageradapter;import Android.support.v4.view.viewpager;import Android.view.view;import Android.view.window;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;import Com.jeremyfeinstein.slidingmenu.lib.app.slidingfragmentactivity;public class Mainactivity extends Slidingfragmentactivity{private viewpager mviewpager;private fragmentpageradapter mAdapter;private List<Fragment > mfragments = new arraylist<fragment> (), @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); This.requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_main);//Initialize Slidemenuinitrightmenu ();//Initialize Viewpagerinitviewpager ();} private void Initviewpager () {Mviewpager = (Viewpager) Findviewbyid (R.id.id_viewpager); MAINTAB01 tab01 = new MainTab01 (); MAINTAB02 TAB02= new MAINTAB02 (); MAINTAB03 tab03 = new MainTab03 (); Mfragments.add (TAB01); Mfragments.add (TAB02); Mfragments.add (TAB03);/** * Initialize Adapter */madapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {@Overridepublic int getcount () {return Mfragments.size ();} @Overridepublic Fragment getItem (int arg0) {return mfragments.get (arg0);}}; Mviewpager.setadapter (Madapter);} private void Initrightmenu () {Fragment leftmenufragment = new Menuleftfragment (); Setbehindcontentview (R.layout.left_ Menu_frame); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.id_left_menu_frame, leftmenufragment). Commit (); Slidingmenu menu = Getslidingmenu (); Menu.setmode (slidingmenu.left_right);//Set Touch screen mode Menu.settouchmodeabove ( Slidingmenu.touchmode_margin); Menu.setshadowwidthres (r.dimen.shadow_width); Menu.setshadowdrawable ( R.drawable.shadow);//Set the width of the Sliding menu view menu.setbehindoffsetres (r.dimen.slidingmenu_offset);// Sets the value of the fade-in gradient effect Menu.setfadedegree (0.35f);//Menu.setbehindscrollscale (1.0f); Menu.setsecondaryshadowdrawable (R.DRawable.shadow);//Set the right (level two) slide-off menu Menu.setsecondarymenu (r.layout.right_menu_frame); Fragment rightmenufragment = new Menurightfragment (); Getsupportfragmentmanager (). BeginTransaction (). Replace ( R.id.id_right_menu_frame, Rightmenufragment). commit ();} public void Showleftmenu (view view) {Getslidingmenu (). ShowMenu ();} public void Showrightmenu (view view) {Getslidingmenu (). Showsecondarymenu ();}}

Briefly, Mainactivity inherits Slidingfragmentactivity, Fragmentpageradapter and Viewpager as the main layout in the activity, The menus on both sides of the Slidingmenu are then initialized separately.


Haha, lying on the gun again ~ ~ This example should be able to meet the needs of the general app.

5. Some common properties of Slidingmenu

Set the position of the side-slip menu, optional values left, right, Left_right (set when both sides have menus)

Menu.setmode (Slidingmenu.left_right);

Set the mode of touch screen, optional only margin, CONTENT
Menu.settouchmodeabove (Slidingmenu.touchmode_margin);

Sets the width of the shadow according to the ID of the dimension resource file
Menu.setshadowwidthres (R.dimen.shadow_width);

Sets the shadow effect of the swipe menu based on the resource file ID
Menu.setshadowdrawable (R.drawable.shadow);

Both of these are set to the width of the sliding menu view, and the second option
Set the offset of the slidingmenu from the screen
Menu.setbehindoffsetres (R.dimen.slidingmenu_offset);
Set width
Menu.setbehindwidth ()

Set the value of the fade-in fade-out effect
Menu.setfadedegree (0.35f);

Sets the speed ratio of the slidingmenu to the movement of the lower view, when the 1 o'clock moves simultaneously, the value 0-1
Menu.setbehindscrollscale (1.0f);

Set the shadow effect on a level two menu
Menu.setsecondaryshadowdrawable (R.drawable.shadow);

Set the right side (level two) Slide menu
Menu.setsecondarymenu (R.layout.right_menu_frame);

To set the layout for the slide menu
Menu.setmenu (R.layout.leftmenu);

Add sliding menu to all activity, optional value sliding_content, Sliding_window
Menu.attachtoactivity (this, slidingmenu.sliding_content);

There are also several listener events:

/** * Set Open listener event when the sliding menu is opened call */public void Setonopenlistener (Onopenlistener listener) {Mopenlistener = listener;} /** * Set Shutdown listener event when the sliding menu is closed call */public void Setoncloselistener (Oncloselistener listener) {// Mviewabove.setoncloselistener (listener); mcloselistener = listener;} /** * Set Open listener event, call */public void Setonopenedlistener (Onopenedlistener listener) After the sliding menu has been opened { Mviewabove.setonopenedlistener (listener);} /** * Set the shutdown listener event to call */public void Setonclosedlistener (Onclosedlistener listener) After the slide menu has been closed Mviewabove.setonclosedlistener (listener);}

And some used to check the


Have any questions ~ Welcome message ~


I put all the examples above including Slidingmenu's lib playing a bag ~

SOURCE Click to download

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.