MAUI CollectionView点击高亮时如何保留安卓原生点击效果?
问题描述
我有一个基础的MAUI CollectionView:
<CollectionView x:Name="ItemsList" SelectionMode="Single"> <CollectionView.ItemTemplate> <DataTemplate> <Grid HeightRequest="80"> <Label Text="Hello world" VerticalOptions="Center"/> </Grid> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
public MainPage() { InitializeComponent(); ItemsList.ItemsSource = new List<string>() { "1", "2", "3", "4" }; }
在安卓平台上,点击列表项会显示默认的点击按压效果。但设置选中项背景色后,点击效果消失了:
<ContentPage.Resources> <Style TargetType="Grid"> <Setter Property="VisualStateManager.VisualStateGroups"> <VisualStateGroupList> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="Selected"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="LightSkyBlue" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateGroupList> </Setter> </Style> </ContentPage.Resources>
请问有其他实现方法可以同时保留点击效果和选中项背景色吗?
解决方法
问题出在直接给Grid设置VisualStateManager.VisualStateGroups时,覆盖了MAUI默认给列表项提供的触摸反馈逻辑,导致原生点击效果丢失。以下是几种可行的解决方式:
方法一:添加Pressed状态模拟原生点击效果
修改样式,在CommonStates中补充Pressed状态,设置临时背景色变化来模拟点击按压效果,同时保留选中状态的背景色:
<ContentPage.Resources> <Style TargetType="Grid"> <Setter Property="VisualStateManager.VisualStateGroups"> <VisualStateGroupList> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <!-- 模拟原生点击的按压效果 --> <VisualState x:Name="Pressed"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="#E0E0E0" /> </VisualState.Setters> <VisualState.Animation> <Animation Duration="0.1" /> </VisualState.Animation> </VisualState> <VisualState x:Name="Selected"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="LightSkyBlue" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateGroupList> </Setter> </Style> </ContentPage.Resources>
方法二:将样式应用到ItemContentView
不给Grid设置样式,转而给CollectionView的ItemContentView设置视觉状态,这样既保留原生点击反馈,又能实现选中项背景色:
<ContentPage.Resources> <Style TargetType="Microsoft.Maui.Controls.ItemContentView"> <Setter Property="VisualStateManager.VisualStateGroups"> <VisualStateGroupList> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="Selected"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="LightSkyBlue" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateGroupList> </Setter> </Style> </ContentPage.Resources>
方法三:用ContentView包裹Grid分离状态
把Grid放在ContentView内部,ContentView保留原生点击效果,Grid单独设置选中状态样式:
<CollectionView x:Name="ItemsList" SelectionMode="Single"> <CollectionView.ItemTemplate> <DataTemplate> <ContentView> <Grid HeightRequest="80"> <VisualStateManager.VisualStateGroups> <VisualStateGroupList> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="Selected"> <VisualState.Setters> <Setter Property="BackgroundColor" Value="LightSkyBlue" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateGroupList> </VisualStateManager.VisualStateGroups> <Label Text="Hello world" VerticalOptions="Center"/> </Grid> </ContentView> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
内容的提问来源于stack exchange,提问作者PEK
相关产品推荐
相关产品推荐

