Android Slidingmenu use and sample detailed _android

Source: Internet
Author: User
Tags commit

Many apps have the function of Sideslip menu, part of the app is sideslip menu ~slidingmenu This open source project can help us to achieve sideslip, if the slidingmenu is not very familiar with the children's shoes, you can refer to the next blog. A lot of ways to introduce the Sideslip menu into the project, this blog first through a number of examples to introduce the way, and then show you an example: the main layout viewpager, the left and right side of the use of a sideslip menu, almost already can meet the needs of most applications. About common properties, described at the end of the article.

1, in the activity through the Slidingmenu construction method, directly set the Sideslip menu

Package Com.zhy.zhy_slidemenu_demo; 
Import androidappactivity; 
 
Import Androidosbundle; 
 
Import Comjeremyfeinsteinslidingmenulibslidingmenu; 
    public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) { 
    Superoncreate (savedinstancestate); 
 
    Setcontentview (Rlayoutactivity_main); 
    Configure the Slidingmenu slidingmenu menu = new Slidingmenu (this); 
    Menusetmode (Slidingmenuleft); 
    Set the touch screen mode menusettouchmodeabove (Slidingmenutouchmode_fullscreen); 
    Menusetshadowwidthres (Rdimenshadow_width); 
 
    Menusetshadowdrawable (Rdrawableshadow); 
    Sets the width of the Sliding menu view menusetbehindoffsetres (Rdimenslidingmenu_offset); 
    Set the value of the gradient menusetfadedegree (35f); /** * Sliding_window would include the Title/actionbar in the content * "The Slidingmenu, while Slidi 
    Ng_content does not */menuattachtoactivity (this, slidingmenusliding_content); Set Cloth for Sideslip menuBureau Menusetmenu (Rlayoutleftmenu); 
 } 
 
}

Effect Chart:

Is it particularly simple ~ a few lines of code to fix ~ ha ~

2, by the activity to inherit slidingactivity

A, Inherit slidingactivity

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

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

Package com.zhy.zhy_slidemenu_demo02; 
 
Import Androidosbundle; 
Import Comjeremyfeinsteinslidingmenulibslidingmenu; 
 
Import comjeremyfeinsteinslidingmenulibappslidingactivity; 
    public class Mainactivity extends slidingactivity {@Override public void onCreate (Bundle savedinstancestate) { 
    Superoncreate (savedinstancestate); 
 
    Setcontentview (Rlayoutactivity_main); 
    Setbehindcontentview (Rlayoutleftmenu); 
    Configure the Slidingmenu slidingmenu menu = Getslidingmenu (); 
    Menusetmode (Slidingmenuleft); 
    Set the touch screen mode menusettouchmodeabove (Slidingmenutouchmode_fullscreen); 
    Menusetshadowwidthres (Rdimenshadow_width); 
 
    Menusetshadowdrawable (Rdrawableshadow); 
    Sets the width of the Sliding menu view menusetbehindoffsetres (Rdimenslidingmenu_offset); 
    Set the value of the gradient menusetfadedegree (35f); /** * Sliding_window would include the Title/actionbar in the content * "The Slidingmenu, while Slidi 
Ng_content does not * *    Menuattachtoactivity (this, slidingmenusliding_content); 
 
  Menusetmenu (Rlayoutleftmenu); 
 } 
 
}

The effect chart is the same as the first way ~ is not also very simple ~ ~

3, the Slidingmenu as a normal control

You can put slidingmenu as a normal view, and then in the layout of the statement, insane play ~ Look at the following example:

<relativelayout xmlns:android= "http://schemasandroidcom/apk/res/android" xmlns:tools= "http:// Schemasandroidcom/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_content "Android:layout_marginleft=" 30DP "android:layout_margintop=" 30DP "> <comjeremyfeinsteinslidingme 
      Nulibslidingmenu xmlns:sliding= "Http://schemasandroidcom/apk/res-auto" android:id= "@+id/slidingmenulayout" Android:layout_width= "120DP" android:layout_height= "170DP" android:background= "#ffffffff" Slidi  
      ng:behindoffset= "0DP" sliding:behindscrollscale= "1" sliding:fadedegree= "3" sliding:fadeenabled= "true" Sliding:touchmodeabove= "fullscreen" sliding:viewabove= "@layout/pic"/> </LinearLayout> </ 

 Relativelayout>

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

See pic Layout below:

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

is a picture of a sister.

Finally see the main activity:

Package comzhyzhy_slidemenu_demo03; 
Import androidappactivity; 
Import Androidosbundle; 
Import Androidviewview; 
 
Import Androidviewviewonclicklistener; 
 
Import Comjeremyfeinsteinslidingmenulibslidingmenu; 
 
  public class Mainactivity extends activity {private Slidingmenu mleftmenu; 
    @Override protected void OnCreate (Bundle savedinstancestate) {superoncreate (savedinstancestate); 
 
    Setcontentview (Rlayoutactivity_main); 
    Mleftmenu = (slidingmenu) Findviewbyid (ridslidingmenulayout); 
    Configure the Slidingmenu//slidingmenu menu = new Slidingmenu (this); 
    Mleftmenusetmode (Slidingmenuleft); 
    Set the touch screen mode mleftmenusetshadowwidthres (rdimenshadow_width); 
 
    Mleftmenusetshadowdrawable (Rdrawableshadow); 
 
    Mleftmenusetmenu (Rlayoutleftmenu); 
        Mleftmenusetonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { if (mleftmenuismenushowing ()) Mleftmenutoggle (); 
    } 
    }); Set the width of the Slide menu view//Set the value of the fade effect/** * Sliding_window would include the Title/actionbar in the content * 

 section of the Slidingmenu, while Sliding_content does is not */}}

