Xamarin Forms Tap Gesture Not Always Recogonized on Android

206 Views Asked by At

I have a Xamarin.Forms app with a Frame that has a TapGestureRecognizer on it like so:

                <Frame StyleClass="Card" Padding="15,10">
                    <Frame.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding DatalogCommand}" />
                    </Frame.GestureRecognizers>
                    <Label Text="Datalog" StyleClass="Text-H2, Text-Bold" />
                </Frame>

It works about 60% of the time. The rest of the time it doesn't trigger the DatalogCommand.

While debugging the app I see a difference in the raw output that is being given.

This is the output when the tap works:

[ViewRootImpl@b4f0159[MainActivity]] ViewPostIme pointer 0
[GestureDetector] obtain mCurrentMotionEventRaw. action: 3 id: 773046282
[ViewRootImpl@b4f0159[MainActivity]] ViewPostIme pointer 1

This is the output when it doesn't work:

[ViewRootImpl@b4f0159[MainActivity]] ViewPostIme pointer 0
[GestureDetector] obtain mCurrentMotionEventRaw. action: 3 id: 522632858
[GestureDetector] obtain mCurrentDownEvent. id: 522632858 caller: crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onTouchEvent:-2 crc643f46942d9dd1fff9.Platform_DefaultRenderer.onTouchEvent:51 android.view.View.dispatchTouchEvent:15155 
[GestureDetector] obtain mCurrentMotionEventRaw. action: 0 id: 522632858
[GestureDetector] obtain mCurrentDownEvent. id: 522632858 caller: crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_dispatchTouchEvent:-2 crc643f46942d9dd1fff9.Platform_DefaultRenderer.dispatchTouchEvent:59 android.view.ViewGroup.dispatchTransformedTouchEvent:3926 
[GestureDetector] obtain mCurrentDownEvent. id: 522632858 caller: crc643f46942d9dd1fff9.PageRenderer.n_onTouchEvent:-2 crc643f46942d9dd1fff9.PageRenderer.onTouchEvent:51 android.view.View.dispatchTouchEvent:15155 
[GestureDetector] obtain mCurrentMotionEventRaw. action: 2 id: 453749340
[ViewRootImpl@b4f0159[MainActivity]] ViewPostIme pointer 1
[GestureDetector] handleMessage TAP

Notice how the one that doesn't work has messages about mCurrentDownEvent and a mCurrentMotionEventRaw with an action of 2. The one that works doesn't have those.

I would appear to me that when it doesn't work it seems to be seeing movement during the tab whereas when it does work it doesn't see that movement.

I looked up the actions. 2 is move and 3 is cancel.

I also tried removing the gesture recognizer on that frame and put a transparent button over the top of the frame in the same grid row. I figured I could just not use the gesture recognizer at all and use a button.

That did not fix the issue. I can still see the same output and it works about as often as the tap gesture recognizer.

It seems weird that there is a handleMessage TAP when it doesn't work. It is like something else is handling the message, but only if some movement was detected.

The other weird thing is it only seems to NOT work when I am tapping around the the middle of the screen. If I tap at the edges of the frame on either side or other frames below this one, it works every time.

It is almost like there is some invisible control there that I can't find anywhere in my xaml.

As a test I went through all of my xaml tree and as far as I can tell I have removed all gesture recognizers that should be active and when tapping near the middle of the screen I get the handleMessage TAP, but everywhere else I don't.

Any suggestions?

1

There are 1 best solutions below

0
user856232 On

After further investigation I figured this out.

The page has an AbsoluteLayout container. There is a grid with an activity indicator in it.

It is set to the middle of the page exactly where the touch is not working.

The weird thing to me is that the IsVisible is false unless the activity indicator is needed. I assumed that would allow input to go through it.

All I had to do was set InputTransparent="True" and that fixed the issue.

When I have used other containers like a StackLayout and made something IsVisible="False", they usually collapse and wouldn't cause this issue.

In this case, I think the AbsoluteLayout makes it stay the same size even when not visible.