Skip to content

Commit 5d79c90

Browse files
committed
more tabbed page hacks
1 parent 6b25eb2 commit 5d79c90

File tree

3 files changed

+67
-55
lines changed

3 files changed

+67
-55
lines changed

ShanedlerSamples/Library/Common/HostExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde
2020
builder.ConfigureMauiHandlers(handlers =>
2121
{
2222
#if ANDROID
23-
handlers.AddHandler(typeof(Page), typeof(WorkaroundPageHandler));
23+
handlers.AddHandler(typeof(Page), typeof(WorkaroundPageHandler));
2424
#endif
2525

2626
#if ANDROID || IOS || MACCATALYST || WINDOWS
@@ -35,7 +35,7 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde
3535
builder.ConfigureEntryNextWorkaround();
3636
builder.ConfigureKeyboardAutoScroll();
3737
#if ANDROID
38-
builder.ConfigureEntryFocusOpensKeyboard();
38+
builder.ConfigureEntryFocusOpensKeyboard();
3939
#endif
4040
}
4141

ShanedlerSamples/Library/Workarounds/CustomFragmentContainer.cs

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,70 @@
1010

1111
namespace Microsoft.Maui.Controls.Platform
1212
{
13-
internal class CustomFragmentContainer : Fragment
14-
{
15-
AView? _pageContainer;
16-
readonly IMauiContext _mauiContext;
17-
Action<AView>? _onCreateCallback;
18-
ViewGroup? _parent;
19-
AdapterItemKey _adapterItemKey;
13+
internal class CustomFragmentContainer : Fragment
14+
{
15+
AView? _pageContainer;
16+
readonly IMauiContext _mauiContext;
17+
Action<AView>? _onCreateCallback;
18+
ViewGroup? _parent;
19+
AdapterItemKey _adapterItemKey;
2020

21-
public CustomFragmentContainer(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
22-
{
23-
_mauiContext = mauiContext;
24-
_adapterItemKey = adapterItemKey;
25-
}
21+
public CustomFragmentContainer(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
22+
{
23+
_mauiContext = mauiContext;
24+
_adapterItemKey = adapterItemKey;
25+
}
2626

27-
public Page Page => _adapterItemKey.Page;
27+
public Page Page => _adapterItemKey.Page;
2828

29-
public static CustomFragmentContainer CreateInstance(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
30-
{
31-
return new CustomFragmentContainer(adapterItemKey, mauiContext) { Arguments = new Bundle() };
32-
}
29+
public static CustomFragmentContainer CreateInstance(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
30+
{
31+
return new CustomFragmentContainer(adapterItemKey, mauiContext) { Arguments = new Bundle() };
32+
}
3333

34-
public void SetOnCreateCallback(Action<AView> callback)
35-
{
36-
_onCreateCallback = callback;
37-
}
34+
public void SetOnCreateCallback(Action<AView> callback)
35+
{
36+
_onCreateCallback = callback;
37+
}
3838

39-
public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
40-
{
41-
_parent = container ?? _parent;
39+
public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
40+
{
41+
_parent = container ?? _parent;
4242

43-
_pageContainer = Page.ToPlatform(_mauiContext, RequireContext(), inflater, ChildFragmentManager);
44-
_adapterItemKey.SetToStableView();
45-
_parent = _parent ?? (_pageContainer.Parent as ViewGroup);
46-
_onCreateCallback?.Invoke(_pageContainer);
43+
_pageContainer = Page.ToPlatform(_mauiContext, RequireContext(), inflater, ChildFragmentManager);
44+
_adapterItemKey.SetToStableView();
45+
_parent = _parent ?? (_pageContainer.Parent as ViewGroup);
46+
_onCreateCallback?.Invoke(_pageContainer);
4747

48-
return _pageContainer;
49-
}
48+
return _pageContainer;
49+
}
5050

51-
public override void OnDestroy()
52-
{
53-
base.OnDestroy();
54-
Page?.Handler?.DisconnectHandler();
55-
}
51+
internal static bool InTheThrowsOfTheAdapterHack;
52+
public override void OnDestroy()
53+
{
54+
base.OnDestroy();
5655

57-
public override void OnResume()
58-
{
59-
if (_pageContainer == null)
60-
return;
56+
if (!InTheThrowsOfTheAdapterHack)
57+
Page?.Handler?.DisconnectHandler();
58+
}
6159

62-
_parent = (_pageContainer.Parent as ViewGroup) ?? _parent;
63-
if (_pageContainer.Parent == null && _parent != null)
64-
{
65-
// Re-add the view to the container if Android removed it
66-
// Because we are re-using views inside OnCreateView Android
67-
// will remove the "previous" view from the parent but since our
68-
// "previous" view and "current" view are the same we have to re-add it
69-
_parent.AddView(_pageContainer);
70-
}
60+
public override void OnResume()
61+
{
62+
if (_pageContainer == null)
63+
return;
7164

72-
base.OnResume();
73-
}
74-
}
65+
_parent = (_pageContainer.Parent as ViewGroup) ?? _parent;
66+
if (_pageContainer.Parent == null && _parent != null)
67+
{
68+
// Re-add the view to the container if Android removed it
69+
// Because we are re-using views inside OnCreateView Android
70+
// will remove the "previous" view from the parent but since our
71+
// "previous" view and "current" view are the same we have to re-add it
72+
_parent.AddView(_pageContainer);
73+
}
74+
75+
base.OnResume();
76+
}
77+
}
7578
}
7679
#endif

ShanedlerSamples/Library/Workarounds/CustomTabbedPageHandler.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,18 @@ protected override void DisconnectHandler(Android.Views.View platformView)
4444

4545
void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
4646
{
47-
4847
var currentIndex = Element.Children.IndexOf(Element.CurrentPage);
49-
_viewPager.Adapter = _previousAdapter;
48+
49+
try
50+
{
51+
CustomFragmentContainer.InTheThrowsOfTheAdapterHack = true;
52+
_viewPager.Adapter = _previousAdapter;
53+
}
54+
finally
55+
{
56+
CustomFragmentContainer.InTheThrowsOfTheAdapterHack = false;
57+
}
58+
5059
(VirtualView as BindableObject)
5160
.Dispatcher.Dispatch(() =>
5261
{

0 commit comments

Comments
 (0)