如何摆脱 Xamarin.Forms 中的列表视图涟漪效应 [英] How to get rid of the listview ripple effect in Xamarin.Forms

查看:23
本文介绍了如何摆脱 Xamarin.Forms 中的列表视图涟漪效应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ListView 的 Xamarin.Forms 应用程序.当按下一个 listItem 时,在 Android 上会产生涟漪效应.这是抑制纹波的必要条件.

I have a Xamarin.Forms app with a ListView. When a listItem in pressed, on Android there is a ripple effect. It is a requirement to suppress that ripple.

我已经在 Android 项目中尝试了一个样式:

I've tried it with a style in the Android project:

 <style name="MainTheme" parent="MainTheme.Base">
     <item name="android:listViewStyle">@style/ListViewStyle.Light</item>
 </style>

 <style name="ListViewStyle.Light" parent="android:style/Widget.ListView">
    <item name="android:listSelector">@drawable/actionbar_background_green</item>
 </style>

运气不好 - 风格没有被接受.我尝试了自定义 ViewCell 渲染器:

No luck - the style does not get picked up. I tried a custom ViewCell renderer:

protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        base.OnCellPropertyChanged(sender, args);

        if (args.PropertyName == "IsSelected")
        {
            _selected = !_selected;

            if (_selected)
            {
                _cellCore.SetBackgroundColor(Color.Transparent);
            }
            else
            {
                _cellCore.SetBackground(_unselectedBackground);
            }
        }
    }

那也没有用.涟漪效果还在,只有当涟漪结束时,选中的项目才会选择颜色新的背景色.

That didn't work either. The ripple effect is still there and only when the ripple is over, the selected item picks up the color new background color.

问题涉及 Xamarin.Forms,而不是 Xamarin.Android 或 Java.

The question concerns Xamarin.Forms, not Xamarin.Android nor Java.

推荐答案

这是一个老问题,但我希望这个答案能帮助其他人寻找解决方案.您可以向 ViewCell 添加一个 Frame 以完全填充它,并向该 Frame 添加一个空的点击手势识别器.这样 ViewCell 就不会检测到任何点击,因此不会触发涟漪效应.

This is an old question, but I hope this answer helps someone else looking for a solution. You can add a Frame to your ViewCell that fills it entirely and add an empty tap gesture recognizer to that Frame. That way the ViewCell won't detect any taps, so the ripple effect won't be triggered.

布局示例:

<ListView ItemsSource="{Binding ItemsSource}" HasUnevenRows="True" SelectionMode="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <AbsoluteLayout>
                    <Frame AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" BackgroundColor="Transparent">
                        <Frame.GestureRecognizers>
                            <TapGestureRecognizer/>
                        </Frame.GestureRecognizers>
                    </Frame>
                    <!-- Your controls !-->
                </AbsoluteLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这篇关于如何摆脱 Xamarin.Forms 中的列表视图涟漪效应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