如何在DialogFragment中显示现有的ListFragment

时间:2012-07-05 10:04:00

标签: android dialog fragment popupwindow

我有以下问题:

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

我的第一个方法是创建一个DialogFragment,其内部必须ListFragment,但显然目前无法将片段放入片段中。

由于大量使用DialogFragment方法,因此无法扩展ListFragment而不是ListFragment

有一种简单的方法吗?

4 个答案:

答案 0 :(得分:10)

对我有用的是

在您调用的DialogFragment的xml布局中, 1),比方说, 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>
DialogFragmentwWithListFragment > 对话框
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);
}

3)以常规DialogFragment方式调用 DialogFragmentwWithListFragment

 DialogFragmentwWithListFragment dialogFragment = DialogFragmentwWithListFragment  .newInstance();
 dialogFragment.setRetainInstance(true);
 dialogFragment.show(getFragmentManager(), "tag");


希望,它有所帮助。

答案 1 :(得分:3)

我要将ListView放在DialogFragment内,或者尝试将ListFragment放在Dialog内。我不确定第二个是否可行。

答案 2 :(得分: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;
        }
}

答案 3 :(得分:1)

在另一个片段中添加片段时,文档说您应该动态地执行它(即,而不是将<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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/okButton" />

    <Button
        android:id="@+id/okButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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();

}