7

I have a master detail page operating on my app. The standard method for opening the master page is to either select the burger menu icon or slide from the left. One of my detail pages happens to use a carousel page. The swiping from the left can therefore either open the master page or slide the carousel to the left (rather irritating if the wrong event occurs).

In order to stop the Master page appearing when sliding from the left, I've set IsGestureEnabled to false. However this stops the Master Page from appearing at all. Despite their being haptic feedback when pressing the burger menu icon, it does nothing.

Is there a way to force the slide gesture to be ignored on a MasterDetailPage and not the tap gesture on the icon?

Here's a very simple app that has a MasterDetailPage and IsGestureEnabled set to false. The Master page will not open. https://www.dropbox.com/s/rkm5eph3vr38avm/MasterDetailPageTest.zip?dl=0

Richard Pike
  • 679
  • 1
  • 8
  • 22
  • You'd have to override the behavior in the default renderer: https://github.com/xamarin/Xamarin.Forms/blob/74cb5c4a97dcb123eb471f6b1dffa1267d0305aa/Xamarin.Forms.Platform.Android/Renderers/MasterDetailRenderer.cs#L287 (Normal) / https://github.com/xamarin/Xamarin.Forms/blob/d178a458ee1cdae63e1ffaf6f5445000f7b9cd0e/Xamarin.Forms.Platform.Android/AppCompat/MasterDetailPageRenderer.cs#L313 (AppCompat) – Jon Douglas Nov 03 '16 at 15:10
  • @JonDouglas that's what I was afraid of. You wouldn't happen to know of an example that has this sort of behavior being overridden that I could get inspiration from? – Richard Pike Nov 03 '16 at 15:21
  • Not that I'm aware of. Maybe somebody else might know. – Jon Douglas Nov 03 '16 at 15:55
  • I just noticed this the other day as well. Seems like a regression to me (I swear this used to work in an earlier version of Forms, if not I'm going crazy). I see that you've already logged an issue in Bugzilla. Now we play the waiting game. – Will Decker Nov 04 '16 at 12:40
  • For those wondering, [here is the Bugzilla report](https://bugzilla.xamarin.com/show_bug.cgi?id=46365) Richard created. – j.f. Feb 15 '17 at 20:46
  • In march 2019 the problem still exists, but it seems the bug was not moved from bugzilla to github. Could not find the issue on github. – this.myself Mar 25 '19 at 14:39
  • Here is the relevant GitHub issue: https://github.com/xamarin/Xamarin.Forms/issues/5973 – j.f. Dec 16 '20 at 18:24

2 Answers2

1

I've come up with a bit of a workaround by creating a custom renderer for MasterDetailPage. It should suit my needs for now.

public class MyMasterDetailPageRenderer : Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer
{
    public override bool OnTouchEvent(MotionEvent e)
    {
        if (IsDrawerOpen(Android.Support.V4.View.GravityCompat.Start))
            return base.OnTouchEvent(e);
        else
        {
            if (e.Action == MotionEventActions.Up || e.Action == MotionEventActions.Down)
                return base.OnTouchEvent(e);
            else
            {
                CloseDrawers();
                return true;
            }
        }
    }
}

The assembly line needs to be added outside of the namespace:

[assembly: ExportRenderer(typeof(MyMasterDetailPage), typeof(MyMasterDetailPageRenderer))]

This doesn't completely solve the issue, but the master page does not open when swiping anymore.

Richard Pike
  • 679
  • 1
  • 8
  • 22
  • I have the exact same problem, but your solution doesn't seem to work well.. i've tried it on a simulator, and if I swipe the mouse over the left border, the master will open. Anyway thank you! – Mark O' Brian Nov 10 '16 at 16:12
  • @MarkO'Brian Admittedly I've only tried it on 1 device (which was a reason I hadn't accepted my answer yet). An e-mail I've received from Xamarin indicates they can't fix this right now as it's an artifact of the API's under the hood. – Richard Pike Nov 11 '16 at 08:50
  • thank you for your answer! i've solved my problem setting MasterDetailPage.IsGestureEnabled = true in my homepage OnAppearing(), and setting MasterDetailPage.IsGestureEnabled = false in my homepage OnDisappearing(). This way, I have swipe enabled only in my homepage, and disabled in all the other pages. It's a bit of a workaround, but fortunately it works! – Mark O' Brian Nov 11 '16 at 10:30
0

Richard's code works for me with some changes

    RootView page;

    protected override void OnElementChanged( VisualElement oldElement, VisualElement newElement ) {
        base.OnElementChanged( oldElement, newElement );
        page = newElement as RootView;
    }

    public override bool OnTouchEvent( MotionEvent e ) {
        if( IsDrawerOpen( Android.Support.V4.View.GravityCompat.Start ) )
            return base.OnTouchEvent( e );
        else {
            if( (e.Action == MotionEventActions.Up || e.Action == MotionEventActions.Down || e.Action == MotionEventActions.Move)
                && (page?.SwipeEnabled ?? false)
            )
                return base.OnTouchEvent( e );
            else {
                CloseDrawers();
                return true;
            }
        }
    }
Anton Duzenko
  • 2,366
  • 1
  • 21
  • 26