Disable Swipe on Tabbed Page

I currently have a set of tabbed pages implemented in a shared project. On one of the tabbed pages there is a WebView containing a marketing banner that users can swipe through. This implementation works fine of iOS; however, on the Android version the tabbed pages can be swiped to navigate to the next tab. This is preventing me from being able to swipe the marketing banner. Is there a way to disable Tabbed page swiping? I've tried to implement a custom renderer but I don't know what to put in the renderer.

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
  • JoshuaPolingJoshuaPoling USMember ✭✭
    edited October 2016

    @AlessandroCaliaro this solution does not help me because I am developing this application using Xamarin Forms. I don't implement native Android code. I need a solution for Xamarin Forms. Thanks.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Yes I know, this is a Xamarin forum.

    You should write a customrenderer.
    Java code should be translated in csharp...

  • JoshuaPolingJoshuaPoling USMember ✭✭

    @AlessandroCaliaro I already found this solution as well. I tried to implement it and always get the following error when trying to set the tab to false.
    System.ArgumentException: Field _useAnimations defined on type Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer is not a field on the target object which is of type App1.Droid.TabPageRenderer.

    Parameter name: obj

    This is the custom tabbed renderer below:

    using Xamarin.Forms.Platform.Android;
    using Xamarin.Forms;
    using App1.Droid;
    using Xamarin.Forms.Platform.Android.AppCompat;
    using System.Reflection;

    [assembly: ExportRenderer(typeof(TabbedPage), typeof(TabPageRenderer))]
    namespace App1.Droid
    {
    public class TabPageRenderer : TabbedRenderer
    {

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
    
            var info = typeof(TabbedPageRenderer).GetTypeInfo();
            var fieldInfo = info.GetField("_useAnimations", BindingFlags.Instance | BindingFlags.NonPublic);
            fieldInfo.SetValue(this, false);
            base.OnElementChanged(e);
        }
    }
    

    }

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    @JoshuaPoling I haven't tried the custom renderer approach. There is a scroll mode property in TabBar.axml. You could set it to fixed, but it would disable swipes on all instances of TabbedPage.

  • NMackayNMackay GBInsider, University mod

    And UWP if anyone is interested :)

    using Foobar.Common.CustomRenderers;
    using Foobar.UWP.Renderers;
    using Xamarin.Forms.Platform.UWP;
    
    [assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(CustomTabbedPageRenderer))]
    namespace MarineOps.UWP.Renderers
    {
        public class CustomTabbedPageRenderer : TabbedPageRenderer
        {
    
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                    Control.Loaded += Control_Loaded;
    
                if (e.OldElement == null) return;
    
                // Unhook when disposing control
                if (Control != null) Control.Loaded -= Control_Loaded;
            }
    
            private void Control_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                // Disable UWP swipe gesture on tabbled page
                if (Control.ItemsPanelRoot != null)
                    Control.ItemsPanelRoot.ManipulationMode = Windows.UI.Xaml.Input.ManipulationModes.None;
            }
        }
    }
    
    
  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    @JoshuaPoling On Android, you could also control this now: https://github.com/xamarin/Xamarin.Forms/pull/409

  • mgiomimgiomi USMember

    Hi everyone.

    I have the following problem: my app has tree tabbeb page, in one of these a google map is loaded.
    How do I avoid loading the map if the table is not selected?

Sign In or Register to comment.