Androidui--slidingmenu Use Example

Source: Internet
Author: User

Slidingmenu Introduction:

Slidingmenu is a relatively new setup interface or configuration interface effect, in the main interface left or right slide display settings interface, can easily do a variety of operations. There are a lot of applications that are using this effect now. such as Evernote, Google +, Foursquare, and so on, the domestic pea folder, everyone, 360 mobile phone assistants, etc. are using the Slidingmenu interface scheme.

Project: Https://github.com/jfeinstein10/SlidingMenu

Note: Slidingmenu relies on another open source project Actionbarsherlock, so you need to add Actionbarsherlock as a Slidingmenu library project, otherwise you will report that the resource could not find an error.

Then add the Slidingmenu to your project.

Slidingmenu Integration Common errors:

Jar mismatch! Fix your dependencies: a conflict caused by a inconsistency in the version of the jar package referenced by the project and itself. Ensure that the jar package versions used by Slidingmenu and Actionbarsherlock are consistent

If you use the relevant API to Actionbarsherlock, you may not be able to find methods such as Getsupportactionbar Actionbarsherlock. The reason is that the activity that uses actionbarsherlock needs to inherit from Sherlockactivity, modify the slidingfragmentactivity in the Slidingmenu library, Let it inherit from the sherlockfragmentactivity and recompile the library import.

Slidingmenu commonly used properties are:

Menu.setmode (slidingmenu.left);//Set Left slide menu

Menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen);//sets the screen range to slide, which can be slid across the full-screen area

Menu.setshadowdrawable (R.drawable.shadow);//Set Shadow Picture

Menu.setshadowwidthres (r.dimen.shadow_width);//Set the width of the shadow picture

Menu.setbehindoffsetres (R.dimen.slidingmenu_offset); the remaining width displayed on the main page when//slidingmenu

Menu.setbehindwidth (400);//Set the width of the Slidingmenu menu

Menu.setfadedegree (0.35f); degree of gradient when//slidingmenu sliding

Menu.attachtoactivity (this, slidingmenu.sliding_content);//enable the Slidingmenu to be attached to the activity

Menu.setmenu (r.layout.menu_layout);//Set Menu layout file

Menu.toggle ();//dynamic judgment automatically shuts down or turns on Slidingmenu

Menu.showmenu ();//Display Slidingmenu

Menu.showcontent ();//display content

Menu.setonopenlistener (Onopenlistener);//Monitor Slidingmenu Open

About close Menu There are two listeners, in a nutshell, for the menu Close event, one is when, and the other is after

Menu. Onclosedlistener (Onclosedlistener);//monitor Slidingmenu Shutdown Event

Menu. Onclosedlistener (Onclosedlistener);//monitor Slidingmenu post-shutdown event

Left and right can be slidingmenu menu only need to set

Menu.setmode (slidingmenu.left_right); property, and then set the layout file for the menu on the right

Menu.setsecondaryshadowdrawable (r.drawable.shadowright);//Shadow picture on right menu

To implement Slidingmenu with fragment:

1. First the activity inherits from the Slidingfragmentactivity under the Slidingmenu package

2. Setcontentview (r.layout.content_frame);//The layout is a full-screen framelayout

3. Setbehindcontentview (r.layout.menu_frame);//Set Slidingmenu to use a layout that is also a full-screen framelayout

4. Set the fragment of the Slidingmenu left menu

1 Setbehindcontentview (r.layout.menu_frame);   2  This . Getsupportfragmentmanager (). BeginTransaction ();   3 New menufragment ();   4 t.replace (R.id.menu_frame, leftmenufragment);   5

Menufragment is actually a fragment that shows a ListView

Then click on each item in the ListView to notify the activity to switch between the different fragment

To see the effect, we created 5 new frament, respectively

Fragment1, Fragment2, Fragment3, Fragment4, FRAGMENT5

Displayed in Slidingmenu with a ListView.

Set the fragment for the main page display:

