不少APP都有側滑菜單的功能,部分APP左右都是側滑菜單~SlidingMenu 這個開源項目能夠很好幫助咱們實現側滑功能,將側滑菜單引入項目的方式不少中,先經過例子介紹各類引入方式,而後給你們展現個實例:主佈局 ViewPager,左右各一個側滑菜單的用法,差很少已經能知足大部分應用的需求了。關於經常使用屬性,在文章末尾介紹。html
一、在Activity中經過SlidingMenu構造方法,直接設置側滑菜單
- 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);
-
-
- 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.leftmenu);
-
- }
-
- }
效果圖:java
是否是特別簡單~幾行代碼搞定~哈~android
二、經過把Activity繼承SlidingActivity
a、繼承SlidingActivity微信
b、而後在onCreate中setBehindContentView(R.layout.leftmenu); 設置側滑菜單的佈局app
c、經過getSlidingMenu()獲得SlidingMenu對象,而後設置樣式ide
- 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);
-
- SlidingMenu menu = getSlidingMenu();
- 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);
-
-
-
-
- }
-
- }
效果圖和第一種方式同樣~是否是也很簡單~~佈局
三、將SlidingMenu看成普通控件
能夠把SlidingMenu做爲普通的view,而後在佈局中聲明,喪心病狂的玩~下面看個例子:this
- <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>
咱們SlidingMenu做爲普通View在佈局文件中設置了佈局,而且設置了viewAbove的值爲另外一個佈局。spa
下面看pic佈局:.net
- <?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" />
就是一張妹子圖片。
最後看主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);
-
-
- mLeftMenu.setMode(SlidingMenu.LEFT);
-
- 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();
- }
- });
-
-
-
-
- }
-
- }
效果圖:
經過SlidingMenu給圖片設置一個滑動展現介紹,你也能夠滑動顯示任何東西,下載、分享按鈕什麼的。圖片不少的時候不知道效率咋樣,能夠這麼玩,可是不建議哈~
四、SlidingMenu設置左右側滑菜單例子
上面介紹的3個方法,SlidingMenu的佈局中控件的事件都須要寫在Activity中,這樣代碼比較臃腫,通常會使用Fragment做爲側滑菜單的佈局容器。
核心代碼:
- 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);
先給側滑菜單經過 setBehindContentView(R.layout.left_menu_frame); 設置一個佈局,此佈局中只有一個FrameLayout,而後使用FragmentManager將Fragment替換掉此Fragment,這樣這個 Fragment就做爲咱們側滑菜單的佈局了,咱們的事件處理代碼也能夠寫在Fragement中,而不是Activity中。
下面看具體例子:
首先分別是左右兩邊的兩個Fragment:
MenuLeftFragment
- package com.zhy.zhy_slidemenu_demo04;
-
- import java.util.Arrays;
- import java.util.List;
-
- 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;
-
- 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);
- }
- }
左邊的側滑佈局就是一個ListView,代碼比較簡單~
MenuRightFragment
- package com.zhy.zhy_slidemenu_demo04;
-
- 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 ;
- }
- }
右邊的側滑菜單的Fragment,加載了一個佈局,沒有作任何事件處理~
最後是MainActivity
- package com.zhy.zhy_slidemenu_demo04;
-
- import java.util.ArrayList;
- import java.util.List;
-
- 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;
-
- 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);
-
- initRightMenu();
-
- 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);
-
- 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.setFadeDegree(0.35f);
-
- 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();
- }
- }
簡單說明一下,MainActivity繼承的是SlidingFragmentActivity ,在Activity中FragmentPagerAdapter和viewPager做爲主佈局,而後分別初始化SlidingMenu的兩邊的菜單。
效果圖:
哈哈,微信又躺槍了~~這個例子應該能夠知足通常APP的需求了。
五、SlidingMenu的一些經常使用屬性
//設置側滑菜單的位置,可選值LEFT , RIGHT , LEFT_RIGHT (兩邊都有菜單時設置)
menu.setMode(SlidingMenu.LEFT_RIGHT);
// 設置觸摸屏幕的模式,可選只MARGIN , CONTENT
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
//根據dimension資源文件的ID來設置陰影的寬度
menu.setShadowWidthRes(R.dimen.shadow_width);
//根據資源文件ID來設置滑動菜單的陰影效果
menu.setShadowDrawable(R.drawable.shadow);
// 這兩個都是設置滑動菜單視圖的寬度,二選一
//設置SlidingMenu離屏幕的偏移量
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//設置寬度
menu.setBehindWidth()
// 設置漸入漸出效果的值
menu.setFadeDegree(0.35f);
//設置SlidingMenu與下方視圖的移動的速度比,當爲1時同時移動,取值0-1
menu.setBehindScrollScale(1.0f);
//設置二級菜單的陰影效果
menu.setSecondaryShadowDrawable(R.drawable.shadow);
//設置右邊(二級)側滑菜單
menu.setSecondaryMenu(R.layout.right_menu_frame);
//爲側滑菜單設置佈局
menu.setMenu(R.layout.leftmenu);
//把滑動菜單添加進全部的Activity中,可選值SLIDING_CONTENT , SLIDING_WINDOW
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
還有幾個監聽事件:
- public void setOnOpenListener(OnOpenListener listener) {
- mOpenListener = listener;
- }
-
-
- public void setOnCloseListener(OnCloseListener listener) {
-
- mCloseListener = listener;
- }
-
-
- public void setOnOpenedListener(OnOpenedListener listener) {
- mViewAbove.setOnOpenedListener(listener);
- }
-
-
- public void setOnClosedListener(OnClosedListener listener) {
- mViewAbove.setOnClosedListener(listener);
- }
- 轉載自:http://blog.csdn.net/lmj623565791/article/details/36677279