Android 在单个活动中的片段之间切换

Android 在单个活动中的片段之间切换,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,我想创建一个活动,它显示一种用户可以浏览的菜单。通过单击一个项目,将显示一个新屏幕,允许用户使用更多选项(类似于向导) 我想用Fragments实现这个功能,但它对我不起作用。 现在我有: main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width

我想创建一个
活动
,它显示一种用户可以浏览的菜单。通过单击一个项目,将显示一个新屏幕,允许用户使用更多选项(类似于向导)

我想用
Fragment
s实现这个功能,但它对我不起作用。
现在我有:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/main_fragmentcontainer" >

    <fragment
        android:id="@+id/mainmenufragment"
        android:name="com.myapp.MainMenuFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <fragment
        android:id="@+id/secondmenufragment"
        android:name="com.myapp.SecondMenuFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>
现在,当我按下该按钮时,应用程序会因以下日志而崩溃:

06-27 01:45:26.309:E/AndroidRuntime(8747):java.lang.IllegalStateException:无法更改fragment SecondMenuFragment的容器ID{405e2a70#1 ID=0x7f060029}:原来是21301099689现在是21301099687
06-27 01:45:26.309:E/AndroidRuntime(8747):在android.support.v4.app.backbackbackrecord.doAddOp(未知来源)
06-27 01:45:26.309:E/AndroidRuntime(8747):在android.support.v4.app.backbackbackrecord.replace(未知来源)
06-27 01:45:26.309:E/AndroidRuntime(8747):在android.support.v4.app.backbackbackrecord.replace(未知来源)
06-27 01:45:26.309:E/AndroidRuntime(8747):在com.myapp.MainMenuFragment$myButtonClickListener.onClick(MainMenuFragment.java:52)


我做错了什么?

就我个人而言,我不会有任何
元素

步骤#1:使用向导变量块的
以及各种按钮填充活动布局

步骤2:在活动的
onCreate()
中,运行
FragmentTransaction
将第一个向导页面加载到
FrameLayout

步骤#3:在“下一步”单击中,运行
FragmentTransaction
,将
FrameLayout
的内容替换为向导的下一页

步骤4:添加适当的smarts,以便在按钮不可用时禁用按钮(例如,返回到第一个向导页面)

此外,您还需要考虑“后退”按钮应如何与向导中的任何屏幕“后退”按钮配合使用。如果希望它们的行为相同,则需要将每个事务添加到后堆栈中,并在处理“后退”按钮时从后堆栈中弹出内容


总有一天,如果没有人能比得上我,我会尝试通过片段示例创建一个向导,或者创建一个可重用组件。

我创建了以下主布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   tools:context="com.example.fragmentsexample.MainActivity" >

   <FrameLayout 
      android:id="@+id/contentFragment"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" />

</LinearLayout>
我用相同的代码对onClick方法进行了回复,更改了片段(事件的仪表板):


另一个选择是使用罗曼·努里克的

主要特点:

  • 分支,或向导步骤影响 后续步骤的可用性
  • 允许用户在提交之前进行审阅
  • 允许用户在向导步骤之间自由导航
  • 对必需和可选步骤的支持
  • 支持步骤类(从技术上讲,每个步骤都是Java类的一个实例,因此您可以在向导中拥有多个实例)

更多信息

如果您试图创建向导,第二个
是用于什么的?我确实看到了这些链接,但我不知道如何在我的案例中应用它们。@Commonware第一个
片段
显示了第一个菜单,其中包含按钮
1
2
3
。第二个
片段
显示第二个菜单,其中包含按钮
1.1
1.2
1.3
。计划是添加第三个
片段
,带有按钮
2.1
2.2
2.3
。还是我在处理这个问题上完全错了?我不确定我是否理解你是对的,但我不知怎么地使它起了作用。我在活动布局文件中放置了一个空的
。然后,在活动的
onCreate()
中,我调用了
FragmentTransaction.add()
,将
新的MainMenuFragment()。在
MainMenuFragment
onClick()
中,我创建了一个
新的SecondMenuFragment()
,并以同样的方式将其添加到
R.id.main\u fragmentcontainer
。这至少有效。我对第一步加上你的各种按钮感到特别困惑。你这是什么意思?我能不能只使用
线性布局
?@Niek:“这是什么意思?”——向导界面通常有“上一步”(或“上一步”)和“下一步”按钮。没有理由将它们放在片段中,因为它们在所有向导步骤中都保持不变,所以我想您应该直接将它们放在活动中。“难道我不能直接使用LinearLayout吗?”--为什么?哦,那些按钮,我以为你是指我的菜单按钮。您在步骤1中建议使用框架布局,但我想这与“下一步”和“上一步”按钮的重叠有关。谢谢你的帮助@聂克:
FrameLayout
是放置片段的“插槽”
FrameLayout
经常用于此,因为我认为它是一种非常便宜的容器处理方式。只要它的行为符合您的意愿,任何
ViewGroup
在技术上都应该可以工作。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   tools:context="com.example.fragmentsexample.MainActivity" >

   <FrameLayout 
      android:id="@+id/contentFragment"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" />

</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
  ...
  Fragment fragment = new Dashboard();
  FragmentManager fm = getSupportFragmentManager();
  FragmentTransaction transaction = fm.beginTransaction();
  transaction.replace(R.id.contentFragment, fragment);
  transaction.commit();
  ...
}
@Override
public void onClick.... {
  ...
  Fragment fragment = new Events();
  FragmentManager fm = getSupportFragmentManager();
  FragmentTransaction transaction = fm.beginTransaction();
  transaction.replace(R.id.contentFragment, fragment); //Container -> R.id.contentFragment
  transaction.commit();
  ...
}