Effect Chart:

By Slidingmenu to the picture to set a slide show introduction, you can also slide show anything, download, share buttons or something. Picture a lot of time do not know how efficient, you can play like this, but do not recommend ha ~

4, Slidingmenu set the left and right side sideslip Menu Example

The 3 methods described above, the slidingmenu of the control in the layout of the event need to be written in the activity, so the code is more bloated, generally use fragment as a side-slip menu layout container.

Core code:

Fragment leftmenufragment = new Menuleftfragment (); 
    Setbehindcontentview (rlayoutleft_menu_frame); 
    Getsupportfragmentmanager () BeginTransaction () 
        replace (ridid_left_menu_frame, leftmenufragment) commit (); 
    Slidingmenu menu = Getslidingmenu (); 
    Menusetmode (slidingmenuleft_right); 
    Set the touch screen mode 
    menusettouchmodeabove (slidingmenutouchmode_margin); 

First give the Sideslip menu through Setbehindcontentview (r.layout.left_menu_frame); Set a layout with only one framelayout in this layout, Then use Fragmentmanager to replace fragment with this fragment, so this fragment is the layout of our sideslip menu, and our event handling code can also be written in fragement, not in the activity.

Here's a concrete example:

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

Menuleftfragment

Package comzhyzhy_slidemenu_demo04; 
Import Javautilarrays; 
 
Import javautillist; 
Import Androidosbundle; 
Import androidsupportvappfragment; 
Import Androidviewlayoutinflater; 
Import Androidviewview; 
Import Androidviewviewgroup; 
Import Androidwidgetarrayadapter; 
Import Androidwidgetlistadapter; 
 
