예제 #1
0
        public override IParcelable SaveState()
        {
            Bundle state = null;

            if (_savedState.Count > 0)
            {
                state = new Bundle();
                var fss = new Fragment.SavedState[_savedState.Count];
                _savedState.CopyTo(fss, 0);
                state.PutParcelableArrayList("states", fss);
            }
            for (var i = 0; i < _fragments.Count; i++)
            {
                var f = _fragments[i];
                if (f != null && f.IsAdded)
                {
                    if (state == null)
                    {
                        state = new Bundle();
                    }
                    var key = "f" + i;
                    _fm.PutFragment(state, key, f);
                }
            }
            return(state);
        }
예제 #2
0
        public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
        {
            var fragmentInfo  = FragmentsInfo[position];
            var fragmentClass = Class.FromType(fragmentInfo.FragmentType);
            var fragment      = FragmentFactory.Instantiate(
                fragmentClass.ClassLoader,
                fragmentClass.Name
                );

            if (!(fragment is IMvxFragmentView mvxFragment))
            {
                return(fragment);
            }

            if (mvxFragment.GetType().IsFragmentCacheable(_activityType) && fragmentSavedState != null)
            {
                return(fragment);
            }

            mvxFragment.ViewModel = GetViewModel(fragmentInfo);

            fragment.Arguments = GetArguments(fragmentInfo);

            return(fragment);
        }
예제 #3
0
        public override Object InstantiateItem(ViewGroup container, int position)
        {
            // If we already have this item instantiated, there is nothing
            // to do.  This can happen when we are restoring the entire pager
            // from its saved state, where the fragment manager has already
            // taken care of restoring the fragments we previously had instantiated.

            if (_fragments.Count > position)
            {
                var existingFragment = _fragments.ElementAtOrDefault(position);
                if (existingFragment != null)
                {
                    return(existingFragment);
                }
            }

            if (_curTransaction == null)
            {
                _curTransaction = _fragmentManager.BeginTransaction();
            }

            var fragmentTag = GetTag(position);

            Fragment.SavedState fss = null;
            if (_savedState.Count > position)
            {
                var savedTag = _savedFragmentTags.ElementAtOrDefault(position);
                if (string.Equals(fragmentTag, savedTag))
                {
                    fss = _savedState.ElementAtOrDefault(position);
                }
            }

            var fragment = GetItem(position, fss);

            if (fss != null)
            {
                fragment.SetInitialSavedState(fss);
            }

#if DEBUG
            Mvx.Trace("Adding item #" + position + ": f=" + fragment + " t=" + fragmentTag);
#endif

            while (_fragments.Count <= position)
            {
                _fragments.Add(null);
            }

            fragment.SetMenuVisibility(false);
            fragment.UserVisibleHint = false;
            _fragments[position]     = fragment;
            _curTransaction.Add(container.Id, fragment, fragmentTag);

            return(fragment);
        }
        public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
        {
            var fragment = base.GetItem(position, fragmentSavedState);

            // If the MvxViewPagerFragmentInfo for this position doesn't have the ViewModel, overwrite it with a new MvxViewPagerFragmentInfo that has the ViewModel we just created.
            // Not doing this means the ViewModel gets recreated every time the Fragment gets recreated!
            if (FragmentsInfo != null && FragmentsInfo.Count > position && fragment is IMvxFragmentView mvxFragment && mvxFragment.ViewModel != null)
            {
                var oldFragInfo = FragmentsInfo[position];

                if (oldFragInfo != null && oldFragInfo.ViewModel == null)
                {
                    var newFragInfo = new MvxViewPagerFragmentInfo(oldFragInfo.Title, oldFragInfo.Tag, oldFragInfo.FragmentType, mvxFragment.ViewModel);
                    FragmentsInfo[position] = newFragInfo;
                }
            }

            return(fragment);
        }
        public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
        {
            var fragmentInfo = FragmentsInfo.ElementAt(position);
            var fragment     = Fragment.Instantiate(_context, fragmentInfo.FragmentType.FragmentJavaName());

            if (!(fragment is IMvxFragmentView mvxFragment))
            {
                return(fragment);
            }

            if (mvxFragment.GetType().IsFragmentCacheable(_activityType) && fragmentSavedState != null)
            {
                return(fragment);
            }

            mvxFragment.ViewModel = GetViewModel(fragmentInfo);

            return(fragment);
        }
예제 #6
0
        public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
        {
            var fragInfo = Fragments.ElementAt(position);
            var fragment = Fragment.Instantiate(_context, FragmentJavaName(fragInfo.FragmentType));

            var mvxFragment = fragment as MvxFragment;

            if (mvxFragment == null)
            {
                return(fragment);
            }

            if (mvxFragment.GetType().IsFragmentCacheable() && fragmentSavedState != null)
            {
                return(fragment);
            }

            var viewModel = CreateViewModel(position);

            mvxFragment.ViewModel = viewModel;

            return(fragment);
        }
        public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
        {
            var fragInfo = FragmentsInfo.ElementAt(position);
            var fragment = Fragment.Instantiate(_context, FragmentJavaName(fragInfo.FragmentType));

            var mvxFragment = fragment as IMvxFragmentView;

            if (mvxFragment == null)
            {
                return(fragment);
            }

            if (mvxFragment.GetType().IsFragmentCacheable(Mvx.Resolve <IMvxAndroidCurrentTopActivity>().Activity.GetType()) && fragmentSavedState != null)
            {
                return(fragment);
            }

            var viewModel = fragInfo.ViewModel ?? CreateViewModel(position);

            mvxFragment.ViewModel = viewModel;

            return(fragment);
        }
예제 #8
0
 public abstract Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null);
예제 #9
0
 public override Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null)
 {
     return(_fragmentInfos[position].Fragment);
 }
예제 #10
0
        public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {
            // If we already have this item instantiated, there is nothing
            // to do.  This can happen when we are restoring the entire pager
            // from its saved state, where the fragment manager has already
            // taken care of restoring the fragments we previously had instantiated.
            if (_fragments.Count > position)
            {
                var existingFragment = _fragments.ElementAtOrDefault(position);
                if (existingFragment != null)
                {
                    return(existingFragment);
                }
            }

            if (_curTransaction == null)
            {
                _curTransaction = _fragmentManager.BeginTransaction();
            }

            var fragmentTag = GetTag(position);

            Fragment.SavedState fss = null;
            if (_savedState.Count > position)
            {
                var savedTag = _savedFragmentTags.ElementAtOrDefault(position);
                if (string.Equals(fragmentTag, savedTag))
                {
                    fss = _savedState.ElementAtOrDefault(position);
                }
            }

            var fragment = GetItem(position, fss);

            if (fss != null)
            {
                fragment.SetInitialSavedState(fss);
            }

            //if fragment tag is null let's set it to something meaning full;
            if (string.IsNullOrEmpty(fragmentTag))
            {
                fragmentTag = fragment.GetType().FragmentJavaName();
            }

#if DEBUG
            MvxLogHost.GetLog <MvxCachingFragmentPagerAdapter>()?.Log(LogLevel.Trace,
                                                                      "Adding item #{position}: f={fragment} t={tag}", position, fragment, fragmentTag);
#endif

            while (_fragments.Count <= position)
            {
                _fragments.Add(null);
            }

            fragment.SetMenuVisibility(false);
            fragment.UserVisibleHint = false;
            _fragments[position]     = fragment;
            _curTransaction.Add(container.Id, fragment, fragmentTag);

            return(fragment);
        }