問題描述
我有以下問題:
我有一個現有的 ListFragment
,但我想將其顯示為對話框.
I have an exisiting ListFragment
, but I would like to display this as a dialog.
我的第一種方法是創建一個 DialogFragment
,它必須在其中 ListFragment
,但目前似乎無法將片段放入片段中.
My first approach was to create a DialogFragment
which has to ListFragment
inside of it, but appearently it is currently not possible to put fragments in fragments.
擴展 DialogFragment
而不是 ListFragment
也是不可能的,因為大量使用了 ListFragment
方法.
Extending DialogFragment
instead of ListFragment
is also not possible, because of the heavy use of ListFragment
methods.
有沒有簡單的方法可以做到這一點?
Is there an easy way to do this?
推薦答案
對我有用的是
1) 在 DialogFragment 的 xml 布局中調用,比如說 DialogFragmentwWithListFragment 指定 ListFragment 類
例如.dialog_fragment_with_list_fragment.xml:
What works for me is
1) in xml layout for your DialogFragment called, let's say, DialogFragmentwWithListFragment specify ListFragment class
E.g. 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>
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);
}
3) 調用 DialogFragmentwWithListFragment 作為常規 DialogFragment:
3) invoke DialogFragmentwWithListFragment as regular DialogFragment:
DialogFragmentwWithListFragment dialogFragment = DialogFragmentwWithListFragment .newInstance();
dialogFragment.setRetainInstance(true);
dialogFragment.show(getFragmentManager(), "tag");
希望,它會有所幫助.
Hope, it helps.
這篇關于如何在 DialogFragment 中顯示現有的 ListFragment的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!