Can I prevent a TapGestureRecognizer from swallowing touches?

bandorbandor USMember ✭✭

I have a custom ViewCell with an Entry in it. I'm using a TapGestureRecognizer in the cell so that I can give focus to the Entry regardless of where the user taps in the cell. In my custom ViewCells without TapGestureRecognizers, I'm able to manage the pressed effect on the cells (adjusting their background color) by using a custom renderer. Unfortunately, the tap effects aren't handled by the custom renderer if the TapGestureRecognizer processes the touch. This leaves me without any sort of pressed effect highlighting for tapping the cell. Is there some way I can prevent the Xamarin TapGestureRecognizer from swallowing touches?

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    You should not be adding a TapGestureRecognizer to the custom ViewCell which has been added tap effect. Every cell already has events that handle tapping on them and a GestureRecognizer would probably only confuse the ViewCell.

    Try to change the background color in TapGestureRecognizer method. Add a StackLayout in ViewCell, and set layout's background color when tapping.

    <ViewCell>
        <StackLayout>
            <Entry />
        </StackLayout>
    </ViewCell>
    
  • bandorbandor USMember ✭✭

    Hi Jarvan. Thanks for the response! There are two reasons I've been reluctant to manage the press effect in the Xamarin TapGestureRecognizer method. First, I want the press effect to last for the duration of the touch. I don't think there's a touch event for releasing touch in the shared code. Second, I'd like to have the native ripple effect for touch on Android devices.

    Like you said, it seems like TapGestureRecognizers conflict with the existing touch functionality of cells. That's why I was hoping there was a way to prevent the TapGestureRecognizers from swallowing the touch--just letting the TapGestureRecognizer do its work and then letting the ViewCell's built-in touch processing do its work on the same touch.

    I'll keep looking for a way to keep the native looking press effects and still redirect focus to the entry. To keep this thread focused, however, can you or anyone else please confirm that there's no way to way to prevent the TapGestureRecognizer from swallowing touch? Thanks!

  • bandorbandor USMember ✭✭

    Thanks for the confirmation Jarvan.

    On a side note, I misspoke in my last post. The TapGestureRecognizer gets called for release, not press down. It's still the same problem about not being able to capture the full duration of the touch, though.

Sign In or Register to comment.