首页 > 代码库 > 利用开源SlidingMenu框架实现左右侧滑菜单的功能

利用开源SlidingMenu框架实现左右侧滑菜单的功能

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends SlidingFragmentActivity
{

	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragments = new ArrayList<Fragment>();

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		// 初始化SlideMenu
		initRightMenu();
		// 初始化ViewPager
		initViewPager();

	}

	private void initViewPager()
	{
		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
		MainTab01 tab01 = new MainTab01();
		MainTab02 tab02 = new MainTab02();
		MainTab03 tab03 = new MainTab03();
		mFragments.add(tab01);
		mFragments.add(tab02);
		mFragments.add(tab03);
		/**
		 * 初始化Adapter
		 */
		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
		{
			@Override
			public int getCount()
			{
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0)
			{
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);
	}

	private void initRightMenu()
	{
		
		Fragment leftMenuFragment = new MenuLeftFragment();
		setBehindContentView(R.layout.left_menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.id_left_menu_frame, leftMenuFragment).commit();
		SlidingMenu menu = getSlidingMenu();
		menu.setMode(SlidingMenu.LEFT_RIGHT);
		// 设置触摸屏幕的模式
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		// 设置滑动菜单视图的宽度
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//		menu.setBehindWidth()
		// 设置渐入渐出效果的值
		menu.setFadeDegree(0.35f);
		// menu.setBehindScrollScale(1.0f);
		menu.setSecondaryShadowDrawable(R.drawable.shadow);
		//设置右边(二级)侧滑菜单
		menu.setSecondaryMenu(R.layout.right_menu_frame);
		Fragment rightMenuFragment = new MenuRightFragment();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.id_right_menu_frame, rightMenuFragment).commit();
	}

	public void showLeftMenu(View view)
	{
		getSlidingMenu().showMenu();
	}

	public void showRightMenu(View view)
	{
		getSlidingMenu().showSecondaryMenu();
	}
}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainTab01 extends Fragment
{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return  inflater.inflate(R.layout.main_tab_01, container, false);
	
	}

}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainTab02 extends Fragment
{

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View messageLayout = inflater.inflate(R.layout.main_tab_02, container, false);
		return messageLayout;
	}

}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainTab03 extends Fragment
{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View newsLayout = inflater.inflate(R.layout.main_tab_03, container, false);
		return newsLayout;
	}

}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.Arrays;
import java.util.List;

public class MenuLeftFragment extends Fragment
{
	private View mView;
	private ListView mCategories;
	private List<String> mDatas = Arrays.asList("聊天", "发现", "通讯录", "朋友圈", "订阅号");
	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 = inflater.inflate(R.layout.left_menu, container, false);
		mCategories = (ListView) mView
				.findViewById(R.id.id_listview_categories);
		mAdapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, mDatas);
		mCategories.setAdapter(mAdapter);
	}
}

 

package com.loaderman.slidingmenudemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MenuRightFragment extends Fragment
{
	private View mView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		if(mView == null)
		{
			mView = inflater.inflate(R.layout.right_menu, container, false);
		}
		return mView ;
	}
}

 activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/title_bar">

        <ImageButton
            android:id="@+id/id_iv_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="3dp"
            android:background="@drawable/showright_selector"
            android:onClick="showLeftMenu"/>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="微信"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <ImageButton
            android:id="@+id/id_iv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            android:background="@drawable/showleft_selector"
            android:onClick="showRightMenu"/>
    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewpager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

</LinearLayout>

 left_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/title_bar" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="功能"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/id_listview_categories"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center" />

</LinearLayout>

 left_menu_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_left_menu_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 main_tab_01.xml   main_tab_02.xml   main_tab_03.xml 实现如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ly_main_weixin" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fcfcfc" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="this is first tab !" android:textColor="#000000" android:textSize="30sp" /> </LinearLayout>

 right_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/title_bar" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="设置"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="个人信息"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="二维码名片"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="腾讯微博"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_last_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="添加朋友"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="我的帐号"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/friends_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="聊天背景"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="通用"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="插件"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="黑名单"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="隐私"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_nomal_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="系统通知"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_last_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="流量同级"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_first_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="帮助与反馈"
                    android:textColor="#000"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/settings_last_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="关于微信"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/friends_item"
                    android:clickable="true"
                    android:drawableRight="@drawable/mm_submenu_normal"
                    android:gravity="center_vertical"
                    android:paddingLeft="25dp"
                    android:text="清空聊天记录"
                    android:textColor="#000"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="20dp"
                android:orientation="vertical" >

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="20sp"
                    android:layout_marginRight="20sp"
                    android:background="@drawable/exit_btn"
                    android:gravity="center"
                    android:text="退出登录"
                    android:textColor="#fff"
                    android:textSize="16sp" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

 right_menu_frame.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_right_menu_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 shadow,xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:endColor="#ff444444"
        android:startColor="#00000000" />

</shape>

 

效果图:

技术分享

利用开源SlidingMenu框架实现左右侧滑菜单的功能