【问题标题】:Adding a fragment to a dialog将片段添加到对话框
【发布时间】:2013-08-18 10:17:23
【问题描述】:

我想将片段添加到对话框(它可以是 DialogFragment 或常规对话框)。我该怎么做?

这是我的 DialogFragment:

public class MyDialogFragment extends DialogFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyDialogFragment2 dialog = new MyDialogFragment2();
        View v = inflater.inflate(R.layout.news_articles, container, false);
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, dialog).commit();
        return v;
    }

}

这里是 news_article.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是我的主要活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            MyDialogFragment dialog = new MyDialogFragment();
            dialog.show(getSupportFragmentManager(), "asdf");
        }
    });
}

但是当我尝试它时,我得到:

No view found for id 0x7f070002 for fragment MyDialogFragment2

我认为这是因为Activity的FragmentManager不是我应该添加的,但我找不到DialogFragment的那个,它在哪里?

【问题讨论】:

  • getChildFragmentManager().beginTransaction()....
  • 谢谢,但这仅适用于 API 17,不是吗?
  • 原生片段是的,因为它们是从 4.​​2 引入的。但是您始终可以选择支持兼容性包中的片段,该包使用相同的getChildFragmentManager() 方法。
  • 哦...因为旧的 v4 版本而找不到它...找到了这个:stackoverflow.com/questions/15805574/…,谢谢!

标签: android android-fragments android-dialogfragment android-nested-fragment


【解决方案1】:

答案(感谢@Luksprog)是使用 getChildFragmentManager 而不是 getActivity().getSupportFragmentManager

它对我不可用,因为我必须升级我的 support-v4 jar,如下所述:android.support.v4.app.Fragment: undefined method getChildFragmentManager()

【讨论】:

    【解决方案2】:

    对话框布局 - R.layout.view_with_plus

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.util.me.TestActivity"
        >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Details"/>
        <fragment
            android:layout_toRightOf="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/email"
            class="com.util.me.test.PlusOneFragment"
            android:layout_centerHorizontal="true"/>
    
    </RelativeLayout>
    

    如何显示对话框

    public void showDialog(View vIew){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
        View view = this.getLayoutInflater().inflate(R.layout.view_with_plus, null);
        builder.setView(view)
                .setPositiveButton("OK", null)
                .setNegativeButton("Cancel", null);
    
        AlertDialog dialog = builder.create();
        dialog.show();
    }
    

    class="com.util.me.test.PlusOneFragment"中的fragment只是Android Studio生成的PlusOneFragment。

    【讨论】:

    • 注意,当您多次显示对话框时,这可能会出现问题。静态充气fragment会将fragment添加到Dialog的ownerActivity的fragmentManager中,当你关闭dialog时,fragment还在。所以下次你充气时,会抛出异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多