我有一个包含 Button(子)的 ScrollView(父)。 问题是在滚动时,父级获取了 TouchEvents 的所有权,并且再也不会将它们分派(dispatch)给它的子级。因此,Button 永远不会收到 UP 事件,并且永远处于按下状态。
我需要 child 在滚动后接收 UP 事件,所以:
- 让父级在滚动后再次调度 TouchEvents(比如,在 UP 上)
- 让父级只监听 Move 事件,并调度其余的(DOWN、UP 等)
顺便说一句,我正在使用 Xamarin。谢谢。
这个简单的 XAML 设置产生了问题:
<ScrollView
Orientation="Horizontal">
<Button Text="drag me horizontally"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
TextColor="White"
WidthRequest="500">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="DefaultStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Black" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
</ScrollView>
最佳答案
我通过如下修改代码解决了这个问题:
MainPage.xaml:
<ScrollView x:Name ="Scroll1"
Orientation="Horizontal" >
<Button x:Name="TestButton1" Text="drag me horizontally"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
TextColor="White"
WidthRequest="500">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="DefaultStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Black" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Released">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Black" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
</ScrollView>
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
TestButton1.Pressed += (sender, args) =>
{
VisualStateManager.GoToState(TestButton1, "Pressed");
};
TestButton1.Released += (sender, args) =>
{
VisualStateManager.GoToState(TestButton1, "Released");
};
Scroll1.Scrolled += (sender, args) =>
{
VisualStateManager.GoToState(TestButton1, "Released");
};
}
关于android - 如何在滚动后恢复 ViewGroup 对 TouchEvents 的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150382/