How do I use the swipe menu Slidingmenu?

Source: Internet
Author: User

Left side slide:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvanvuahvhag91c2u=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "align=" left "height=" 389 "width=" 217 ">

Right side slide:








Click here to download the source code

Before that, I used my online friends to define my own side-slip menu. Use rewrite layouts to control and implement. But by the time the final project was written in 80%. The sudden discovery has clashed with my function.

I'll go. I can't handle this messy code.

So there is no way, on the internet crazy Search, three days later.

It's done. and was successfully embedded in my project.

The test demo is like!

Do not take the curiosity to use those self-defined side-slip menu, when your project immediately to knot, you use the slide menu with your project features conflict, you can not solve, then you call a crazy.

Therefore, long-lasting more durable, others are used, when you project failure, talent quickly cooked to solve;

All right. Take a look at how to use it!

/** Slidingmenu often uses attributes: */menu.setmode (slidingmenu.left);//Set Left slide menu slidingmenu.right slidingmenu.left_right right side slide Left and right side slip menu.settouchmodeabove (slidingmenu.touchmode_fullscreen);//Set sliding screen limits. This setting is capable of sliding menu.setshadowdrawable (R.drawable.shadow) in full-screen area,//Setting Shadow Menu.setshadowwidthres (R.dimen.shadow_width);// Sets the width of the shadow Menu.setbehindoffsetres (R.dimen.slidingmenu_offset); leftover width of the main page when//slidingmenu is drawn menu.setbehindwidth (400) ;//Set the width of the Slidingmenu menu to Menu.setfadedegree (0.35f), and the degree of gradient menu.attachtoactivity when//slidingmenu is sliding (this. slidingmenu.sliding_content);//Slidingmenu attached to activity Menu.setmenu (r.layout.menu_layout);// Set Menu Layout file Menu.toggle ();//dynamic determine active closed or open slidingmenumenu.showmenu ();//display slidingmenumenu.showcontent ();// Display content Menu.setonopenlistener (onopenlistener);//slidingmenu Open about closed menu there are two. In simple terms, for a menu close transaction. One is when, one is after menu. Onclosedlistener (Onclosedlistener);//slidingmenu closed when the transaction menu. Onclosedlistener (Onclosedlistener);//slidingmenu closed Service

Simple to set the left-side menu side-slip usage:
public class Slidingexample extends Activity {    @Override public    void OnCreate (Bundle savedinstancestate) {        Super.oncreate (savedinstancestate);        Settitle (R.string.attach);        Set the content view        Setcontentview (r.layout.content);        Configure the Slidingmenu        slidingmenu menu = new Slidingmenu (this);        Menu.setmode (slidingmenu.left);        Menu.settouchmodeabove (slidingmenu.touchmode_fullscreen);        Menu.setshadowwidthres (r.dimen.shadow_width);        Menu.setshadowdrawable (R.drawable.shadow);        Menu.setbehindoffsetres (r.dimen.slidingmenu_offset);        Menu.setfadedegree (0.35f);        Menu.attachtoactivity (this, slidingmenu.sliding_content);        Menu.setmenu (R.layout.menu);    }}

