Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
In an article a long time ago, a brief introduction to the use of the Open source project Slidingmenu control, this article will be a more detailed introduction to the latest version of the Slidingmenu control use method.
Slidingmenu's github address: Https://github.com/jfeinstein10/SlidingMenu
Integrate Slidingmenu into your project
Before we can use open source projects, we need to introduce the project as a resource into our current project. Before you integrate, download the sample code for the project on GitHub and import it into eclipse. There are two ways to import an external project into the current project.
(1) Set the resource project as a library into our project, as shown in the example code in examplelistctivity. One thing to be aware of is that the Actionbarsharlock dependency project is used in the Slidingmenu sample code, so we need not only Slidingmenu code, but also Actionbarsharlock Project Resources. Actionbarsharlock's GitHub project address is https://github.com/JakeWharton/ActionBarSherlock you can download it yourself. After we have set up the dependency project, we can use the Slidingmenu.
(2) In addition to using the above-mentioned method of dependency engineering, we can directly copy the dependent project, that is, the code inside the library project directly into our target project. I personally prefer this method, although this will increase the volume of the project installation package, but for the project management, more convenient. In this project, we will copy the following file into our project.
How to add Slidingmenu controls
(1) Inherit from slidingfragmentactivity. If this is the way, our activity needs to inherit from slidingfragmentactivity. Slidingfragmentactivity is a class within Slidingmenu that inherits from it, and we can have a slidingmenu control. If we want to use this method, we can refer to the following code.
Package Com.example.slidingmenudemo;import Android.os.bundle;import android.support.v4.app.FragmentTransaction; Import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;import com.jeremyfeinstein.slidingmenu.lib.app.slidingfragmentactivity;/** * Inherits from Slidingfragmentactivity's slide-bar implementation, Can realize the slide bar is the effect of fragment slide bar * * @author Zhaokaiqiang * @see http://blog.csdn.net/zhaokaiqiang1992 * */public class MethodOne Activity extends Slidingfragmentactivity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate);//Set the layout file of the main interface Setcontentview (r.layout.activity_method_one);// Set the layout file for the container that holds the skid bar Setbehindcontentview (r.layout.frame_menu);//fills the fragment class of the slide bar into the layout file of the slide bar's container fragmenttransaction Transaction = Getsupportfragmentmanager (). BeginTransaction (); Samplelistfragment fragment = new Samplelistfragment (); Transaction.replace (r.id.menu_frame, fragment); Transaction.commit ();//Get to the Slidingmenu object and set some common properties Slidingmenu sm = Getslidingmenu ();//Set the width of the shadow Sm.setshadowwidth (0 );//Set the color of the shadow Sm.setsHadowdrawable (R.drawable.shadow);///Set the slide bar completely expanded, the distance from the other side, the unit PX, the larger the setting, the smaller the width of the slide bar sm.setbehindoffset (100);//Set the degree of the gradient, The range is 0-1.0f, and the larger the setting, the darker the color will be when the skid bar is just scratched. 1.0f, the color is All Black Sm.setfadedegree (0.3f),//Set the touch mode, you can select Full screen, or the edge of the underline, or is not sm.settouchmodeabove ( Slidingmenu.touchmode_fullscreen),///Set Actionbar can follow the slide bar movement, if not, you can remove the setslidingactionbarenabled (false);}}
Using this implementation, our skid bar is implemented using a fragment,Setbehindcontentview () method, we can set the container to put the skid bar layout, this container is a very simple layout file, generally more fixed, such as the following can be
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/menu_frame " android:layout_width=" match_parent " android:layout_height=" Match_parent "/>
In this case, a fragmentlayout layout with ID is set so that we cantransaction.replace (r.id.Menu_frame, Fragment) method fills the fragment of our skid bar into Slidingmenu.
Because a slidingmenu is already integrated into the slidingfragmentactivity, we can get a Silidingmenu object by Getslidingmenu (), You can then set the usual properties just like the code above.
Adding Slidingmenu in this way is very convenient, but we have to inherit from Slidingfeagmentactivity, there are some limitations.
(2) inherit from fragmentactivity
In addition to inheriting Slidingmenu's own activity, we can also directly inherit from the fragmentactivity, if this way, we can refer to the following code implementation.
Package Com.example.slidingmenudemo;import Android.os.bundle;import android.support.v4.app.FragmentActivity; Import com.jeremyfeinstein.slidingmenu.lib.slidingmenu;/** * Inherit from the fragmentactivity of the slide bar implementation, you can realize the slide bar is the effect of the fragment slide bar * @ Author Zhaokaiqiang * @see http://blog.csdn.net/zhaokaiqiang1992 */public class Methodtwoactivity extends fragmentactivity {private Slidingmenu menu; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_method_two); menu = new Slidingmenu (this); menu.setshadowwidth (0); menu.setshadowdrawable (R.drawable.shadow); Menu.setbehindoffset (+); Menu.setfadedegree (0.35f); Menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen); Menu.attachtoactivity (This, SlidingMenu.SLIDING_ CONTENT); Menu.setmenu (R.layout.frame_menu); Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.menu_ Frame, new Samplelistfragment ()). commit ();}}
In this way, we need to define slidingmenu ourselves and then connect Slidingmenu to our fragmentactivity via Attachtoactivity, SetMenu () The way to set up is the layout of our skid-bar container, and the first method above is the same.
Other Common Properties(1) Double slide barwe can use the following code to achieve the effect of double slide bar
@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Set the left side of the slide bar Getslidingmenu (). SetMode (Slidingmenu.left_right); Getslidingmenu (). Settouchmodeabove ( Slidingmenu.touchmode_fullscreen); Setcontentview (r.layout.content_frame); Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.content_frame, New Samplelistfragment ()). commit ();//Set the right side of the slide bar Getslidingmenu (). Setsecondarymenu (R.layout.menu_frame_two); Getslidingmenu (). setsecondaryshadowdrawable (R.drawable.shadowright); Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.menu_frame_two, New Samplelistfragment ()). commit ();}
(2) Setting touch modethroughGetslidingmenu (). Settouchmodeabove () canset the touch mode of the slide bar with the following 3 constant values
Slidingmenu. Touchmode_fullscreen Full-screen mode, full-screen swipe to open
Slidingmenu. Touchmode_margin side mode, slide only on the side of the screen to open, center slide not open
Slidingmenu. Touchmode_none No Touch mode, can not be opened by touch, only through slidingmenu (). Toggle () Open or close
(3) Set slide show animationthroughSlidingmenu.setbehindcanvastransformer (Canvastransformer) method to set the display animation for the slide bar, which is a Canvastransformer object. Here are the settings for several common animations
Collapse Animation
New Canvastransformer () {@Overridepublic void Transformcanvas (canvas canvas, float percentopen) {Canvas.scale ( Percentopen, 1, 0, 0);}}
Zoom in on animation
New Canvastransformer () {@Overridepublic void Transformcanvas (canvas canvas, float percentopen) {Float scale = (float) (PE rcentopen*0.25 + 0.75); Canvas.scale (scale, scale, canvas.getwidth ()/2, Canvas.getheight ()/2);}}
Rise animation
private static Interpolator Interp = new Interpolator () {@Overridepublic float getinterpolation (float t) {t-= 1.0f;return T * t * + 1.0f;}}; New Canvastransformer () {@Overridepublic void Transformcanvas (canvas canvas, float percentopen) {canvas.translate (0, Canvas.getheight () * (1-interp.getinterpolation (Percentopen)));}}
For additional properties, consult the sample code yourself.
"Android Interface Implementation" Slidingmenu the latest version of the use of detailed