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?
Create a SliderRenderer
, and then call the 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;
The SecondPage 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 RequestDisallowInterceptTouchEvent(true)
method. The Parent.Parent.Parent.Parent.Parent.Parent.Parent
in my code means the Xamarin.Forms.Platform.Android.PageRenderer
, The reason that the Slider
cannt work right in the video is that the page
(in my code is JustView) prevent the slider from handling the drag gesture.