Android Side-Sliding Menu - Detailed Use of Sliding Menu

Keywords: Android Attribute Gradle Fragment

brief introduction

Sliding Menu is an Android-side sliding menu component. It has powerful functions and simple settings. It can also set menu shadows, gradients, scrolling modes and so on.
Sliding Menu is an open source Android project on github that can be used to quickly integrate Android sideslip menu effects
Sliding menu can contain multiple components such as fragment viewpager listview.

Environment building

  1. Download Sliding Menu Click to download
  2. New AS project, copy the library folder in Sliding Menu-master (I renamed this folder sliding_menu) to the new project folder at the same level as the app folder
  3. Add include': sliding_menu'to the settings.gradle file

include ':sliding_menu'
4. Add a dependent compile project(': sliding_menu') to build.gradle(Module:app)
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.2'
compile project(':sliding_menu')
}
5. At the end of Step 4, I prompted you that there was an error. Modify some parameters in sliding_menu's build.gradle, such as compileSdkVerdion, build Tools Version, to match your AS (refer to app's build.gradle).
Settings in

  1. Build / Make Project (Sliding Menu has been added successfully by this point)

Slidingmenu provides attribute modification

1 Initialize Slidingmenu:
SlidingMenu menu = getSlidingMenu();

2 setMode // Set Sliding Mode
Set whether to slide left or right or left or right.
menu.setMode(SlidingMenu.LEFT); //Set the left-slide menu
Menu. setMode (Sliding Menu. LEFT_RIGHT); // Settings are slidable left and right
menu.setMode(SlidingMenu.TOUCHMODE_NONE); // Can't gesture sliding

3 Set Sliding Menu Shadow
menu.setShadowWidthRes(R.dimen.shadow_width); //Set the width of the shadow image
Menu. setSecondary Shadow Drawable (R. drawable. shadowright); // Setting Image Resources for Right Menu Shadow
Menu.setShadow Drawable (R.drawable.shadow); //Set the left menu shadow picture

4 Set Sliding Menu Border Distance
Menu. setBehindOffsetRes (); / / Sliding Menu's remaining width from the border when sliding out
menu.setBehindOffset(getWindowManager().getDefaultDisplay().getWidth() / 5);
// getWindows Manager (). getDefaultDisplay (). getWidth ()/ 5 represents one fifth of the screen width.

5 Setting Sliding Menu Gradient
menu.setFadeEnabled(true); / Is there a gradient?
menu.setFadeDegree(0.35f);//Set Gradient Ratio

6 Setting Sliding Menu Layout

sm = getSlidingMenu();  
setBehindContentView(R.layout.main_menu_frame);  
sm.setSecondaryMenu(R.layout.main_menu_frame_two);  
messageFragment = new MessageFragment();  
profileFragment = new ProfileFragment();  
        getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, messageFragment).commit();  
        getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame_two, profileFragment).commit();  

There are three ways to set up the layout:

setContentView(R.layout.main); //Set the layout in the middle of sliding menu
setBehindContentView(R.layout.main_menu_frame); // Set the layout on the left
Sm.setSecondary Menu (R.layout.main_menu_frame_two);//Set the layout on the right side

I use the left and right sides. I usually set it to fragment.
getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame,messageFragment).commit();
Represented as replacing fragment

7 Setting the drag effect of Sliding Menu sliding
slidingMenu.setBehindScrollScale(0);

8 Set Sliding Menu to determine the open state and automatically close or open
menu.toggle();
If Sliding Menu is open, it will be closed, and vice versa.

9 Set the range of Sliding Menu touching the screen
menu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
Set the menu to slide, touch the screen range setTouchModeAbove

10 Setting Sliding Menu Closer Monitor
There are two main types of open and close listening

open:
menu.setOnOpenedListener(onOpenListener); //listen for slidingmenu calls after opening
menu.setOnOpenListener(onOpenListener); //listen for slidingmenu calls when opened

close:
Two listeners noticed that one was closed and the other was close.
menu.setOnClosedListener(listener);
menu.setOnCloseListener(listener);

