0

我有一个活动,使用滑动菜单库,我尝试创建 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();

我究竟做错了什么?我只是看不出区别

4

2 回答 2

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();
于 2013-10-03T09:25:14.637 回答
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();

对于 a FragmentTransaction,您只能有一个提交。在您的代码中,您创建了一个FragmentTransaction对象并为rightSlide. 所以 t 不再可用于交易。所以你已经创建了另一个FragmentTransaction,就像我在上面的代码中所做的那样。我希望这对你有用。

于 2013-01-16T14:22:51.687 回答