Android开发 侧边滑动菜单栏SlidingMenu结合Fragment

SlidingMenu是一个开源项目,https://github.com/jfeinstein10/SlidingMenu。功能是创建侧边滑动菜单栏,效果类似人人Android客户端,可点击按钮或是在屏幕上划动来展开,收缩侧边的菜单。下面是Demo效果图:
device-2013-06-25-143031
简单介绍一下:整个demo由三个Fragment,一个Activity组成,菜单一个Fragment,包含两个按钮,在内容区域切换显示FragmentA和FragmentB.
项目用了AndroidAnnotations框架。
MainActivity:

package com.pocketdigi.slidingmenudemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.pocketdigi.fragment.FragmentA_;
import com.pocketdigi.fragment.FragmentB_;
import com.pocketdigi.fragment.MenuFragment_;


@EActivity(R.layout.layout_content)
public class MainActivity extends FragmentActivity {
	Fragment fragmentA, fragmentB, menuFragment;
	SlidingMenu menu;

	@AfterViews
	public void afterViews() {
		//添加左侧菜单
		menuFragment = new MenuFragment_();
		menu = new SlidingMenu(this);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		//设置拉出菜单后,上层内容留下的宽度,即这个宽度+菜单宽度=屏幕宽度
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		//设置隐藏或显示菜单时,菜单渐变值,0,不变,1黑色,值为0-1
		menu.setFadeDegree(0f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setMenu(R.layout.layout_menu);
		
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.layout_menu, menuFragment).commit();
		
		//显示FragmentA
		showFragmentA();
	}

	/**
	 * 捕捉返回键,如果当前显示菜单,刚隐藏
	 */
	@Override
	public void onBackPressed() {
		if (menu.isMenuShowing()) {
			menu.showContent();
		} else {
			super.onBackPressed();
		}
	}
	
	public void showFragmentA()
	{
		if(fragmentA==null)
		{
			fragmentA = new FragmentA_();
		}
		if(!fragmentA.isVisible())
		{
			getSupportFragmentManager().beginTransaction()
			.replace(R.id.layout_content, fragmentA).commit();
		}
		menu.showContent(true);
	}
	
	public void showFragmentB()
	{
		if(fragmentB==null)
		{
			fragmentB = new FragmentB_();
		}
		if(!fragmentB.isVisible())
		{
			getSupportFragmentManager().beginTransaction()
			.replace(R.id.layout_content, fragmentB).commit();
		}
		menu.showContent(true);
	}


}

MenuFragment.java:

package com.pocketdigi.fragment;

import android.support.v4.app.Fragment;
import android.widget.Button;

import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.ViewById;
import com.pocketdigi.slidingmenudemo.MainActivity;
import com.pocketdigi.slidingmenudemo.R;

@EFragment(R.layout.fragment_menu)
public class MenuFragment extends Fragment {
	@ViewById
	Button btn_fragmentA,btn_fragmentB;
	MainActivity mainActivity;
	
	@AfterViews
	public void afterViews()
	{
		mainActivity=(MainActivity)getActivity();
	}
	
	@Click
	public void btn_fragmentA()
	{
		mainActivity.showFragmentA();
	}
	@Click
	public void btn_fragmentB()
	{
		mainActivity.showFragmentB();
	}
}

源码下载:
[download id=”40″]

© 2013, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

发表评论

电子邮件地址不会被公开。 必填项已用*标注