1 if(Savedinstancestate = =NULL) {//= = NULL when creating a new Fragment12Contentfragment =NewFragment1 (); 3}Else{//not equal to NULL, direct get out4     //not equal to NULL to find the fragment of the current activity display previously saved5Contentfragment = Getsupportfragmentmanager (). Getfragment (Savedinstancestate, "Contentfragment"); 6 }  7 //Set Content Fragment8 Getsupportfragmentmanager ()9 . BeginTransaction ()Ten . Replace (R.id.content_frame, contentfragment) One. commit ();

Saves the currently displayed fragment in the activity's onsaveinstancestate

Getsupportfragmentmanager (). Putfragment (Outstate, "contentfragment", contentfragment);

Setting the Slidingmenu Property

SM = Getslidingmenu ();

If only the left menu is displayed, the right side, left, is supported on Left_right

Sm.setmode (slidingmenu.left_right);//Set menu slide mode, whether the menu appears on the left or right, or both sides

Sm.setshadowdrawable (R.drawable.shadow);//Set the Shadow's picture resource

Sm.setshadowwidthres (r.dimen.shadow_width);//Set the width of the shadow picture

Sm.setbehindwidth (200);//Setting the width of the menu

Sm.setbehindoffsetres (R.dimen.slidingmenu_offset); the remaining width displayed on the main page when//slidingmenu

Sm.settouchmodeabove (Slidingmenu.touchmode_fullscreen);//Set the area of the slide

Supports the right-side underline menu:

Slidingmenu can simultaneously support the left and right side of the menu, non-conflict, and animation beautiful, good experience.

Sm.setsecondarymenu (r.layout.menu_frame2);//Set Right menu

Sm.setsecondaryshadowdrawable (r.drawable.shadowright);//Set the picture Resource for the right menu shadow

Right Slidingmenu fragment.

Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.menu_frame2, New Samplelistfragment ()). commit ();

Settings Actionbar can be clicked:

Getsupportactionbar (). Sethomebuttonenabled (TRUE);//actionbar main button can be clicked

Getsupportactionbar (). Setdisplayhomeasupenabled (true);//Show Left icon

Setslidingactionbarenabled (false);//Slidingmenu The left and right sides of the fragment whether to display the title bar

Toggle the fragment of the main page display:

public void Switchcontent (Fragment f) {

Assign a value to the content fragment and save the fragment when Onsaveinstancestate

Contentfragment = f;

Fragmenttransaction t = Getsupportfragmentmanager (). BeginTransaction ();

T.replace (R.id.content_frame, F);

T.commit ();

Sm.showcontent ();

Use normal activity to implement Slidingmenu:

1Slidingmenu menu =NewSlidingmenu ( This);//direct new, not Getslidingmenu2 Menu.setmode (Slidingmenu.left); 3 Menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen); 4 menu.setshadowdrawable (R.drawable.shadow); 5 menu.setshadowwidthres (r.dimen.shadow_width); 6 menu.setbehindoffsetres (R.dimen.slidingmenu_offset); 7Menu.setbehindwidth (400);//set the width of the Slidingmenu menu8Menu.setfadedegree (0.35f); 9Menu.attachtoactivity ( This, slidingmenu.sliding_content);//must callTenMenu.setmenu (R.layout.menu_layout_left);//is the normal layout OneMenu.setbehindcanvastransformer (Mtransformer);

The corresponding slidingmenu in the Click event, because Slidingmenu has been included in the activity, so the direct Findviewbyid (ID), get the view can be processed accordingly.

Supports both left and right sides to draw a menu

Menu.setsecondarymenu (R.layout.menu_layout_right);

Menu.setsecondaryshadowdrawable (R.drawable.shadowright);

The right menu is the same as direct Findviewbyid (ID), after you get the view can be processed arbitrarily

Change the Slidingmenu animation

Slidingmenu supports the definition of different animations, including stretching, zooming, rotating, and so on, when left or right-sliding. It is the animation that slidingmenu how to appear during the sliding process.

Animation is also easy to use

First define Canvastransformer Mtransformer; variables:

1Mtransformer =NewCanvastransformer () {2 @Override3      Public voidTransformcanvas (Canvas canvas,floatPercentopen) {  4         floatScale = (float) (percentopen*0.25 + 0.75); 5Canvas.scale (scale, scale, canvas.getwidth ()/2, Canvas.getheight ()/2); 6     }  7};

Then set the Mtransformer object to Slidingmenu, which is the Zoom animation:

1 Private voidInitslidupcanvastransformer () {2Mtransformer =NewCanvastransformer () {3 @Override4          Public voidTransformcanvas (Canvas canvas,floatPercentopen) {  5Canvas.translate (0, Canvas.getheight () * (1-interp.getinterpolation (Percentopen)); 6         }  7     }; 8 }  9 Ten Private StaticInterpolator Interp =Newinterpolator () { One @Override A      Public floatGetinterpolation (floatt) { -T-= 1.0f;  -         returnT * t * + 1.0f;  the     }   -};

Stretch animations:

1 New Canvastransformer () {  2    @Override  3      public  voidfloat  percentopen) {  4         Canvas.scale (percentopen, 1, 0, 0) ;   5     }  6 });  

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.