Android:带有片段的向导式活动

3

你好。

我在这里和其他网站上阅读了很多内容,但似乎没有人有一个可行的解决方案来使用片段作为向导样式的应用程序。

我试图实现一个,但目前遇到了一些问题。

这是带有步骤片段占位符的 Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/cmd_previous"
    android:layout_alignParentTop="true" >
  </FrameLayout>

  <Button
    android:id="@+id/cmd_previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/center_separator_buttons"
    android:text="@string/rpz_cmd_perv" />

  <View
    android:id="@+id/center_separator_buttons"
    style="@style/wd_center_aligner"
    android:layout_alignParentBottom="true" />

  <Button
    android:id="@+id/cmd_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/center_separator_buttons"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/rpz_cmd_next" />

</RelativeLayout>

在OnCreate方法中,我添加了该活动的第一步:
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_report_wizard);

  if (this.getIntent().getExtras() != null) {
    Bundle extra = this.getIntent().getExtras();
    if (extra.containsKey(ISelected.ID_KEY)) {
      _ID = extra.getLong(ISelected.ID_KEY);
    }
  }

  if (findViewById(R.id.fragment_container) != null && savedInstanceState == null) {
    _ReportTypeAndName = new ReportWizardTypeAndNameFragment();
    _ReportTypeAndName.setArguments(getIntent().getExtras());
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, _ReportTypeAndName).commit();
  }
}

NEXT按钮会用新的Fragment替换当前的Fragment。这似乎工作正常。

private void runNext(View view) {
  switch (_CurrentPage) {
    case ReportTypeAndName:
      _ReportSelect = new ReportWizardSelectFragment();
      _ReportSelect.setArguments(getIntent().getExtras());
      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, _ReportSelect).addToBackStack(null).commit();

      _CurrentPage = ReportWizardPage.ReportSelect;
      break;

    case ReportSelect:
      _CurrentPage = ReportWizardPage.ReportPreview;
      break;

    case ReportPreview:
      _CurrentPage = ReportWizardPage.None;
      break;

    default:
      break;
  }
}

但是在“上一页”按钮上,这个功能不起作用。
private void runPrevious(View view) {
  switch (_CurrentPage) {
    case ReportTypeAndName:
      // Umstellung des Typs
      _CurrentPage = ReportWizardPage.None;
      this.finish();
      break;

    case ReportSelect:
      _ReportTypeAndName = new ReportWizardTypeAndNameFragment();
      _ReportTypeAndName.setArguments(getIntent().getExtras());
      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, _ReportTypeAndName).commit();

      _CurrentPage = ReportWizardPage.ReportTypeAndName;
      break;

    case ReportPreview:
      _CurrentPage = ReportWizardPage.ReportSelect;
      break;

    default:
      break;
  }
}

我收到了以下错误(LogCat):
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #13: Error inflating class fragment
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  at de.webducer.android.worktime.beta.ui.fragment.ReportWizardTypeAndNameFragment.onCreateView(ReportWizardTypeAndNameFragment.java:40)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
  at android.os.Handler.handleCallback(Handler.java:605)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4424)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f05008b, tag null, or parent id 0x0 with another fragment for de.webducer.android.worktime.beta.ui.fragment.ReportTypeSelectorSpinnerFragment
  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:275)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
  ... 18 more

第40行代码是Fragment的布局填充器。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  return inflater.inflate(R.layout.frag_report_wizard_1, container, false);
}

如何在不重复使用ID的情况下绑定(添加/删除)片段?或者如何在不进行新视图初始化的情况下重用片段?

看一下“caused by”的部分...它指向了其他地方。发布ReportTypeSelectorSpinnerFragment,也许我们可以找出问题所在。 - Barak
3个回答

2

我想告诉你,现在有一个可行的解决方案与你所寻找的完全相符。它是一个轻量级的Android库,名为WizarDroid


2
把你的片段放在后堆栈中,需要时调用它们。
使用addToBackStack将其添加到后堆栈中:
list = new ListFragment_List();
getFragmentManager().beginTransaction().replace(R.id.pane, listlist).addToBackStack(null).commit();

使用popBackStack将其拉回:
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();

请注意,这段代码来自我使用的v4支持库的应用程序,因此如果您仅为Android 3.0+开发,则对片段管理器的调用将略有不同。

在写这篇文章之前,我已经尝试过一次,现在又尝试了一次。在LogCat中,我得到了相同的异常。 - WebDucer

2

我使用v4支持库和Sherlock ActionBar。

在尝试了一些有或没有BackStack以及简化步骤片段布局的情况后,我终于找到了异常的原因。

我在第一个步骤片段中使用了一个片段(选择下拉菜单),请参见XML布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <TextView
    android:id="@+id/lbl_report_type"
    style="@style/wd_field_label"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/rpz_lbl_report_type" />

  <fragment
    android:id="@+id/frag_report_type_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/lbl_report_type"
    class="de.webducer.android.ReportTypeSelectorSpinnerFragment" />

  <TextView
    android:id="@+id/lbl_report_name"
    style="@style/wd_field_label"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/frag_report_type_spinner"
    android:text="@string/rpz_lbl_report_name" />

  <EditText
    android:id="@+id/report_name"
    style="@style/wd_text_edit_pref"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/lbl_report_name"
    android:hint="@string/rpz_hint_report_name" />

  <TextView
    android:id="@+id/lbl_report_comment"
    style="@style/wd_field_label"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/report_name"
    android:text="@string/rpz_lbl_report_comment" />

  <EditText
    android:id="@+id/report_comment"
    style="@style/wd_text_multi_edit_pref"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/lbl_report_comment"
    android:hint="@string/rpz_hint_report_comment" />

  <TextView
    android:id="@+id/lbl_active"
    style="@style/wd_field_label"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/report_comment"
    android:text="@string/lbl_active" />

  <CheckBox
    android:id="@+id/report_type_active"
    style="@style/wd_text_edit_pref"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/lbl_active"
    android:checked="true"
    android:text="@string/rpz_hint_report_type_active" />

</RelativeLayout>

这个内部片段由于外部片段在onCreateView中尝试填充内部片段而导致异常。而且这个片段已经在给定ID的FragmentManager中。

我的解决方案是在替换为新片段之前或从后堆栈调用之前销毁内部片段。

private void runPrevious(View view) {
  switch (_CurrentPage) {
    case ReportTypeAndName:
      // Umstellung des Typs
      _CurrentPage = ReportWizardPage.None;
      this.finish();
      break;

    case ReportSelect:
      if (getSupportFragmentManager().findFragmentById(R.id.frag_report_type_spinner) != null) {
        getSupportFragmentManager().beginTransaction()
          .remove(getSupportFragmentManager().findFragmentById(R.id.frag_report_type_spinner)).commit();
      }
      getSupportFragmentManager().popBackStack();
      // Umstellung des Typs
      _CurrentPage = ReportWizardPage.ReportTypeAndName;
      break;

    case ReportPreview:
      _CurrentPage = ReportWizardPage.ReportSelect;
      break;

    default:
      break;
  }
  setButtonState();
}

我希望这可以帮助那些尝试实现向导的人,特别是在IT技术领域。请注意,本文不提供解释,但会保留HTML标记。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接