Android third party open source framework Slidingmenu detailed [reprint]

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.

Open source framework, integrated with another open source project Actionbarsherlock:Click to download。

Note: Slidingmenu relies on another open source project Actionbarsherlock (above), so you need to add Actionbarsherlock as a Slidingmenu library project, otherwise it will report that the resource could not find an error.
Then add the Slidingmenu to your project.
Let's talk about the Acitonbarsherlock Import method ():

1. Import the Actionbarsherlock folder into the project first.
2, the Actionbarsherlock-samples folder in the demo file into the project.
3, add the Actionbarsherlock file to the demo project can be.
Slidingmenu Step ():

1, the library into the project.
2, the example into the project.
3, the Actionbarsherlock file add to the library, you need to delete the Library project Lib V4 package, otherwise it will error.
4, then the library (this time the library is containing actionbarsherlock) add to example, which is required to modify a code. See:


in the process of import, you will inevitably encounter a lot of errors:
To summarize:
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, and if you use the relevant API to Actionbarsherlock, May not find the way to Getsupportactionbar and other 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.

Well, with the above steps, we have successfully imported this third-party open source framework into our project:

Open source framework of the class there are many, I do not one to introduce, but give you a list of commonly used properties, so that you can fully and efficiently use this open source framework:

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

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

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:
[Java]
if (savedinstancestate = = null) {//== NULL when creating a new Fragment1
Contentfragment = new Fragment1 ();
} else {//not equal to NULL, direct get out
Not equal to NULL to find the fragment of the current activity display previously saved
Contentfragment = Getsupportfragmentmanager (). Getfragment (Savedinstancestate, "contentfragment");
}
Set Content Fragment
Getsupportfragmentmanager ()
. BeginTransaction ()
. replace (R.id.content_frame, contentfragment)
. 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:
[Java]
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 the width of the Slidingmenu menu
Menu.setfadedegree (0.35f);
Menu.attachtoactivity (this, slidingmenu.sliding_content);//Must be called
Menu.setmenu (r.layout.menu_layout_left);//is the normal layout
Menu.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:
[Java]
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:
[Java]
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 animations:
[Java]
Mtransformer = new Canvastransformer () {
@Override
public void Transformcanvas (canvas canvas, float percentopen) {
Canvas.scale (Percentopen, 1, 0, 0);
}
});

Original source: http://www.cnhonkerarmy.com/thread-206972-1-1.html

Android third party open source framework Slidingmenu detailed [reprint]

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.