forked from martijn00/MvvmCross-AndroidSupport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMvxFragmentPagerAdapter2.cs
228 lines (188 loc) · 7.55 KB
/
MvxFragmentPagerAdapter2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Linq;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Views;
using MvvmCross.Platform;
using Java.Lang;
using Object = Java.Lang.Object;
namespace MvvmCross.Droid.Support.V4
{
//http://speakman.net.nz/blog/2014/02/20/a-bug-in-and-a-fix-for-the-way-fragmentstatepageradapter-handles-fragment-restoration/
//https://github.com/adamsp/FragmentStatePagerIssueExample/blob/master/app/src/main/java/com/example/fragmentstatepagerissueexample/app/FixedFragmentStatePagerAdapter.java
public abstract class MvxFragmentPagerAdapter2 : PagerAdapter
{
private Fragment _currentPrimaryItem;
private FragmentTransaction _curTransaction;
private readonly FragmentManager _fragmentManager;
private readonly List<Fragment> _fragments = new List<Fragment>();
private List<string> _savedFragmentTags = new List<string>();
private readonly List<Fragment.SavedState> _savedState = new List<Fragment.SavedState>();
protected MvxFragmentPagerAdapter2(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
protected MvxFragmentPagerAdapter2(FragmentManager fragmentManager)
{
_fragmentManager = fragmentManager;
}
public abstract Fragment GetItem(int position, Fragment.SavedState fragmentSavedState = null);
public override void DestroyItem(ViewGroup container, int position, Object objectValue)
{
var fragment = (Fragment)objectValue;
if (_curTransaction == null)
_curTransaction = _fragmentManager.BeginTransaction();
#if DEBUG
Mvx.Trace("Removing item #" + position + ": f=" + objectValue + " v=" + ((Fragment) objectValue).View +
" t=" + fragment.Tag);
#endif
while (_savedState.Count <= position)
{
_savedState.Add(null);
_savedFragmentTags.Add(null);
}
_savedState[position] = _fragmentManager.SaveFragmentInstanceState(fragment);
_savedFragmentTags[position] = fragment.Tag;
_fragments[position] = null;
_curTransaction.Remove(fragment);
}
public override void FinishUpdate(ViewGroup container)
{
if (_curTransaction == null)
return;
_curTransaction.CommitAllowingStateLoss();
_curTransaction = null;
_fragmentManager.ExecutePendingTransactions();
}
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 bool IsViewFromObject(View view, Object objectValue)
{
return ((Fragment)objectValue).View == view;
}
public override void RestoreState(IParcelable state, ClassLoader loader)
{
if (state == null)
return;
var bundle = (Bundle)state;
bundle.SetClassLoader(loader);
var fss = bundle.GetParcelableArray("states");
_savedState.Clear();
_fragments.Clear();
var tags = bundle.GetStringArrayList("tags");
if (tags != null)
_savedFragmentTags = tags.ToList();
else
_savedFragmentTags.Clear();
if (fss != null)
{
for (var i = 0; i < fss.Length; i++)
{
var parcelable = fss.ElementAt(i);
var savedState = parcelable.JavaCast<Fragment.SavedState>();
_savedState.Add(savedState);
}
}
var keys = bundle.KeySet();
foreach (var key in keys)
{
if (!key.StartsWith("f"))
continue;
var index = Integer.ParseInt(key.Substring(1));
var f = _fragmentManager.GetFragment(bundle, key);
if (f != null)
{
while (_fragments.Count() <= index)
_fragments.Add(null);
f.SetMenuVisibility(false);
_fragments[index] = f;
}
}
}
public override IParcelable SaveState()
{
Bundle state = null;
if (_savedState.Any())
{
state = new Bundle();
var fss = new IParcelable[_savedState.Count];
for (var i = 0; i < _savedState.Count; i++)
fss[i] = _savedState.ElementAt(i);
state.PutParcelableArray("states", fss);
state.PutStringArrayList("tags", _savedFragmentTags);
}
for (var i = 0; i < _fragments.Count; i++)
{
var f = _fragments.ElementAtOrDefault(i);
if (f == null)
continue;
if (state == null)
state = new Bundle();
var key = "f" + i;
_fragmentManager.PutFragment(state, key, f);
}
return state;
}
public override void SetPrimaryItem(ViewGroup container, int position, Object objectValue)
{
var fragment = (Fragment)objectValue;
if (fragment == _currentPrimaryItem)
return;
if (_currentPrimaryItem != null)
{
_currentPrimaryItem.SetMenuVisibility(false);
_currentPrimaryItem.UserVisibleHint = false;
}
if (fragment != null)
{
fragment.SetMenuVisibility(true);
fragment.UserVisibleHint = true;
}
_currentPrimaryItem = fragment;
}
public override void StartUpdate(View container)
{
}
protected virtual string GetTag(int position)
{
return null;
}
}
}