Android:带有片段的向导式活动
你好 我在这里和其他网站上读了很多文章,但似乎没有人能找到一个有效的解决方案来使用片段作为类似向导的应用程序 我试图实现一个,但目前遇到了一些问题 以下是带有步骤片段占位符的活动XML:Android:带有片段的向导式活动,android,android-activity,fragment,wizard,Android,Android Activity,Fragment,Wizard,你好 我在这里和其他网站上读了很多文章,但似乎没有人能找到一个有效的解决方案来使用片段作为类似向导的应用程序 我试图实现一个,但目前遇到了一些问题 以下是带有步骤片段占位符的活动XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.andro
<?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>
“下一步”按钮将当前片段替换为新片段。这似乎很有效
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):
碎片的第40行是它们的充气器
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_report_wizard_1, container, false);
}
如何绑定(添加/删除)片段而不获得重复的ID?或者在没有新视图初始化的情况下如何重用片段
将您的碎片放回后台,并根据需要将其回调 使用
addToBackStack
将其添加到backstack:
list = new ListFragment_List();
getFragmentManager().beginTransaction().replace(R.id.pane, listlist).addToBackStack(null).commit();
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
使用popbackbackstack
将其拉回:
list = new ListFragment_List();
getFragmentManager().beginTransaction().replace(R.id.pane, listlist).addToBackStack(null).commit();
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
请注意,此代码来自我使用v4支持库的一个应用程序,因此如果您仅为Android 3.0+开发,则对片段管理器的调用将略有不同。我使用支持库v4和Sherlock Actionbar 在进行了一些尝试后,我最终找到了异常的原因,尝试了有后台和无后台,并简化了步骤片段的布局 我在第一步fragmen中使用片段(选择微调器)(参见XML布局)
我希望,这对尝试实现向导的othesr有所帮助。我想您可能想知道,现在有一个有效的解决方案,可以完全满足您的需求。这是一个轻量级的Android库,名为。我在这里写之前尝试过,现在又尝试过。我在LogCat中也遇到了同样的异常。看看“原因”部分。。。它指向别处。发布
报告TypeSelectorSpinnerFragment
,也许我们可以找到答案。
<?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>
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();
}