在单个活动中切换片段

nha*_*man 11 android android-fragments android-activity

我想创建一个Activity显示用户可以通过的菜单.通过单击项目,将显示一个新屏幕,允许用户提供更多选项(类似向导).

我想用Fragments 来实现它,但它对我不起作用.
现在我有:

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>
Run Code Online (Sandbox Code Playgroud)

MainMenuFragmentOnClickListener:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.mainmenu, container, false);

    setupButton(view);
    return view;
}

/* Button setup code omitted */

@Override
public void onClick(View v) {
    SherlockFragment secondRunMenuFragment = (SherlockFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.secondmenufragment);
    FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction();

    transaction.replace(android.R.id.content, secondMenuFragment); //also crashes with R.id.main_fragmentcontainer
    transaction.addToBackStack(null);
    transaction.commit();
}
Run Code Online (Sandbox Code Playgroud)

现在当我按下按钮时,应用程序崩溃了这个logcat:

06-27 01:45:26.309:E/AndroidRuntime(8747):java.lang.IllegalStateException:无法更改片段的容器ID SecondMenuFragment {405e2a70#1 id = 0x7f060029}:是2131099689 now 2131099687
06-27 01:45 :26.309:E/AndroidRuntime(8747):在android.support.v4.app.BackStackRecord.doAddOp(未知来源)
06-27 01:45:26.309:E/AndroidRuntime(8747):在android.support.v4.app .BackStackRecord.replace(未知来源)
06-27 01:45:26.309:E/AndroidRuntime(8747):在android.support.v4.app.BackStackRecord.replace(未知来源)
06-27 01:45:26.309:E/AndroidRuntime(8747):在com.myapp.MainMenuFragment $ MyButtonOnClickListener.onClick(MainMenuFragment.java:52)

我究竟做错了什么?

小智 14

我创建了这个主要布局:

<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>
Run Code Online (Sandbox Code Playgroud)

我在FrameActivity中补充:

@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();
  ...
}
Run Code Online (Sandbox Code Playgroud)

我在onClick方法上使用相同的代码,使用Fragment(Dashboard for Events):

@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();
  ...
}
Run Code Online (Sandbox Code Playgroud)


Com*_*are 13

就个人而言,我不会有任何<fragment>元素.

步骤1:使用<FrameLayout>向导的可变部分以及各种按钮填充活动布局.

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

步骤3:在"下一步"单击时,运行a FragmentTransaction以使用FrameLayout向导的下一页替换内容.

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

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

总有一天,如果没有人打败我,我会尝试创建一个逐页向导的示例,或者可能是一个可重用的组件.