Skip to content

Commit

Permalink
more tabbed page hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Mar 9, 2023
1 parent 6b25eb2 commit 5d79c90
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 55 deletions.
4 changes: 2 additions & 2 deletions ShanedlerSamples/Library/Common/HostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde
builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
handlers.AddHandler(typeof(Page), typeof(WorkaroundPageHandler));
handlers.AddHandler(typeof(Page), typeof(WorkaroundPageHandler));
#endif

#if ANDROID || IOS || MACCATALYST || WINDOWS
Expand All @@ -35,7 +35,7 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde
builder.ConfigureEntryNextWorkaround();
builder.ConfigureKeyboardAutoScroll();
#if ANDROID
builder.ConfigureEntryFocusOpensKeyboard();
builder.ConfigureEntryFocusOpensKeyboard();
#endif
}

Expand Down
105 changes: 54 additions & 51 deletions ShanedlerSamples/Library/Workarounds/CustomFragmentContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,70 @@

namespace Microsoft.Maui.Controls.Platform
{
internal class CustomFragmentContainer : Fragment
{
AView? _pageContainer;
readonly IMauiContext _mauiContext;
Action<AView>? _onCreateCallback;
ViewGroup? _parent;
AdapterItemKey _adapterItemKey;
internal class CustomFragmentContainer : Fragment
{
AView? _pageContainer;
readonly IMauiContext _mauiContext;
Action<AView>? _onCreateCallback;
ViewGroup? _parent;
AdapterItemKey _adapterItemKey;

public CustomFragmentContainer(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
{
_mauiContext = mauiContext;
_adapterItemKey = adapterItemKey;
}
public CustomFragmentContainer(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
{
_mauiContext = mauiContext;
_adapterItemKey = adapterItemKey;
}

public Page Page => _adapterItemKey.Page;
public Page Page => _adapterItemKey.Page;

public static CustomFragmentContainer CreateInstance(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
{
return new CustomFragmentContainer(adapterItemKey, mauiContext) { Arguments = new Bundle() };
}
public static CustomFragmentContainer CreateInstance(AdapterItemKey adapterItemKey, IMauiContext mauiContext)
{
return new CustomFragmentContainer(adapterItemKey, mauiContext) { Arguments = new Bundle() };
}

public void SetOnCreateCallback(Action<AView> callback)
{
_onCreateCallback = callback;
}
public void SetOnCreateCallback(Action<AView> callback)
{
_onCreateCallback = callback;
}

public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
{
_parent = container ?? _parent;
public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
{
_parent = container ?? _parent;

_pageContainer = Page.ToPlatform(_mauiContext, RequireContext(), inflater, ChildFragmentManager);
_adapterItemKey.SetToStableView();
_parent = _parent ?? (_pageContainer.Parent as ViewGroup);
_onCreateCallback?.Invoke(_pageContainer);
_pageContainer = Page.ToPlatform(_mauiContext, RequireContext(), inflater, ChildFragmentManager);
_adapterItemKey.SetToStableView();
_parent = _parent ?? (_pageContainer.Parent as ViewGroup);
_onCreateCallback?.Invoke(_pageContainer);

return _pageContainer;
}
return _pageContainer;
}

public override void OnDestroy()
{
base.OnDestroy();
Page?.Handler?.DisconnectHandler();
}
internal static bool InTheThrowsOfTheAdapterHack;
public override void OnDestroy()
{
base.OnDestroy();

public override void OnResume()
{
if (_pageContainer == null)
return;
if (!InTheThrowsOfTheAdapterHack)
Page?.Handler?.DisconnectHandler();
}

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

base.OnResume();
}
}
_parent = (_pageContainer.Parent as ViewGroup) ?? _parent;
if (_pageContainer.Parent == null && _parent != null)
{
// Re-add the view to the container if Android removed it
// Because we are re-using views inside OnCreateView Android
// will remove the "previous" view from the parent but since our
// "previous" view and "current" view are the same we have to re-add it
_parent.AddView(_pageContainer);
}

base.OnResume();
}
}
}
#endif
13 changes: 11 additions & 2 deletions ShanedlerSamples/Library/Workarounds/CustomTabbedPageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ protected override void DisconnectHandler(Android.Views.View platformView)

void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{

var currentIndex = Element.Children.IndexOf(Element.CurrentPage);
_viewPager.Adapter = _previousAdapter;

try
{
CustomFragmentContainer.InTheThrowsOfTheAdapterHack = true;
_viewPager.Adapter = _previousAdapter;
}
finally
{
CustomFragmentContainer.InTheThrowsOfTheAdapterHack = false;
}

(VirtualView as BindableObject)
.Dispatcher.Dispatch(() =>
{
Expand Down

0 comments on commit 5d79c90

Please sign in to comment.