Android Slidingmenu Use specific explanations

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/36677279

Very many apps have a side-slip menu feature. Some apps are side-slip menu ~slidingmenu This open source project can be very good to help us achieve the slip function, assuming that the slidingmenu is not very familiar with the children's shoes, can participate in this blog. The way to introduce the side-slip menu to the project is very much, this blog first through the sample introduction of various introduction methods, and then show you an example: the main layout viewpager. The use of a side-slip menu is almost identical to the needs of most applications. About frequently used attributes, described at the end of the article.

1, in the activity through the Slidingmenu construction method. Directly set the side slide menu

Package Com.zhy.zhy_slidemenu_demo;import Android.app.activity;import Android.os.bundle;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;public class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /Configure the Slidingmenuslidingmenu menu = new Slidingmenu (this); Menu.setmode (slidingmenu.left);// Sets the mode of the touch screen Menu.settouchmodeabove (slidingmenu.touchmode_fullscreen); Menu.setshadowwidthres (R.dimen.shadow_width) ; menu.setshadowdrawable (R.drawable.shadow);//Set the width of the Sliding menu view menu.setbehindoffsetres (r.dimen.slidingmenu_offset);// Sets the value of the fade-out effect Menu.setfadedegree (0.35f);/** * Sliding_window would include the Title/actionbar in the content * section of the Slidingmenu, while Sliding_content does not. */menu.attachtoactivity (this, slidingmenu.sliding_content);//Set Layout Menu.setmenu (R.layout.leftmenu) for the side-slip menu;}}


is not particularly simple ~ a few lines of code to do ~ HA ~

2. By inheriting the activity slidingactivity

A, Inherit slidingactivity

b, then Setbehindcontentview in OnCreate (R.layout.leftmenu); Set the layout of the Sideways menu

C, get the Slidingmenu object through Getslidingmenu (), then set the style

Package Com.zhy.zhy_slidemenu_demo02;import Android.os.bundle;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity; public class Mainactivity extends slidingactivity{@Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Setbehindcontentview ( R.layout.leftmenu);//Configure the Slidingmenuslidingmenu menu = Getslidingmenu (); Menu.setmode (slidingmenu.left);// Sets the mode of the touch screen Menu.settouchmodeabove (slidingmenu.touchmode_fullscreen); Menu.setshadowwidthres (R.dimen.shadow_width) ; menu.setshadowdrawable (R.drawable.shadow);//Set the width of the Sliding menu view menu.setbehindoffsetres (r.dimen.slidingmenu_offset);// Sets the value of the fade-out effect Menu.setfadedegree (0.35f);/** * Sliding_window would include the Title/actionbar in the content * section of the Slidingmenu, while Sliding_content does not. *///menu.attachtoactivity (this, slidingmenu.sliding_content);//Menu.setmenu (R.layout.leftmenu);}}

As in the first way ~ is not very easy~~

3. Use Slidingmenu as a normal control

Can take slidingmenu as a normal view, and then declare in the layout, the insane play ~ below to see a sample:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/id_main_ly "android:layout_width=" Match_parent "android:layout_height = "Match_parent" > <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:layout_marginleft=" 30DP "android:layout_margintop=" 30DP "> <com.jeremyfeinstein.s Lidingmenu.lib.SlidingMenu xmlns:sliding= "Http://schemas.android.com/apk/res-auto" android:id= "@+id/ Slidingmenulayout "android:layout_width=" 120DP "android:layout_height=" 170DP "Android:bac Kground= "#ffffffff" sliding:behindoffset= "0DP" sliding:behindscrollscale= "1" sliding:fade Degree= "0.3" sliding:fadeenabled= "true" sliding:touchmodeabove= "fullscreen" Sliding:viewa Bove= "@layout/pic"/> </linearlayout></RelativeLayout> 

We slidingmenu layout in the layout file as normal view, and set the Viewabove value to another layout.

See pic Layout below:

<?xml version= "1.0" encoding= "Utf-8"? ><imageview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" 120DP "    android:layout_height=" 170DP "    android:src=" @drawable/zhy "/ >

is a picture of a sister.

Last look at the main activity:

Package Com.zhy.zhy_slidemenu_demo03;import Android.app.activity;import Android.os.bundle;import Android.view.View ; Import Android.view.view.onclicklistener;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;public class Mainactivity extends Activity{private slidingmenu mleftmenu; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); MLeftMenu = ( Slidingmenu) Findviewbyid (r.id.slidingmenulayout);//Configure the slidingmenu//slidingmenu menu = new Slidingmenu ( this); Mleftmenu.setmode (slidingmenu.left);//Set Touch screen mode mleftmenu.setshadowwidthres (r.dimen.shadow_width); Mleftmenu.setshadowdrawable (R.drawable.shadow); Mleftmenu.setmenu (R.layout.leftmenu); Mleftmenu.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (mleftmenu.ismenushowing ()) Mleftmenu.toggle ();}); /Set the width of the sliding menu view//Set the value of the fade-out effect/** * Sliding_window would include the Title/actionbar in the content * section of the Slidingmen U, while Sliding_content does not. */}}


You can also swipe to show whatever it is by setting a slide show introduction to the picture by Slidingmenu. Download, share button or something.

Pictures very many times do not know how efficient, can play like this, but do not recommend ha ~


4, Slidingmenu set left and right side slide Menu Example

The 3 methods described above. The events of the controls in the Slidingmenu layout need to be written in the activity. This makes the code more bloated. Fragment is typically used as a layout container for a side-slip menu.

Core code:

Fragment leftmenufragment = new Menuleftfragment (); Setbehindcontentview (R.layout.left_menu_frame); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.id_left_menu_frame, leftmenufragment). commit (); Slidingmenu menu = Getslidingmenu (); Menu.setmode (slidingmenu.left_right);//Set Touch screen mode Menu.settouchmodeabove ( Slidingmenu.touchmode_margin);