The difference between the two is that
menu.OnCloseListener(OnClosedListener); //Listen for events when Slidingmenu closes
menu.OnClosedListener(OnClosedListener); //Listen for events after Slidingmenu closes

11 Set Sliding Menu to fade in/out
menu.setFadeEnabled(true);

** Key Contents** 12 Other
menu.showMenu(); // Display SlidingMenu
menu.showContent();// Display Content
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); //Make SlidingMenu attached to Activity
menu.setMenu(R.layout.menu);//Set menu layout file

Common problem

Tips do not support similar methods such as getSupportActionBar

Solution: Modify the Sliding FragmentActiviyt class in SlideMenu to inherit Sherlock FragmentActivity such as class Sliding FragmentActivity extends Sherlock FragmentActivity implements Sliding Activity Base

After running, it was always prompted that no related classes could be found.

Solution: This problem has been troubling me for a long time. Because android-support-v4.jar is used in every open source library, we need to incorporate the latest version of this. jar package into the libs folder in the workplace to unify their versions.

Code case

1. Sliding Menu is used to construct side-slip menu directly in Activity

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  
{  

    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        // configure the SlidingMenu  
        SlidingMenu menu = new SlidingMenu(this);  
        menu.setMode(SlidingMenu.LEFT);  
        // Setting the mode of 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);  
        // Set the value of the fade-in and fade-out effect  
        menu.setFadeDegree(0.35f);  
        /** 
         * SLIDING_WINDOW will include the Title/ActionBar in the content 
         * section of the SlidingMenu, while SLIDING_CONTENT does not. 
         */  
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);  
        //Set the layout for the sideslip menu  
        menu.setMenu(R.layout.leftmenu);  
    }   
}  

2. Inherit Sliding Activity by Inheriting Activity
a. Inheriting Sliding Activity
b, and then set BehindContentView (R. layout. leftmenu) in onCreate; set the layout of the sideslip menu
c. Get the SlidingMenu object through getSlidingMenu(), and 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  
{  
    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        setBehindContentView(R.layout.leftmenu);  
        // configure the SlidingMenu  
        SlidingMenu menu = getSlidingMenu();  
        menu.setMode(SlidingMenu.LEFT);  
        // Setting the mode of 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);  
        // Set the value of the fade-in and fade-out effect  
        menu.setFadeDegree(0.35f);  

    } 
}  

3. Treat Sliding Menu as a Common Control

<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_content"  
        android:layout_marginLeft="30dp"  
        android:layout_marginTop="30dp" >  


        <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu  
            xmlns:sliding="http://schemas.android.com/apk/res-auto"  
            android:id="@+id/slidingmenulayout"  
            android:layout_width="120dp"  
            android:layout_height="170dp"  
            android:background="#ffffffff"  
            sliding:behindOffset="0dp"  
            sliding:behindScrollScale="1"  
            sliding:fadeDegree="0.3"  
            sliding:fadeEnabled="true"  
            sliding:touchModeAbove="fullscreen"  
            sliding:viewAbove="@layout/pic" />  
    </LinearLayout>  

</RelativeLayout>  

We SlidingMenu set the layout in the layout file as a normal View, and set the value of viewAbove to another layout.
Look at the 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" />  

It's a picture of a sister.

Finally, 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;

@Override  
protected 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);  
    // Setting the mode of touch screen  
    mLeftMenu.setShadowWidthRes(R.dimen.shadow_width);  
    mLeftMenu.setShadowDrawable(R.drawable.shadow);  

    mLeftMenu.setMenu(R.layout.leftmenu);  

    mLeftMenu.setOnClickListener(new OnClickListener()  
    {  
        @Override  
        public void onClick(View v)  
        {  
            if (mLeftMenu.isMenuShowing())  
                mLeftMenu.toggle();  
        }  
    });  
    // Set the width of the sliding menu view  
    // Set the value of the fade-in and fade-out effect  
}  

Posted by dayang on Sat, 23 Mar 2019 10:39:52 -0700