创建一个SliderRenderer,然后在DispatchTouchEvent(MotionEvent e)中调用RequestDisallowInterceptTouchEvent(boolean),就可以解决这个问题,下面是我的代码:
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;
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);
}
您必须调用RequestDisallowInterceptTouchEvent(true) 方法。我的代码中的Parent.Parent.Parent.Parent.Parent.Parent.Parent 表示Xamarin.Forms.Platform.Android.PageRenderer,Slider 不能在video 中正常工作的原因是page(在我的代码中是 JustView)阻止滑块处理拖动手势。