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

Camping is getting popular. Poetry and distant places. Is a tent enough?

Review expert: Gu Minghui, a well-known roundtabl...

Marketing skills of online store promotion software

The most mature project on the Internet is Taobao...

2022 National Trend Marketing Rules!

Although the issue of national trends is always m...

Mobile phone industry: Do you know these little-known facts about mobile phones?

What little-known facts about mobile phones do yo...

How is China heading towards the deep sea?

This article was specially produced by China Nati...

What exactly is “Ice No. 7”?

Since the news that people can wear short sleeves...

What new customers can mini program production companies attract?

Customers with sensitive data traffic Free ones a...