在一个单一的活动片段之间切换 [英] Switching between Fragments in a single Activity

查看:92
本文介绍了在一个单一的活动片段之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个活动这显示出排序菜单,用户可以通过。通过点击项目,一新的屏幕显示,允许用户更多的选择(类似向导)。

I want to create an Activity which shows a sort of menu a user can go through. By clicking an item, a new screen is shown, allowing the user more options (wizard-like).

我想用片段 s到实现这一点,但它不是为我工作。
现在我有:

I wanted to implement this using Fragments, but it's not working for me.
Right now I have:

的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>

MainMenuFragment OnClickListener

@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();
}

现在,当我preSS的按钮,这个logcat的应用程序崩溃:

Now when I press the button, the application crashes with this logcat:

1月6号至27号:45:26.309:E / AndroidRuntime(8747):java.lang.IllegalStateException:无法更改片段SecondMenuFragment的容器ID {405e2a70#1 ID = 0x7f060029}:是2131099689现在2131099687
  1月六日至27日:45:26.309:E / AndroidRuntime(8747):在android.support.v4.app.BackStackRecord.doAddOp(来源不明)
  1月六日至27日:45:26.309:E / AndroidRuntime(8747):在android.support.v4.app.BackStackRecord.replace(来源不明)
  1月六日至27日:45:26.309:E / AndroidRuntime(8747):在android.support.v4.app.BackStackRecord.replace(来源不明)
  1月六日至27日:45:26.309:E / AndroidRuntime(8747):在com.myapp.MainMenuFragment $ MyButtonOnClickListener.onClick(MainMenuFragment.java:52)

06-27 01:45:26.309: E/AndroidRuntime(8747): java.lang.IllegalStateException: Can't change container ID of fragment SecondMenuFragment{405e2a70 #1 id=0x7f060029}: was 2131099689 now 2131099687
06-27 01:45:26.309: E/AndroidRuntime(8747): at android.support.v4.app.BackStackRecord.doAddOp(Unknown Source)
06-27 01:45:26.309: E/AndroidRuntime(8747): at android.support.v4.app.BackStackRecord.replace(Unknown Source)
06-27 01:45:26.309: E/AndroidRuntime(8747): at android.support.v4.app.BackStackRecord.replace(Unknown Source)
06-27 01:45:26.309: E/AndroidRuntime(8747): at com.myapp.MainMenuFragment$MyButtonOnClickListener.onClick(MainMenuFragment.java:52)

我是什么做错了吗?

推荐答案

就个人而言,我不会有任何&LT;片断&gt; 元素

Personally, I would not have any <fragment> elements.

步骤#1:用填充你的活动布局的&LT;的FrameLayout&GT; 的变量一块精灵,再加上你的各种按钮

Step #1: Populate your activity layout with a <FrameLayout> for the variable piece of the wizard, plus your various buttons.

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

Step #2: In onCreate() of the activity, run a FragmentTransaction to load the first wizard page into the FrameLayout.

第三步:在下一个点击,运行 FragmentTransaction 替换内容的的FrameLayout 该向导的下一个页面。

Step #3: On the "next" click, run a FragmentTransaction to replace the contents of the FrameLayout with the next page of the wizard.

第四步:添加在适当的智慧禁用按钮时,他们无法使用(例如,后面的第一个向导页)

Step #4: Add in the appropriate smarts for disabling the buttons when they are unusable (e.g., back on the first wizard page).

此外,你将要考虑BACK按钮应该如何工作结合向导屏幕上的后退按钮。如果你想他们两个相同的行为,则需要每个事务添加到后退堆栈和流行的东西掉回堆栈,当你处理后退按钮。

Also, you will want to think about how the BACK button should work in conjunction with any on-screen "back" button in the wizard. If you want them to both behave identically, you will need to add each transaction to the back stack and pop stuff off the back stack when you handle the "back" button.

有一天,如果没人打我吧,我会尽力创造一个向导通过向-的片段例子,或者是一个可重用的组件。

Someday, if nobody beats me to it, I'll try to create a wizard-by-way-of-fragments example, or perhaps a reusable component.

这篇关于在一个单一的活动片段之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