如何在dialogfragment中更改片段

时间:2014-08-24 19:35:25

标签: android android-fragments

我想使用DialogFragment制作一个空的LinearLayout,然后更改LinearLayout内的片段。例如,登录时第一个片段是3个按钮(Facebook,谷歌+,电子邮件登录),当有人按下电子邮件时,2.片段的布局为EditTexts如果按下Google或Facebook则会出现另一个片段使用ProgressBar

这是我的空对话框布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/dialog_background">

    <LinearLayout
        android:id="@+id/testFragmentController"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="15dp"></LinearLayout>
</RelativeLayout>

这是第一个片段的代码(我正在使用android注释):

@EFragment(R.layout.dialog)
public class FragmentGeneralDialog extends ClickDialogFragment {

    @ViewById
    LinearLayout testFragmentController;


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        this.setStyle(R.style.Dialog_No_Border, getTheme());

        return dialog;
    }


    @AfterViews
    void afterViews() {
        loadActivity();
        GlobalData.setFragmentContainer(testFragmentController);
        activity.loadMenuFragment(FragmentSocialDialog_.class, new SlideLeftAnimation());

    }


}

loadMenuFragments(...)就是这样:

public <T extends Fragment> T loadMenuFragment(final Class<T> cls,
                                                   final IAnimation a) {
        T fragment = null;
        try {
            fragment = cls.newInstance();
        } catch (Exception ex) {
            throw new RuntimeException("Fragment " + cls.toString()
                    + " has no empty constructor");
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (a != null) {
            transaction.setCustomAnimations(a.getInId(), a.getOutId(), a.getInId(),
                    a.getOutId());
        }

        transaction.replace(R.id.testFragmentController, fragment);

        try {
            transaction.commit();
        } catch (Exception e) {
        }
        return fragment;
    }

2 个答案:

答案 0 :(得分:6)

您需要从对话框片段link获取childFragmentManager,然后从子片段中获取可以通过getParentFragment().getChildFragmentManager()

更改片段的片段

答案 1 :(得分:0)

我找到了比getParentFragment更好的方法。您可以在父片段中添加public static FragmentManager并在嵌套片段中使用它:

在父母:

public class SelectProductDialogFragment extends DialogFragment {
public static FragmentManager fragmentManager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.dialog_add_product,container,false);
    fragmentManager = getChildFragmentManager();
    return v;
}
}

在孩子:例如替换交易:

ProductsGridFragment productsGridFragment = new ProductsGridFragment();
                    FragmentTransaction transaction = SelectProductDialogFragment.fragmentManager.beginTransaction()
                            .replace(R.id.dialog_order_container,productsGridFragment)
                            .addToBackStack(null);

                    transaction.commit();