Suppose you want to set the left and right side of the menu sideways, look here:
public class Mainactivity extends fragmentactivity {public slidingmenu mslidingmenu; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mslidingmenu = new Slidingmenu (this); Mslidingmenu.setmode (slidingmenu.left_right); mslidingmenu.setbehindoffsetres (R.dimen.slidingmenu_offset); Mslidingmenu.settouchmodeabove (Slidingmenu.touchmode_fullscreen); Mslidingmenu.attachtoactivity (this, slidingmenu.sliding_content); Mslidingmenu.setmenu (R.layout.slidemenu_ Primary)///left Side slip mslidingmenu.setsecondarymenu (r.layout.slidemenu_secondery);//Right Side slip getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.aty_main_framlayout, New Viewpagerfragment ()). commit (); Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.framlayout_fragment, New Translationanimationfragment ()). commit ();}  /** override return key and Menu key, control menu side slide Off/@Overridepublic boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method Stubswitch (KeyCode){Case KeyEvent.KEYCODE_BACK:if (mslidingmenu.ismenushowing ()) mslidingmenu.showcontent (); else finish (); return true; Case KeyEvent.KEYCODE_MENU:mSlidingMenu.showSecondaryMenu (); return true;} Return Super.onkeydown (KeyCode, event);}}

Here's a look at how to solve the collision between the side slide and the Viewpager: "Of course, my source code shows how to use"

Mslidingmenu.addignoredview (Viewpager);
Before your Viewpager content is displayed. Call the method above. It's OK,

Slidingmenu.settouchmodeabove (). This includes a total of three gesture modes: Touchmode_fullscreen full-screen mode, which can be opened through gestures in the body layout slidingmenutouchmode_ Margin mode, at the edge of the body layout can be found by gesture Slidingmenutouchmode_none nature is unable to open the Slidingmenu by gesture

When you set the shadow, in the Res/drawable/shadow.xml
<?xml version= "1.0" encoding= "Utf-8"?

><shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <gradient android: Centercolor= "#11000000" android:endcolor= "#00000000" android:startcolor= "#33000000"/></shape>


To continue writing:

One: After you import project, assume that you are using Actionbar, the Actionbarsherlocklibrary class library, the Slidemenu class library, and the built project, You must use the same Android-support-v4.jar package, (Use your own project, and then replace the others); otherwise, you will get an error!

Second: When we have finished importing the side-slip effect, did we find the same distance between the left and right sides of the sideways menu we made? The answer is necessary, as the source code does. Does not give us a separate set of side slip--left or right distance. Well, that's what we're going to set ourselves. I looked at the source code and looked at the Internet to see the small partners on the slippery side of their own definition of understanding. In summary, you can complete this function simply by changing several methods and properties.

Let's feel free to set the width of the left and right side slides.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvanvuahvhag91c2u=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "align=" left "height=" 325 "width=" 182 ">


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvanvuahvhag91c2u=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "align=" left "height=" 324 "width=" 189 ">





Click here Source code download









First, add several methods to the Slidingmenu.java in the source code:

Define your own extended side slip width method ********************************************//    Sets the offset from the left edge of the right sidebar menu when it is expanded public void setrightmenuoffset (int offset) {mviewbehind.setrightwidthoffset (offset); }//Set the right side bar menu to expand the offset from the left margin public void setrightmenuoffsetres (int resId) {int i = (int) getcontext (). Get            Resources (). Getdimension (ResId);    Setrightmenuoffset (i);            }//sets the width of the right sidebar @SuppressWarnings ("deprecation") public void setrightbehindwidth (int i) {int width; Display display = ((WindowManager) GetContext (). Getsystemservice (Context.window_serv            ICE). Getdefaultdisplay ();                    try {class<?> cls = Display.class; class<?

>[] parametertypes = {Point.class}; Point parameter = new Point (); method = Cls.getmethod ("GetSize", parametertypes); Method.invoke (display, parameter); width = parameter.x; } catch (Exception e) {width = Display.getwidth (); } setrightmenuoffset (Width-i); }//sets the width of the right sidebar public void setrightbehindwidthres (int res) {int i = (int) getcontext (). Getresources (). GE Tdimension (RES); Setrightbehindwidth (i); }


Then, change the Customviewbehind.java class

That is: 1. Add the necessary methods in the source code Customviewbehind.java class:

----------------------define the extended join method----------------------------------------private int mrightwidthoffset; Right width offset public void setrightwidthoffset (int i) {mrightwidthoffset = I;requestlayout ();} private int getrightbehindwidth () {return msecondarycontent.getwidth ();}

2. Change method: Scrollbehindt (View content, int x, int y);

public void Scrollbehindto (View content, int x, int y) {int vis = view.visible; if (Mmode = = Slidingmenu.left) {if (x >= content.getleft ()) Vis = View.invisible                ;        ScrollTo ((int) ((x + getbehindwidth ()) * Mscrollscale), y); } else if (Mmode = = slidingmenu.right) {if (x <= content.getleft ()) Vis = View.i                nvisible;                                                ScrollTo ((int) (Getrightbehindwidth ()-getwidth () + (X-getrightbehindwidth ())        * Mscrollscale), y); } else if (Mmode = = slidingmenu.left_right) {mcontent.setvisibility (x >= content.getleft ()?

View.INVISIBLE:View.VISIBLE); Msecondarycontent. setvisibility (x <= content.getleft ()? View.INVISIBLE:View.VISIBLE); Vis = X = = 0?

View.INVISIBLE:View.VISIBLE; if (x <= content.getleft ()) {scrollTo ((int) ((x + getbehindwidth ()) * Mscrollscale), y); } else {scrollTo ((int) (Getrightbehindwidth ()- GetWidth () + (X-getrightbehindwidth ()) * Mscrollscale), y); }} if (vis = = view.invisible) log.v (TAG, "behind INVISIBLE"); Setvisibility (VIS);}

