How to Use the slide menu SlidingMenu ?, Slide menu slidingmenu
Slide left:
Slide on the right side:
Click here to download the source code
Previously, I used a slide menu customized by an online friend. The overwrite layout is used to control and implement the layout, but when the project is written 80% at the end. I suddenly found a conflict with my functions. I can't solve this messy code. So there is no way, just search online, three days later. Finished, and successfully embedded into my project. The test demo is shown in figure! Do not use custom slide menus with curiosity. When your project is about to close items, you can use this slide menu to conflict with your project functions, and you cannot solve the problem, at this time, you are crazy.
Therefore, it is more durable and used by people. When problems occur in your project, you can solve them quickly;
Okay. Let's see how to use it!
/** SlidingMenu common attributes: */menu. setMode (SlidingMenu. LEFT); // set the SlidingMenu of the LEFT slide menu. RIGHT SlidingMenu. slide right/left/right navigation menu. setTouchModeAbove (sregistringmenu. TOUCHMODE_FULLSCREEN); // sets the screen limitations of sliding. you can slide the menu in full screen mode. setShadowDrawable (R. drawable. shadow); // sets the shadow menu. setShadowWidthRes (R. dimen. shadow_width); // set the width of the Shadow menu. setBehindOffsetRes (R. dimen. slidingmenu_offset); // The remaining width menu displayed on the home page when SlidingMenu is selected. setBehindWidth (400); // set the width of the SlidingMenu menu. setFadeDegree (0.35f); // gradient menu of SlidingMenu. attachToActivity (this, SlidingMenu. SLIDING_CONTENT); // append SlidingMenu to the Activity menu. setMenu (R. layout. menu_layout); // sets the layout file menu of the menu. toggle (); // dynamically determines whether to actively close or enable SlidingMenumenu. showMenu (); // display SlidingMenumenu. showContent (); // display the content menu. setOnOpenListener (onOpenListener); // There are two enable menu closure in slidingmenu. In simple terms, for the menu close transaction, one is when and the other is after menu. onClosedListener (OnClosedListener); // slidingmenu closes the current affairs menu. onClosedListener (OnClosedListener); // transaction after slidingmenu is closed
The following describes how to use the slide menu in the left direction:
public class SlidingExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle(R.string.attach); // set the content view setContentView(R.layout.content); // configure the SlidingMenu SlidingMenu menu = new SlidingMenu(this); menu.setMode(SlidingMenu.LEFT); menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); menu.setShadowWidthRes(R.dimen.shadow_width); menu.setShadowDrawable(R.drawable.shadow); menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); menu.setFadeDegree(0.35f); menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); menu.setMenu(R.layout.menu); }}
If you want to set the Left and Right menus to slide, see here:
Public class MainActivity extends FragmentActivity {public SlidingMenu mSlidingMenu; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mSlidingMenu = new SlidingMenu (this); mSlidingMenu. setMode (SlidingMenu. LEFT_RIGHT); mSlidingMenu. setBehindOffsetRes (R. dimen. slidingmenu_offset); mSlidingMenu. setTouchModeAbove (sregistringmenu. TOUCHMODE_FULLSCREEN); mSlidingMenu. attachToActivity (this, SlidingMenu. SLIDING_CONTENT); mSlidingMenu. setMenu (R. layout. slidemenu_primary); // The left side slides mSlidingMenu. setSecondaryMenu (R. layout. slidemenu_secondery); // slide getsuppfrfragmentmanager () on the right (). beginTransaction (). replace (R. id. aty_main_framLayout, new ViewPagerFragment ()). commit (); getSupportFragmentManager (). beginTransaction (). replace (R. id. framLayout_fragment, new TranslationAnimationFragment ()). commit ();}/** rewrite the return key and menu key, and control the menu to slide and close */@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stubswitch (keyCode) {case KeyEvent. KEYCODE_BACK: if (mSlidingMenu. isMenuShowing () mSlidingMenu. showContent (); else finish (); return true; case KeyEvent. KEYCODE_MENU: mSlidingMenu. showSecondaryMenu (); return true;} return super. onKeyDown (keyCode, event );}}
Next let's take a look at how to solve the conflict between the sliding between the side and the sliding between ViewPager: "Of course, my source code shows how to use it."
mSlidingMenu.addIgnoredView(viewPager);
Before your Viewpager content is displayed, call the above method and you will be OK,
SlidingMenu. setTouchModeAbove (). there are three gesture modes in total: TOUCHMODE_FULLSCREEN full screen mode. You can also enable SlidingMenuTOUCHMODE_MARGIN edge mode through gestures in the body layout, at the edge of the text layout, you can use gestures to open SlidingMenuTOUCHMODE_NONE. Naturally, SlidingMenu cannot be opened through gestures.
When you set shadow
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:centerColor="#11000000" android:endColor="#00000000" android:startColor="#33000000" /></shape>
Implementation of Slide menus using DrawerLayout is different from SlidingMenu
After reading the slidingmenu source code, you will know that the difference is that slidingmenu also divides the view into two layers, but the top layer is the main content page, and the next layer is the menu page. The implementation of drawerlayout is the opposite. The menu page is on the Content Page. According to my understanding, the menu width of slidingmenu can be determined by the distance between the menu and the menu target, while drawerlayout is direct. view the original post for the menu size>
Now, is it better for android to use the SlidingMenu open-source project to slide the menu bar, or is it better to use the Navigation Drawer that comes with android?
If you are learning the code, it is recommended to study the official version. However, SlidingMenu has many functions, such as controlling the background behind the menu when sliding out. It seems that the background of Navigation Drawer cannot be moved. It is a trend to replace open-source projects with official APIs, because everyone thinks that official interfaces are safe to use, but open-source projects may be more flexible.