gpt4 book ai didi

Android:像带 fragment 的 Activity 一样的向导

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:42 25 4
gpt4 key购买 nike

你好。

我在这里和其他网站上读了很多书,但似乎没有人有像应用程序一样使用 fragment 作为向导的可行解决方案。

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

这是带有 Step-Fragments 占位符的 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 中,我将第一步添加到 Activity 中:

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

但是在 PREVIOUS 按钮上,这不起作用。

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

Fragment 的第 40 行是它们的充气器

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_report_wizard_1, container, false);
}

如何绑定(bind)(添加/删除) fragment 而不获取重复 ID?或者如何在没有新 View 初始化的情况下重用 fragment ?

enter image description here

最佳答案

将你的 fragment 放在后台栈中,并在需要时调用它们。

使用addToBackStack将它添加到backstack:

list = new ListFragment_List();
getFragmentManager().beginTransaction().replace(R.id.pane, listlist).addToBackStack(null).commit();

使用 popBackStack 将其拉回:

FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();

请注意,此代码来 self 的一个使用 v4 支持库的应用程序,因此如果您仅针对 Android 3.0+ 进行开发,则对 fragment 管理器的调用会略有不同。

关于Android:像带 fragment 的 Activity 一样的向导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10915052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com