From c6ac9376fcef9cc47b967cad9f35e17b4e1cec92 Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Thu, 1 Oct 2020 14:16:37 -0400 Subject: [PATCH] re-select support for android tabbed page buttons (#1100) * re-select support for android tabbed page buttons * applied Sane Person Technology (tm) to index check * remove extra line --- src/Android/Android.csproj | 1 + src/Android/Renderers/CustomTabbedRenderer.cs | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/Android/Renderers/CustomTabbedRenderer.cs diff --git a/src/Android/Android.csproj b/src/Android/Android.csproj index 41867dcb9..07454774a 100644 --- a/src/Android/Android.csproj +++ b/src/Android/Android.csproj @@ -120,6 +120,7 @@ + diff --git a/src/Android/Renderers/CustomTabbedRenderer.cs b/src/Android/Renderers/CustomTabbedRenderer.cs new file mode 100644 index 000000000..74d1c0dd3 --- /dev/null +++ b/src/Android/Renderers/CustomTabbedRenderer.cs @@ -0,0 +1,60 @@ +using Android.Content; +using Android.Views; +using Bit.Droid.Renderers; +using Google.Android.Material.BottomNavigation; +using Xamarin.Forms; +using Xamarin.Forms.Platform.Android; +using Xamarin.Forms.Platform.Android.AppCompat; + +[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedRenderer))] +namespace Bit.Droid.Renderers +{ + public class CustomTabbedRenderer : TabbedPageRenderer, BottomNavigationView.IOnNavigationItemReselectedListener + { + private TabbedPage _page; + + public CustomTabbedRenderer(Context context) : base(context) { } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + if (e.NewElement != null) + { + _page = e.NewElement; + GetBottomNavigationView()?.SetOnNavigationItemReselectedListener(this); + } + else + { + _page = e.OldElement; + } + } + + private BottomNavigationView GetBottomNavigationView() + { + for (var i = 0; i < ViewGroup.ChildCount; i++) + { + var childView = ViewGroup.GetChildAt(i); + if (childView is ViewGroup viewGroup) + { + for (var j = 0; j < viewGroup.ChildCount; j++) + { + var childRelativeLayoutView = viewGroup.GetChildAt(j); + if (childRelativeLayoutView is BottomNavigationView bottomNavigationView) + { + return bottomNavigationView; + } + } + } + } + return null; + } + + async void BottomNavigationView.IOnNavigationItemReselectedListener.OnNavigationItemReselected(IMenuItem item) + { + if (_page?.CurrentPage?.Navigation != null && _page.CurrentPage.Navigation.NavigationStack.Count > 0) + { + await _page.CurrentPage.Navigation.PopToRootAsync(); + } + } + } +}