MenuDrawer需要和一个Activity绑定,所以我们将MenuDraw初始化在Activity上:
RightMenu = MenuDrawer.attach(this, MenuDrawer.Type.OVERLAY, Position.END);
RightMenu.setMenuView(R.layout.slidmenu);
RightMenu.setContentView(R.layout.activity_main);
RightMenu.setTouchMode(MenuDrawer.TOUCH_MODE_NONE);
RightMenu.setMenuSize(getWindowManager()
.getDefaultDisplay().getWidth() * 7 / 8);
menu = new StatisticsMenuDraw(this, RightMenu);
通过RightMenu.setTouchMode(MenuDrawer.TOUCH_MODE_NONE);
设置MenuDrawer不能通过侧滑的方式弹出,然后在需要使用菜单的fragment里注册事件监听:
class menuItemClick implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainActivity main = (MainActivity) getActivity();
switch (v.getId()) {
case R.id.bt_open_menu:
main.RightMenu.openMenu();
break;
case R.id.tx_slidmenu_ok:
main.RightMenu.closeMenu();
break;
}
}
}
这样,我们就可以通过点击,打开或者关闭侧滑菜单了。