3. How to change: Getmenuleft (View content, int page);
public int                Getmenuleft (View content, int page) {if (Mmode = = slidingmenu.left) {switch (page) {                Case 0:return Content.getleft ()-getbehindwidth ();                Case 2:return Content.getleft ();                        }} else if (Mmode = = slidingmenu.right) {switch (page) {case 0:                return Content.getleft ();                Case 2:return Content.getleft () + getrightbehindwidth ();                        }} else if (Mmode = = slidingmenu.left_right) {switch (page) {case 0:                Return Content.getleft ()-getbehindwidth ();                Case 2:return Content.getleft () + getrightbehindwidth (); }} return Content.getleft ();} 
4. How to change: Getabsrightbound (View content);
public int Getabsrightbound (View content) {        if (Mmode = = slidingmenu.left) {                return content.getleft ();        } else if (Mmode = = Slidingmenu.right                        | | mmode = = slidingmenu.left_right) {                return content.getleft () + Getrightbehi Ndwidth ();        }        return 0;}

At last. Use the attribute variable Mrightwidthoffset and method Getrightbehindwidth (). To replace a method in the source code Customviewbehind.java class
OnLayout (Boolean changed, int l, int t, int r, int b); onmeasure (int widthmeasurespec, int heightmeasurespec);
The variable property in Mwidthoffset. And the methods in
Drawfade (View content, canvas canvas, float openpercent)
Getbehindwidth () in the detail code. For example, see the following:
@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {final int width = r-l;final int height = B-t;mcontent.layout (0, 0, width-mwidthoffset, height); if (msecondarycontent! = null)//change here// Msecondarycontent.layout (0, 0, width-mwidthoffset, height); msecondarycontent.layout (0, 0, Width-mrightwidthoffset, height);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int width = getdefaultsize (0, WIDTHMEASURESPEC); int height = getdefaultsize (0, Heightmeasurespec); setmeasureddimension (width, height);//change here// Final int contentwidth = Getchildmeasurespec (widthmeasurespec, 0, width//-mwidthoffset); final int contentwidth = GetChil Dmeasurespec (widthmeasurespec, 0, width-mrightwidthoffset); final int contentheight = Getchildmeasurespec ( Heightmeasurespec, 0,height); Mcontent.measure (Contentwidth, contentheight); if (msecondarycontent! = null) Msecondarycontent.measure (Contentwidth, contentheight);}

public void Drawfade (View content, canvas canvas, float openpercent) {if (!mfadeenabled) return;final int alpha = (int) (MF  Adedegree * 255 * Math.Abs (1-openpercent)) Mfadepaint.setcolor (Color.argb (alpha, 0, 0, 0)); int left = 0;int right = 0;if (Mmode = = Slidingmenu.left) {left = Content.getleft ()-getbehindwidth (); right = Content.getleft ();} else if (Mmode = = slidingmenu.right) {left = Content.getright (), right = Content.getright () + Getbehindwidth ();} else if (m Mode = = slidingmenu.left_right) {left = Content.getleft ()-getrightbehindwidth ();//replace original getbehindwidth (); right = Content.getleft (); Canvas.drawrect (left, 0, right, getheight (), mfadepaint); left = Content.getright (); right = Content.getright () + getrightbehindwidth ();//Replace the original Getbehindwidth ();} Canvas.drawrect (left, 0, right, getheight (), mfadepaint);}

Last and last. Adding a few lines of code to the activity class that controls the side-slip menu is done:
Mslidingmenu = new Slidingmenu (this); Mslidingmenu.setmode (slidingmenu.left_right); mslidingmenu.setbehindoffsetres (r.dimen.slidingmenu_offset);//sliding distance mslidingmenu.setrightmenuoffsetres (r.dimen.slidingmenu_right_offset);// Rewrite the right side slip distance mslidingmenu.settouchmodeabove (slidingmenu.touchmode_fullscreen); Mslidingmenu.attachtoactivity (This, slidingmenu.sliding_content); Mslidingmenu.setmenu (r.layout.slidemenu_primary); Mslidingmenu.setsecondarymenu ( R.layout.slidemenu_secondery); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.aty_main_framlayout, New Viewpagerfragment ()). commit (); Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.framlayout_ Fragment, New Translationanimationfragment ()). commit ();




How do I use the swipe menu Slidingmenu?

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.