向活动添加2个滑动菜单

时间:2013-01-16 14:18:14

标签: android android-activity menu fragment sliding

我有一个活动,使用滑动菜单库,我尝试创建2个滑动菜单。 这是我试过的代码:

 FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.RIGHT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    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.tutorial_layout);
    rightSlide = new HelpFragment();
    t.replace(R.id.slidingList2, rightSlide);
    t.commit();
    menu.setSecondaryMenu(R.layout.log_history);
    leftSlide = new LogHistory();
    t.replace(R.id.loghistorycon, leftSlide);
    t.commit();

现在我得到一个ANR错误,而Logcat说,FragmentTransaction t已经被提交了。 我查看了以下示例:github.com/jfeinstein10/SlidingMenu,它允许他做2次提交:

 setContentView(R.layout.content_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, new SampleListFragment())
    .commit();

    getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
    getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame_two, new SampleListFragment())
    .commit();

我做错了什么?我无法看到差异

2 个答案:

答案 0 :(得分:1)

将以上代码更改为

 FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.RIGHT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    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.tutorial_layout);
    rightSlide = new HelpFragment();
    t.replace(R.id.slidingList2, rightSlide);
    t.commit();
    t = this.getSupportFragmentManager().beginTransaction();
    menu.setSecondaryMenu(R.layout.log_history);
    leftSlide = new LogHistory();
    t.replace(R.id.loghistorycon, leftSlide);
    t.commit();

对于FragmentTransaction,您只能进行一次提交。在您的代码中,您创建了一个FragmentTransaction对象,并为rightSlide调用了一次提交。所以t不再适用于交易。所以你已经在上面的代码中创建了另一个FragmentTransaction。我希望这对你有用。

答案 1 :(得分:1)

@LLL 以下代码对我正常工作。我希望,它会帮助你更多...

    SlidingMenu slidingMenu  = getSlidingMenu();slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
    slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
    slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    slidingMenu.setFadeDegree(0.35f);
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
    slidingMenu.setMenu(R.layout.profile);
    slidingMenu.setSecondaryMenu(R.layout.nextactivity);
    Button csButton=(Button)findViewById(R.id.txtx);
    inside csButton onclick listener just need to call slidingMenu.showSecondaryMenu();
    and, Button csButton1=(Button)findViewById(R.id.button1);
    inside csButton1 onclick listener just need to call slidingMenu.showMenu();