Xamarin Forms Slider on ListView in TabbedPage not sliding correctly

I have a DataTemplate that puts a slider inside a ListView in a TabbedPage. On Android, the slider does not move correctly, and left-right swiping activates the TabbedPage's slide function. If the slider is outside the ListView, it works correctly. How can I prevent the slider controls from controlling the drag gesture?

The ViewParent.requestDisallowInterceptTouchEvent (boolean) method might help, but what kind of render, etc. would i do it?

Slider video

+3


source to share


1 answer


Create SliderRenderer

and then call RequestDisallowInterceptTouchEvent(boolean)

in DispatchTouchEvent(MotionEvent e)

, it can solve this problem, here is my code:

MainPage

:

        var tabsXaml = new TabbedPage { Title = "Working with ListView" };
        tabsXaml.Children.Add(new BasicListXaml { Title = "Basic", Icon = "icon.png" } );
        tabsXaml.Children.Add(new JustView { Title = "JustView", Icon = "icon.png" });
        tabsXaml.Children.Add(new UnevenRowsXaml { Title = "UnevenX", Icon = "icon.png" });
        tabsXaml.Children.Add(new SliderPage { Title = "SliderPage", Icon = "icon.png" });
        MainPage = tabsXaml;

      

Second page in TabbedPage

:

<ContentPage.Content>
    <local:MyListView x:Name="listView" ItemSelected="OnItemSelected">
        <local:MyListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <local:MySlider HorizontalOptions="Fill"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </local:MyListView.ItemTemplate>
    </local:MyListView>
</ContentPage.Content>

      



SlidererRenderer

:

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

      

You must call the method RequestDisallowInterceptTouchEvent(true)

. Parent.Parent.Parent.Parent.Parent.Parent.Parent

in my code means Xamarin.Forms.Platform.Android.PageRenderer

. The reason it can't work Slider

in is because page

(in my code it's JustView) so that the slider doesn't handle the drag gesture.

enter image description here

+2


source







All Articles