Android Open Source Project sidebar menu (slidingmenu) use detailed _android

Source: Internet
Author: User

Project Download Address: 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 cannot find an error.
Then add the Slidingmenu to your project.

Slidingmenu Integration Common errors:

Jar mismatch! Fix your dependencies: conflict caused by an inconsistency between the referenced project and the jar pack version since its own project. Ensure that the jar package version used by Slidingmenu and Actionbarsherlock is consistent

If you use the relevant APIs to Actionbarsherlock, you may not find a way to actionbarsherlock such as Getsupportactionbar. The reason is that the activity of using actionbarsherlock should inherit from Sherlockactivity, modify the slidingfragmentactivity in Slidingmenu library, Let it inherit from Sherlockfragmentactivity and recompile the library import.

Slidingmenu Common Properties Introduction:

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

Menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen)//Set sliding screen range, which can slide in 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 of the main page displayed when the//slidingmenu is drawn

Menu.setbehindwidth (400);//Set Width of slidingmenu menu

Menu.setfadedegree (0.35f);//slidingmenu the degree of gradual sliding

Menu.attachtoactivity (this, slidingmenu.sliding_content);//make slidingmenu attached to activity

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

Menu.toggle ()//dynamic judgment automatically closes or opens Slidingmenu

Menu.showmenu ()//Display Slidingmenu

Menu.showcontent ()//display content

Menu.setonopenlistener (Onopenlistener)//Listening Slidingmenu Open

On the off menu there are two listening, in simple terms, for the menu Close event, one is when, one is after

Menu. Onclosedlistener (Onclosedlistener)//monitoring Slidingmenu Shutdown Event

Menu. Onclosedlistener (Onclosedlistener)//monitor Slidingmenu event after shutdown

Can be left or right to draw the Slidingmenu menu only need to set

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

Menu.setsecondaryshadowdrawable (r.drawable.shadowright);//Shadow Picture of right menu

Use fragment to implement Slidingmenu:

1. First 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 use of the layout, is also a full-screen framelayout

4. Set the Slidingmenu left menu fragment

Setbehindcontentview (r.layout.menu_frame); 
Fragmenttransaction t = This.getsupportfragmentmanager (). BeginTransaction (); 
Leftmenufragment = new Menufragment (); 
T.replace (R.id.menu_frame, leftmenufragment); 

Menufragment is actually a fragment, showing a ListView
Then click on each item of 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
Shown in Slidingmenu with ListView.

Set the fragment displayed on the main page:

if (savedinstancestate = = null) {//== NULL when the new Fragment1 
contentfragment = new Fragment1 (); 
} else {//is not equal to NULL, Direct get out 
//Not equal to NULL, find out the fragment 
contentfragment = Getsupportfragmentmanager () of the current activity shown previously. getfragment (Savedinstancestate, "contentfragment"); 
} 
Set content fragment 
Getsupportfragmentmanager () 
. BeginTransaction (). 
Replace (R.id.content_frame, Contentfragment) 

Save the currently displayed fragment in the onsaveinstancestate of the activity
Getsupportfragmentmanager (). Putfragment (Outstate, "contentfragment", contentfragment);

Setting the Slidingmenu Property

SM = Getslidingmenu ();
If only the left side of the menu is displayed, the right side is left, and both sides are supported Left_right
Sm.setmode (Slidingmenu.left_right)//Set menu slide mode, menu appears on the left or right, or both sides
Sm.setshadowdrawable (R.drawable.shadow);//Set the picture resource for the shadow
Sm.setshadowwidthres (r.dimen.shadow_width);//Set the width of the shadow picture
Sm.setbehindwidth (200);//Set the width of the menu
Sm.setbehindoffsetres (R.dimen.slidingmenu_offset); the remaining width of the main page displayed when the//slidingmenu is drawn
Sm.settouchmodeabove (Slidingmenu.touchmode_fullscreen);/Set the area of the slide

Support to the right side underline menu:

Slidingmenu can also support the left and right sides of the menu, not conflicting, but also beautiful animation, good experience.
Sm.setsecondarymenu (r.layout.menu_frame2)//Set Right menu
Sm.setsecondaryshadowdrawable (r.drawable.shadowright);//Set Picture resource for right menu shadow
The fragment of the right Slidingmenu
Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.menu_frame2, New Samplelistfragment ()). commit ();

Set Actionbar can be clicked:

Getsupportactionbar (). Sethomebuttonenabled (TRUE);//actionbar main button can be clicked
Getsupportactionbar (). Setdisplayhomeasupenabled (true);//Show Left icon
Setslidingactionbarenabled (FALSE),//left and right side slidingmenu fragment whether to display the title bar

Toggle the fragment displayed on the main page:

public void Switchcontent (Fragment f) {
Assign values to content fragment and save this fragment when Onsaveinstancestate
Contentfragment = f;
Fragmenttransaction t = Getsupportfragmentmanager (). BeginTransaction ();
T.replace (R.id.content_frame, F);
T.commit ();
Sm.showcontent ();

Use common activity to implement Slidingmenu:

Slidingmenu menu = new Slidingmenu (this);/direct new, not getslidingmenu
Menu.setmode (Slidingmenu.left);
Menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen);
Menu.setshadowdrawable (R.drawable.shadow);
Menu.setshadowwidthres (R.dimen.shadow_width);
Menu.setbehindoffsetres (R.dimen.slidingmenu_offset);
Menu.setbehindwidth (400);//Set Width of slidingmenu menu
Menu.setfadedegree (0.35f);
Menu.attachtoactivity (this, slidingmenu.sliding_content);//Must call
Menu.setmenu (r.layout.menu_layout_left);/is the common layout layout
Menu.setbehindcanvastransformer (Mtransformer);

Corresponding Slidingmenu in the click event, because the Slidingmenu has been included in the activity, so direct Findviewbyid (ID), after the view can be processed accordingly.

Supports both sides of the menu to underline

Menu.setsecondarymenu (R.layout.menu_layout_right);
Menu.setsecondaryshadowdrawable (R.drawable.shadowright);

Right menu as direct Findviewbyid (ID), after the view can be arbitrary processing

Replace the Slidingmenu animation

Slidingmenu supports animations that define different animations, such as stretching, scaling, rotation, and so on, when left or right-sliding. It is in the process of sliding, slidingmenu how to appear animation.
Animation is also very easy to use
First, define Canvastransformer Mtransformer variables:

Mtransformer = new Canvastransformer () { 
@Override public 
void Transformcanvas (Canvas Canvas, float percentopen ) {
Float scale = (float) (percentopen*0.25 + 0.75); 
Canvas.scale (scale, scale, canvas.getwidth ()/2, Canvas.getheight ()/2);
}
;

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

private void Initslidupcanvastransformer () { 
Mtransformer = new Canvastransformer () { 
 
@Override 
public void Transformcanvas (Canvas Canvas, float percentopen) { 
canvas.translate (0, Canvas.getheight () * (1- Interp.getinterpolation (Percentopen))); 
} 
 
private static Interpolator Interp = new Interpolator () { 
@Override public 
float getinterpolation (float t) {
   t = 1.0f; 
return T * t * t + 1.0f; 
} 
; 

Stretch Animation:

Mtransformer = new Canvastransformer () { 
@Override public 
void Transformcanvas (Canvas Canvas, float Percentopen) { 
Canvas.scale (percentopen, 1, 0, 0); 
} 
});

Related Article

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.