6

我有一个自定义DialogFragment,其中包含一个布局、一些 UI 元素和一个 Fragment holder 布局。我需要做的是将内容片段膨胀到支架布局中并在其中提供导航。单击添加的片段内的按钮时,视图将导航到另一个视图。该片段将被同一持有者中的另一个片段替换,contentFragment1即将显示一些数据,并在单击预览按钮时将替换contentFragment1contentFragment2。我在某处读到,您无法将硬编码的片段替换为xml另一个片段。所以我试图添加contentFragment1到对话框片段的viewholderfrom the中。onActivityCreated()但是我收到一个错误,即R.id.fragmentHolder找不到指向的资源。可能的原因是什么?

这是我的代码DialogFragment

public class MyDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog customDialog = new Dialog(getActivity());
    customDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    customDialog.setContentView(R.layout.reports_dialog);
    return customDialog;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    android.app.FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.myFragmentHolder, new ReportsListFragment());
    fragmentTransaction.commit();
    super.onActivityCreated(savedInstanceState);
}

在此处输入图像描述

<?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="400dp"
    android:background="@android:color/transparent" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="This is my header text view for the Dialog"
        android:textSize="18sp" />

<RelativeLayout
    android:id="@+id/myFragmentHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/headerlayout" >

</RelativeLayout>

4

5 回答 5

5

尝试了很多之后,我得出了一个onCreateDialog()没有视图的结论,它只是在调用上设置了一个视图setView()

这就是为什么在对话框片段的布局中添加动态(框架布局标签)或静态片段(片段标签)不会给出父视图重复 id错误。

为了实现上述目的,应该使用onCreateView可以动态膨胀的框架布局标签。然后将标题和警报按钮添加到布局中。

于 2015-06-17T11:23:04.627 回答
4

R.id.myFragmentHolder膨胀到对话框的布局,并getFragmentManager()返回活动的管理器,所以它找不到视图。

使用 API 级别 17 中的嵌套片段,您可以使用getChildFragmentManager().

于 2014-08-09T01:03:25.570 回答
1

只要确保reports_dialog 布局包含一个id myFragmentHolder 像这样的布局

<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
于 2014-01-21T12:39:01.937 回答
1

仅供参考,正如 Brandon 提到的正确答案是使用getChildFragmentManager(),请记住,android 也会恢复片段的状态。

添加第一个片段的正确代码是:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // if the saved instance isn't null, the fragment state will be restored by android
    if (savedInstanceState == null) {
        getChildFragmentManager().beginTransaction().add(R.id.myFragmentHolder, new ReportsListFragment()).commit();
    }
}

添加视图后。如果只能同时显示一个片段,则稍后使用替换。

我还建议调用transaction.addToBackStack(null); 如果应支持 Android 后退按钮。

于 2017-01-10T15:02:20.173 回答
0

布局中片段持有者的 id 是fragmentHolder并且您myFragmentHolder在代码中使用尝试将其替换为:

 fragmentTransaction.add(R.id.fragmentHolder, new ReportsListFragment());
于 2014-01-21T17:44:36.213 回答