First, slide the side menu through Setbehindcontentview (r.layout.left_menu_frame); set a layout. There is only one framelayout in this layout. Then use Fragmentmanager to replace the fragment with this fragment, so this fragment is the layout of our side-slip menu. Our event-handling code can also be written in fragement, not in activity.

See the detailed example below:

First, each of the two fragment on the left and right sides:

Menuleftfragment

Package Com.zhy.zhy_slidemenu_demo04;import Java.util.arrays;import Java.util.list;import android.os.Bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.listadapter;import Android.widget.listview;public class Menuleftfragment extends Fragment{private View mview;private ListView mCategories ;p rivate list<string> mdatas = arrays.aslist ("chat", "Discovery", "Contacts", "Friends Circle", "subscription number");p rivate listadapter madapter;@ Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {if (MView = = NULL) {Initview (Inflater, container);} return MView;} private void Initview (Layoutinflater inflater, ViewGroup container) {MView = Inflater.inflate (R.layout.left_menu, container, false); mcategories = (ListView) Mview.findviewbyid (r.id.id_listview_categories); madapter = new Arrayadapter<string> (Getactivity (), Android. R.layout.simple_liSt_item_1, Mdatas); Mcategories.setadapter (Madapter);}} 

The left side slide layout is a ListView, the code is simpler ~

Menurightfragment

Package Com.zhy.zhy_slidemenu_demo04;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class MenuRightFragment Extends Fragment{private view MView; @Overridepublic view Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {if (MView = = null) {MView = Inflater.inflate (R.layout.right_menu, container, false);} return MView;}}
Fragment on the right side of the slide menu. A layout was loaded. Did not do whatever event handling ~

And finally the mainactivity.

