コード例 #1
0
        public static void SetContentView([NotNull] this ViewGroup frameLayout, object content,
                                          FragmentTransaction transaction = null, Action <ViewGroup, Fragment, FragmentTransaction> updateAction = null)
        {
            Should.NotBeNull(frameLayout, "frameLayout");
            if (content == null)
            {
                bool hasFragment     = false;
                var  fragmentManager = frameLayout.GetFragmentManager();
                if (fragmentManager != null)
                {
                    var fragment = fragmentManager.FindFragmentById(frameLayout.Id);
                    hasFragment = fragment != null;
                    if (hasFragment && !fragmentManager.IsDestroyed)
                    {
                        fragmentManager.BeginTransaction().Remove(fragment).CommitAllowingStateLoss();
                        fragmentManager.ExecutePendingTransactions();
                    }
                }
                if (!hasFragment)
                {
                    frameLayout.RemoveAllViews();
                }
                return;
            }

            var view = content as View;

            if (view == null)
            {
                var fragment = (Fragment)content;
                ValidateViewIdFragment(frameLayout, fragment);
                var             addToBackStack = PlatformDataBindingModule.AddToBackStackMember.GetValue(frameLayout, null);
                FragmentManager manager        = null;
                if (transaction == null)
                {
                    manager = frameLayout.GetFragmentManager();
                    if (manager == null)
                    {
                        return;
                    }
                    transaction = manager.BeginTransaction();
                }
                if (addToBackStack && fragment.Arguments != null)
                {
                    addToBackStack = !fragment.Arguments.GetBoolean(AddedToBackStackKey);
                }

                if (updateAction == null)
                {
                    if (fragment.IsDetached)
                    {
                        transaction.Attach(fragment);
                    }
                    else
                    {
                        if (addToBackStack)
                        {
                            if (fragment.Arguments == null)
                            {
                                fragment.Arguments = new Bundle();
                            }
                            fragment.Arguments.PutBoolean(AddedToBackStackKey, true);
                        }
                        transaction.Replace(frameLayout.Id, fragment);
                    }
                }
                else
                {
                    updateAction(frameLayout, fragment, transaction);
                }
                if (addToBackStack)
                {
                    transaction.AddToBackStack(null);
                }


                if (manager != null)
                {
                    transaction.Commit();
                    manager.ExecutePendingTransactions();
                }
            }
            else
            {
                if (frameLayout.ChildCount == 1 && frameLayout.GetChildAt(0) == view)
                {
                    return;
                }
                frameLayout.RemoveAllViews();
                frameLayout.AddView(view);
            }
        }