【问题标题】:how to change fragment inside a dialogfragment如何更改对话框片段中的片段
【发布时间】:2018-02-05 17:55:49
【问题描述】:

我想用LinearLayout 制作一个空的DialogFragment,然后更改LinearLayout 中的片段。例如,第一个片段是 3 按钮(facebook、google+、电子邮件登录)的登录,当有人按下电子邮件时,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;
    }

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我找到了比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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多