如何在DialogFragment中显示现有的ListFragment

kna*_*cke 10 android dialog fragment popupwindow

我有以下问题:

我有一个现有的ListFragment,但我想将其显示为对话框.

我的第一种方法是创建一个DialogFragment必须ListFragment在其中的内容,但显然目前不可能将片段放入片段中.

由于方法的大量使用,延伸DialogFragment而不是ListFragment也是不可能的ListFragment.

是否有捷径可寻?

use*_*419 10

对我来说有用的是

1)在您的DialogFragment的xml布局中调用,比方说,DialogFragmentwWithListFragment指定ListFragment类,
例如dialog_fragment_with_list_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
             android:id="@+id/flContent"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:padding = "10dp"
             class="com.xxx.yyy.DialogFragmentwWithListFragment " />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

2)DialogFragmentwWithListFragment中膨胀dialog_fragment_with_list_fragment.xml

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);
}
Run Code Online (Sandbox Code Playgroud)

3)调用DialogFragmentwWithListFragment 作为常规DialogFragment:

 DialogFragmentwWithListFragment dialogFragment = DialogFragmentwWithListFragment  .newInstance();
 dialogFragment.setRetainInstance(true);
 dialogFragment.show(getFragmentManager(), "tag");
Run Code Online (Sandbox Code Playgroud)


希望能帮助到你.