Package Com.zhy.zhy_slidemenu_demo04;import Java.util.arraylist;import Java.util.list;import android.os.Bundle; Import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentpageradapter;import Android.support.v4.view.viewpager;import Android.view.view;import Android.view.window;import Com.jeremyfeinstein.slidingmenu.lib.slidingmenu;import Com.jeremyfeinstein.slidingmenu.lib.app.slidingfragmentactivity;public class Mainactivity extends Slidingfragmentactivity{private viewpager mviewpager;private fragmentpageradapter mAdapter;private List<Fragment > mfragments = new arraylist<fragment> (), @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); This.requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_main);//Initialize Slidemenuinitrightmenu ();//Initialize Viewpagerinitviewpager ();} private void Initviewpager () {Mviewpager = (Viewpager) Findviewbyid (R.id.id_viewpager); MAINTAB01 tab01 = new MainTab01 (); MAINTAB02 TAB02= new MAINTAB02 (); MAINTAB03 tab03 = new MainTab03 (); Mfragments.add (TAB01); Mfragments.add (TAB02); Mfragments.add (TAB03);/** * Initialize Adapter */madapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {@Overridepublic int getcount () {return Mfragments.size ();} @Overridepublic Fragment getItem (int arg0) {return mfragments.get (arg0);}}; Mviewpager.setadapter (Madapter);} private void Initrightmenu () {Fragment leftmenufragment = new Menuleftfragment (); Setbehindcontentview (R.layout.left_ Menu_frame); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.id_left_menu_frame, leftmenufragment). Commit (); Slidingmenu menu = Getslidingmenu (); Menu.setmode (slidingmenu.left_right);//Set Touch screen mode Menu.settouchmodeabove ( Slidingmenu.touchmode_margin); Menu.setshadowwidthres (r.dimen.shadow_width); Menu.setshadowdrawable ( R.drawable.shadow);//Set the width of the Sliding menu view menu.setbehindoffsetres (r.dimen.slidingmenu_offset);// Sets the value of the fade-in gradient effect Menu.setfadedegree (0.35f);//Menu.setbehindscrollscale (1.0f); Menu.setsecondaryshadowdrawable (R.DRawable.shadow)///Set Right (level two) side slide menu Menu.setsecondarymenu (r.layout.right_menu_frame); Fragment rightmenufragment = new Menurightfragment (); Getsupportfragmentmanager (). BeginTransaction (). Replace ( R.id.id_right_menu_frame, Rightmenufragment). commit ();} public void Showleftmenu (view view) {Getslidingmenu (). ShowMenu ();} public void Showrightmenu (view view) {Getslidingmenu (). Showsecondarymenu ();}}

To explain briefly, Mainactivity inherits the Slidingfragmentactivity. In activity fragmentpageradapter and Viewpager as the main layout, and then respectively initialize the menu on both sides of the slidingmenu.


Haha, lying on the gun again ~ ~ This sample should be able to meet the needs of the general app.

5, Slidingmenu Some of the frequently used attributes

Set the position of the side slide menu, optional value left, right, Left_right (set when both sides have a menu)

Menu.setmode (Slidingmenu.left_right);

Set the mode of touch screen, optional only margin, CONTENT
Menu.settouchmodeabove (Slidingmenu.touchmode_margin);

Set the width of the shadow according to the ID of the dimension resource file
Menu.setshadowwidthres (R.dimen.shadow_width);

Set the shadow Effect of the swipe menu based on the resource file ID
Menu.setshadowdrawable (R.drawable.shadow);

Both of these are set to the width of the sliding menu view, and the second option
Set the offset of the slidingmenu from the screen
Menu.setbehindoffsetres (R.dimen.slidingmenu_offset);
Set width
Menu.setbehindwidth ()

Set the value of the fade-in fade-out effect
Menu.setfadedegree (0.35f);

Sets the speed ratio of slidingmenu to the movement of the lower view, when it moves at the same time as 1 o'clock. Value 0-1
Menu.setbehindscrollscale (1.0f);

Set the shadow effect on a level two menu
Menu.setsecondaryshadowdrawable (R.drawable.shadow);

Set right (Level two) Sideways menu
Menu.setsecondarymenu (R.layout.right_menu_frame);

Set the layout for the side-slip menu
Menu.setmenu (R.layout.leftmenu);

Add the sliding menu to the entire activity. An optional value of sliding_content. Sliding_window
Menu.attachtoactivity (this, slidingmenu.sliding_content);

There are also several listener events:

/** * Set Open listener event when the sliding menu is opened call */public void Setonopenlistener (Onopenlistener listener) {Mopenlistener = listener;} /** * Set Shutdown listener event. Called */public void Setoncloselistener (Oncloselistener listener) {//mviewabove.setoncloselistener (listener) when the sliding menu is closed; Mcloselistener = listener;} /** * Set Open listener event. When the sliding menu is opened, call */public void Setonopenedlistener (Onopenedlistener listener) {Mviewabove.setonopenedlistener (listener );} /** * Set the shutdown listener event to call */public void Setonclosedlistener (Onclosedlistener listener) After the slide menu has been closed Mviewabove.setonclosedlistener (listener);}

While others use it, check it out.


Have no matter what question ~ Welcome message ~


I put all of the above examples including Slidingmenu Lib dozen a package ~

Source code click to download




Android Slidingmenu Use specific explanations

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.