Xamarin Forms Slider in ListView in TabbedPage not sliding correctly

Viewed 1063

I have a DataTemplate that puts a slider inside a ListView within a TabbedPage. On Android the slider does not move correctly and sliding left to right activates the TabbedPage sliding feature. If the slider is outside the ListView it works correctly. How can I prevent parent controls of the slider from handling the drag gesture?

The method ViewParent.requestDisallowInterceptTouchEvent(boolean) may be of help but which renderer etc would I do this?

Video of issue with the slider

2 Answers

I base my answer on the solution provided by York Shen. I didn't like the way parent nesting was hard-coded, so I just added a function to help find the renderer on which we want to call RequestDisallowInterceptTouchEvent. My Slider was nested in another view, which means I needed a different level parent. So here you go:

public override bool DispatchTouchEvent(MotionEvent e)
{
    switch (e.Action)
    {
        case MotionEventActions.Down:
            (_navigationPage ??= GetNavigationPage(Parent)).RequestDisallowInterceptTouchEvent(true);
            break;
        case MotionEventActions.Move:
            //This is the core of the problem!!!
            _navigationPage.RequestDisallowInterceptTouchEvent(true);
            break;
        case MotionEventActions.Up:
            break;
        default:
            break;
    }
    return base.DispatchTouchEvent(e);
}

private IViewParent _navigationPage;

private IViewParent GetNavigationPage(IViewParent parent)
{
    if (parent is NavigationPageRenderer)
        return parent;
    return GetNavigationPage(parent.Parent);
}
Related