Created
June 20, 2018 19:04
-
-
Save LynoDesu/64904b6d143892cf14a60a32798a36bb to your computer and use it in GitHub Desktop.
Disable ShiftMode in Xamarin.Forms Android BottomNavigationView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Android.Support.Design.Internal; | |
using Android.Support.Design.Widget; | |
namespace MyProject.App.Droid.Helpers | |
{ | |
public static class AndroidHelpers | |
{ | |
public static void SetShiftMode(this BottomNavigationView bottomNavigationView, bool enableShiftMode, bool enableItemShiftMode) | |
{ | |
try | |
{ | |
var menuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView; | |
if (menuView == null) | |
{ | |
System.Diagnostics.Debug.WriteLine("Unable to find BottomNavigationMenuView"); | |
return; | |
} | |
var shiftMode = menuView.Class.GetDeclaredField("mShiftingMode"); | |
shiftMode.Accessible = true; | |
shiftMode.SetBoolean(menuView, enableShiftMode); | |
shiftMode.Accessible = false; | |
shiftMode.Dispose(); | |
for(int i = 0; i < menuView.ChildCount; i++) | |
{ | |
var item = menuView.GetChildAt(i) as BottomNavigationItemView; | |
if (item == null) | |
continue; | |
item.SetShiftingMode(enableItemShiftMode); | |
item.SetChecked(item.ItemData.IsChecked); | |
} | |
menuView.UpdateMenuView(); | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine($"Unable to set shift mode: {ex}"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<controls:BottomNavTabPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:views="clr-namespace:MyProject.App.Views;assembly=MyProjects.App" | |
xmlns:controls="clr-namespace:MyProject.App.Controls;assembly=MyProjects.App" | |
x:Class="MyProject.App.Views.MainTabPage" | |
Title=""> | |
<views:NewsFeed></views:NewsFeed> | |
<views:Rewards></views:Rewards> | |
<views:Nominations></views:Nominations> | |
<views:Notifications></views:Notifications> | |
</controls:BottomNavTabPage> |
FYI
var shiftMode = menuView.Class.GetDeclaredField("mShiftingMode");
This property is not accessible if pro guard obfuscates it. Either edit the pro guard configuration, or just comment it out. The code just below it where each child contents in the tab has item.SetShifting(false) -> will solve the issue for most. In such cases, the swipe from edges will still allow tab switching, but not swipe inside the content. I found that optimal in my use case.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aren't you supposed to just include the namespace of the extension class ? In this case, "using MyProject.App.Droid.Helpers;" . This will let Visual Studio automatically resolve the extension method from the AndroidHelpers class. That's how extensions are to be used.