Import Androidwidgetlistview; 
  public class Menuleftfragment extends Fragment {private View mview; 
  Private ListView mcategories; 
  Private list<string> Mdatas = Arrays aslist ("chat", "Discovery", "Address Book", "Circle of Friends", "subscription number"); 
 
  Private ListAdapter Madapter;  
    @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { 
    if (Mview = = null) {Initview (Inflater, container); 
  return mview; } private void Initview (Layoutinflater inflater, ViewGroup container) {Mview = Inflaterinflate (rlayoutleft_m 
    ENU, container, false); Mcategories = (ListView) Mview FINDVIEWBYid (ridid_listview_categories); 
    Madapter = new Arrayadapter<string> (getactivity (), androidrlayoutsimple_list_item_1, MDatas); 
  Mcategoriessetadapter (Madapter); 
 } 
}

The left side of the Sideslip layout is a ListView, the code is relatively simple ~

Menurightfragment

Package comzhyzhy_slidemenu_demo04; 
 
Import Androidosbundle; 
Import androidsupportvappfragment; 
Import Androidviewlayoutinflater; 
Import Androidviewview; 
Import Androidviewviewgroup; 
 
public class Menurightfragment extends Fragment 
{ 
  private View mview; 
 
  @Override public 
  View Oncreateview (layoutinflater inflater, ViewGroup container, 
      Bundle savedinstancestate 
  { 
    if (Mview = = null) 
    { 
      Mview = inflaterinflate (Rlayoutright_menu, container, false); 
    } 
    Return Mview 
  } 
} 

The right side of the Sideslip menu fragment, loaded with a layout, did not do any event processing ~

And finally, mainactivity.

Package comzhyzhy_slidemenu_demo04; 
Import javautilarraylist; 
 
Import javautillist; 
Import Androidosbundle; 
Import androidsupportvappfragment; 
Import Androidsupportvappfragmentpageradapter; 
Import Androidsupportvviewviewpager; 
Import Androidviewview; 
 
Import Androidviewwindow; 
Import Comjeremyfeinsteinslidingmenulibslidingmenu; 
 
Import comjeremyfeinsteinslidingmenulibappslidingfragmentactivity; 
  public class Mainactivity extends Slidingfragmentactivity {private Viewpager mviewpager; 
  Private Fragmentpageradapter Madapter; 
 
  Private list<fragment> mfragments = new arraylist<fragment> (); 
    @Override public void OnCreate (Bundle savedinstancestate) {superoncreate (savedinstancestate); 
    Thisrequestwindowfeature (Windowfeature_no_title); 
    Setcontentview (Rlayoutactivity_main); 
    Initialization of Slidemenu Initrightmenu (); 
 
  Initialization of Viewpager Initviewpager (); private void Initviewpager () {Mviewpager = (Viewpager) Findviewbyid (Ridid_viewpager); 
    MAINTAB01 tab01 = new MainTab01 (); 
    MAINTAB02 tab02 = new MAINTAB02 (); 
    MAINTAB03 tab03 = new MainTab03 (); 
    Mfragmentsadd (TAB01); 
    Mfragmentsadd (TAB02); 
    Mfragmentsadd (TAB03); /** * Initialize Adapter * * Madapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {@Overr 
      IDE public int GetCount () {return mfragmentssize (); 
      @Override public Fragment getitem (int arg0) {return mfragmentsget (arg0); 
    } 
    }; 
  Mviewpagersetadapter (Madapter); 
    private void Initrightmenu () {Fragment leftmenufragment = new Menuleftfragment (); 
    Setbehindcontentview (Rlayoutleft_menu_frame); 
    Getsupportfragmentmanager () BeginTransaction () replace (ridid_left_menu_frame, leftmenufragment) commit (); 
    Slidingmenu menu = Getslidingmenu (); 
    Menusetmode (Slidingmenuleft_right); Set the mode of the touch screen MenusettouchModeabove (Slidingmenutouchmode_margin); 
    Menusetshadowwidthres (Rdimenshadow_width); 
    Menusetshadowdrawable (Rdrawableshadow); 
    Sets the width of the Sliding menu view menusetbehindoffsetres (Rdimenslidingmenu_offset); 
    Set the value of the gradient menusetfadedegree (35f); 
    Menusetbehindscrollscale (0f); 
    Menusetsecondaryshadowdrawable (Rdrawableshadow); 
    Set the right (Level two) sideslip menu Menusetsecondarymenu (rlayoutright_menu_frame); 
    Fragment rightmenufragment = new Menurightfragment (); 
  Getsupportfragmentmanager () BeginTransaction () replace (ridid_right_menu_frame, rightmenufragment) commit (); 
  public void Showleftmenu (view view) {Getslidingmenu () ShowMenu (); 
  public void Showrightmenu (view view) {Getslidingmenu () Showsecondarymenu (); 

 } 
}

Briefly, Mainactivity inherits Slidingfragmentactivity, Fragmentpageradapter and Viewpager as the primary layout in the activity, Then initialize the menus on both sides of the slidingmenu separately.

Effect Chart:

Haha, micro-letter and lying on the gun ~ ~ This example should be able to meet the needs of the general app.

5, some common properties of Slidingmenu

Set the position of the Sideslip menu, optional value left, right, Left_right (set on both sides of the menu)

Menu.setmode (slidingmenu.left_right);

Set the mode of touch screen, optional only MARGIN, CONTENT 
menu.settouchmodeabove (slidingmenu.touchmode_margin);

Sets the width of the shadow
Menu.setshadowwidthres (R.dimen.shadow_width) based on the ID of the dimension resource file;

Sets the shadow effect of the sliding menu
menu.setshadowdrawable (R.drawable.shadow) According to the resource file ID;

Both are set to the width of the Slide menu view, two Select one
//set Slidingmenu offset from the screen
menu.setbehindoffsetres (r.dimen.slidingmenu_offset);
Set width
menu.setbehindwidth ()

//Set the value of the gradient gradient
menu.setfadedegree (0.35f);

Sets the speed ratio of the slidingmenu to the move of the lower view, and moves at the same time as 1 o'clock, taking the value 0-1
menu.setbehindscrollscale (1.0f);

Set the shadow Effect of level two menu
menu.setsecondaryshadowdrawable (R.drawable.shadow);

Set the right (Level two) sideslip menu
menu.setsecondarymenu (r.layout.right_menu_frame);

Set the layout
Menu.setmenu (r.layout.leftmenu) for the Sideslip menu;

Add the sliding menu to all activity, optional value sliding_content, Sliding_window
menu.attachtoactivity (This, slidingmenu.sliding_ CONTENT);

There are also several listening events:

/** 
   * Setting turns on the listener event and calls 
  /public void Setonopenlistener (Onopenlistener listener) 
    When the sliding menu is opened Mopenlistener = Listener; 
  } 
 
  /** 
   * Settings turn off the listener event and call/public 
  void Setoncloselistener (Oncloselistener listener) When the sliding menu is 
    closed Mviewabovesetoncloselistener (listener); 
    Mcloselistener = Listener; 
  } 
 
  /** 
   * Setting turns on the listener event and calls 
  /public void Setonopenedlistener (Onopenedlistener listener) 
    After the sliding menu is opened Mviewabovesetonopenedlistener (listener); 
  } 
 
  /** 
   * Settings turn off the listener event and call 
  /public void Setonclosedlistener (Onclosedlistener listener) 
    When the sliding menu is closed Mviewabovesetonclosedlistener (listener); 
  } 

There are some use of the time to check the ~

I took all of the above examples including Slidingmenu's lib to play a bag ~:demo

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.