Android-menudrawer-master open source powerful sidebar menu

Android-menudrawer-master open source powerful sidebar menu

Source code introduction

There are different styles of menu display on Android, among which the most used and experienced one is to slide left and right to display the hidden menu. android-menudrawer is a sliding menu implementation that allows users to achieve seamless navigation in the application. This project has a variety of menu display effects, the most common of which is to display it by dragging the edge of the screen or clicking the "up" button of the action bar.

Source code running screenshot

Source code snippet

  1. package net.simonvt.menudrawer.samples;
  2.   
  3. import net.simonvt.menudrawer.MenuDrawer;
  4. import net.simonvt.menudrawer.Position;
  5.   
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.support.v4.app.Fragment;
  9. import android.support.v4.app.FragmentManager;
  10. import android.support.v4.app.FragmentTransaction;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.TextView;
  15.   
  16. public   class FragmentSample extends BaseListSample {
  17.   
  18. private   static   final String STATE_CURRENT_FRAGMENT = "net.simonvt.menudrawer.samples.FragmentSample" ;
  19.   
  20. private FragmentManager mFragmentManager;
  21. private FragmentTransaction mFragmentTransaction;
  22.   
  23. private String mCurrentFragmentTag;
  24.   
  25. @Override  
  26. protected   void onCreate(Bundle inState) {
  27. super .onCreate(inState);
  28.   
  29. mFragmentManager = getSupportFragmentManager();
  30.   
  31. if (inState != null ) {
  32. mCurrentFragmentTag = inState.getString(STATE_CURRENT_FRAGMENT);
  33. } else {
  34. mCurrentFragmentTag = ((Item) mAdapter.getItem( 0 )).mTitle;
  35. attachFragment(mMenuDrawer.getContentContainer().getId(), getFragment(mCurrentFragmentTag),
  36. mCurrentFragmentTag);
  37. commitTransactions();
  38. }
  39.   
  40. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  41. getActionBar().setDisplayHomeAsUpEnabled( true );
  42. }
  43.   
  44. mMenuDrawer.setOnDrawerStateChangeListener( new MenuDrawer.OnDrawerStateChangeListener() {
  45. @Override  
  46. public   void onDrawerStateChange( int oldState, int newState) {
  47. if (newState == MenuDrawer.STATE_CLOSED) {
  48. commitTransactions();
  49. }
  50. }
  51.   
  52. @Override  
  53. public   void onDrawerSlide( float openRatio, int offsetPixels) {
  54. // Do nothing  
  55. }
  56. });
  57. }
  58.   
  59. @Override  
  60. protected   void onSaveInstanceState(Bundle outState) {
  61. super .onSaveInstanceState(outState);
  62. outState.putString(STATE_CURRENT_FRAGMENT, mCurrentFragmentTag);
  63. }
  64.   
  65. @Override  
  66. protected   void onMenuItemClicked( int position, Item item) {
  67. if (mCurrentFragmentTag != null ) detachFragment(getFragment(mCurrentFragmentTag));
  68. attachFragment(mMenuDrawer.getContentContainer().getId(), getFragment(item.mTitle), item.mTitle);
  69. mCurrentFragmentTag = item.mTitle;
  70. mMenuDrawer.closeMenu();
  71. }
  72.   
  73. @Override  
  74. protected   int getDragMode() {
  75. return MenuDrawer.MENU_DRAG_WINDOW;
  76. }
  77.   
  78. @Override  
  79. protected Position getDrawerPosition() {
  80. return Position.LEFT;
  81. }
  82.   
  83. protected FragmentTransaction ensureTransaction() {
  84. if (mFragmentTransaction == null ) {
  85. mFragmentTransaction = mFragmentManager.beginTransaction();
  86. mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  87. }
  88.   
  89. return mFragmentTransaction;
  90. }
  91.   
  92. private Fragment getFragment(String tag) {
  93. Fragment f = mFragmentManager.findFragmentByTag(tag);
  94.   
  95. if (f == null ) {
  96. f = SampleFragment.newInstance(tag);
  97. }
  98.   
  99. return f;
  100. }
  101.   
  102. protected   void attachFragment( int layout, Fragment f, String tag) {
  103. if (f != null ) {
  104. if (f.isDetached()) {
  105. ensureTransaction();
  106. mFragmentTransaction.attach(f);
  107. } else   if (!f.isAdded()) {
  108. ensureTransaction();
  109. mFragmentTransaction.add(layout, f, tag);
  110. }
  111. }
  112. }
  113.   
  114. protected   void detachFragment(Fragment f) {
  115. if (f != null && !f.isDetached()) {
  116. ensureTransaction();
  117. mFragmentTransaction.detach(f);
  118. }
  119. }
  120.   
  121. protected   void commitTransactions() {
  122. if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
  123. mFragmentTransaction.commit();
  124. mFragmentTransaction = null ;
  125. }
  126. }
  127.   
  128. public   static   class SampleFragment extends Fragment {
  129.   
  130. private   static   final String ARG_TEXT = "net.simonvt.menudrawer.samples.SampleFragment.text" ;
  131.   
  132. public   static SampleFragment newInstance(String text) {
  133. SampleFragment f = new SampleFragment();
  134.   
  135. Bundle args = new Bundle();
  136. args.putString(ARG_TEXT, text);
  137. f.setArguments(args);
  138.   
  139. return f;
  140. }
  141.   
  142. @Override  
  143. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  144. View v = inflater.inflate(R.layout.fragment_sample, container, false );
  145.   
  146. ((TextView) v.findViewById(R.id.text)).setText(getArguments().getString(ARG_TEXT));
  147.   
  148. return v;
  149. }
  150. }
  151. }

Source code link: http://download..com/data/1968347

<<:  Android custom ListView to implement contact sorting

>>:  WP10 may be renamed Windows Mobile 10, weird name?

Recommend

There is a "microbial city" in our mouth

The invention of the microscope first expanded pe...

Understand what product operation is in 10 minutes!

What is operation ? All means used to connect use...

I'm tired of waiting and found out that iOS 9.2 beta4 is the official version

After 3 weeks of waiting, we finally welcomed the...

Three tips for SMS push elements based on the 5W2H method

Here is a summary first. Please scroll to the bot...