如何在 DialogFragment 中显示现有的 ListFragment
Posted
技术标签:
【中文标题】如何在 DialogFragment 中显示现有的 ListFragment【英文标题】:How to display an existing ListFragment in a DialogFragment 【发布时间】:2012-07-05 17:19:31 【问题描述】:我有以下问题:
我有一个现有的ListFragment
,但我想将其显示为对话框。
我的第一种方法是创建一个DialogFragment
,其中必须有ListFragment
,但目前似乎无法将片段放入片段中。
扩展DialogFragment
代替ListFragment
也是不可能的,因为ListFragment
方法的大量使用。
有没有简单的方法可以做到这一点?
【问题讨论】:
我也有类似的问题。我很惊讶我在任何地方都找不到答案。我认为这是一项常见的任务,例如当您想在纵向显示带有 Fragment 的全屏活动,并在横向显示与对话框相同的 Fragment 时。 我最终在框架布局中显示了覆盖主片段的现有 ListFragment。在我看来,我看起来很不错,有点像这样:1.bp.blogspot.com/-f_1A98GQ1fU/T0bzdWnYIOI/AAAAAAAAUmA/… 也许这个例子:***.com/questions/12516626/… 【参考方案1】:对我有用的是 1) 在您的 DialogFragment 的 xml 布局中调用,比如说,DialogFragmentwWithListFragment 指定 ListFragment 类 例如。 dialog_fragment_with_list_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<fragment
android:id="@+id/flContent"
android:layout_
android:layout_
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:
DialogFragmentwWithListFragment dialogFragment = DialogFragmentwWithListFragment .newInstance();
dialogFragment.setRetainInstance(true);
dialogFragment.show(getFragmentManager(), "tag");
希望,它会有所帮助。
【讨论】:
谢谢,但是如果您在创建DialogFragmentwWithListFragment
时需要setArguments()
怎么办??【参考方案2】:
我要么将ListView
放入DialogFragment
,要么尝试将ListFragment
放入Dialog
。我不确定第二个是否可能。
【讨论】:
如果我运行它两次,这会爆炸:“原因:java.lang.IllegalArgumentException:二进制 XML 文件第 6 行:重复 id 0x7f09007b、标签 null 或父 id 0xffffffff 与另一个片段” 【参考方案3】:您可以通过DialogFragment
以这种方式显示列表:(使用支持 v4 库)
public class MyListDialogFragment extends DialogFragment
onDlgListClick mCallback;
private String[] lista;//the list you want to show with the dialog
public static MyListDialogFragment newInstance(Bundle fB)
MyListDialogFragment lstFrag = new MyListDialogFragment();
Bundle args = new Bundle();
args.putStringArray("lista", fB.getStringArray("lista"));//the list
args.putString("titulo", fB.getString("titulo"));//the title of the list
lstFrag.setArguments(args);
return lstFrag;
public interface onDlgListClick
public void onLstItemSelected(String selection);
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try
mCallback = (onDlgListClick) activity;
catch (ClassCastException e)
throw new ClassCastException(activity.toString()
+ " must implement onLstItemSelected");
this.setCancelable(false);
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
lista = getArguments().getStringArray("lista");
return new AlertDialog.Builder(getActivity())
.setTitle(getArguments().getString("titulo"))
.setCancelable(false)
.setItems(lista, new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int item)
mCallback.onLstItemSelected(lista[item]);
getDialog().dismiss(); //maybe you dont need these two lines
MyListDialogFragment.this.dismiss();
).create();
在你扩展FragmentActivity
的主要活动上并实现接口MyListDialogFragment.onDlgListClick
//the interface
@Override
public void onLstItemSelected(String selection) //list dialog fragment interface
//do whatever you want
//calling the dialog
public void showFrags(int id)
Bundle fB = new Bundle();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("listdialog");
if (prev != null)
ft.remove(prev);
ft.commit();
switch(id)
case 0:
fB.putStringArray("lista", list); fB.putString("titulo",title);
MyListDialogFragment newListDlg = MyListDialogFragment.newInstance(fB);
newListDlg.show(ft, "listdialog");
break;
【讨论】:
最小实现和其他修复:***.com/questions/12516626/…【参考方案4】:当在另一个片段中添加一个片段时,文档说您应该动态地执行此操作(即,而不是将 <fragment>
标记硬编码到您的布局 XML 中。
所以这里是如何动态地做到这一点。在这种情况下,我将MyListFragment
添加到MyDialogFragment
:
MyDialogFragment.java
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class MyDialogFragment extends DialogFragment
public static final String TAG = MyDialogFragment.class.getSimpleName();
private static final String ARG_TITLE = "ARG_TITLE";
private EditText mEditText;
public MyDialogFragment()
// Empty constructor required for DialogFragment
public static MyDialogFragment newInstance(String title)
MyDialogFragment myDialogFragment = new MyDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
myDialogFragment.setArguments(args);
return myDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
Dialog dialog = super.onCreateDialog(savedInstanceState);
Bundle args = getArguments();
if (args != null)
dialog.setTitle(args.getString(ARG_TITLE));
return dialog;
public void setTitle(String title)
Dialog dialog = getDialog();
dialog.setTitle(title);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.dialog_fragment_selected_products, container, false);
//addInnerFragment();
Button okButton = (Button)view.findViewById(R.id.okButton);
okButton.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View view)
dismiss();
//dismissAllowingStateLoss();
);
return view;
@Override
public void onStart()
super.onStart();
//addInnerFragment();
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
addInnerFragment();
public void addInnerFragment()
FragmentManager childFragmentManager = getChildFragmentManager();
FragmentTransaction transaction = childFragmentManager.beginTransaction();
//transaction.add(R.id.fragmentContainer, new MyListFragment());
transaction.add(R.id.fragmentContainer, MyListFragment.newInstance(MyListFragment.MODE_SELL));
//transaction.commit();
transaction.commitAllowingStateLoss();
childFragmentManager.executePendingTransactions();
(如您所见,它还包含一些设置对话框标题的功能。)
dialog_fragment_selected_products.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyDialogFragment"
android:orientation="vertical">
<LinearLayout
android:id="@+id/fragmentContainer"
android:layout_
android:layout_
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_above="@+id/okButton" />
<Button
android:id="@+id/okButton"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:text="@string/ok" />
</RelativeLayout>
这样做的另一个好处是您可以创建内部片段的实例,以便将任何参数传递给它。
为了完整起见,这里是我在活动中用来显示 DialogFragment 的代码:
MyActivity.java
private void showCurrentItemsDialog()
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance("cpuk.org");
//myDialogFragment.setRetainInstance(true);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(myDialogFragment, MyDialogFragment.TAG);
transaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
【讨论】:
以上是关于如何在 DialogFragment 中显示现有的 ListFragment的主要内容,如果未能解决你的问题,请参考以下文章
如何在xamarin android的MainActivity上的dialogFragment中显示editText的文本?
在 DialogFragment 中为 AlertDialog 膨胀自定义视图时出现问题
关闭 DialogFragment 时出现 NullPointerException