Xamarin.Forms 无法点击的 ListView(移除选择波纹效果)

Xamarin.Forms untappable ListView (remove selection ripple effect)

我有一个带有显示文章的自定义 ViewCell 的 ListView。但是,当您 select 一个项目时,它会显示 material 设计 ripple/selection 效果。

Xaml:

   <ListView HasUnevenRows="True" ItemsSource="{Binding NewsArticles}" IsPullToRefreshEnabled="True">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout Padding="10">
                <Label Text="{Binding Title}" HorizontalOptions="Center" FontAttributes="Bold" />
                <Image Source="{Binding ImageUrl}" IsVisible="{Binding HasImage}" />
                <Label Text="{Binding Content}"></Label>
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>

如何消除涟漪效应?

所以经过很长很长的时间我们弄明白了,你可以用自定义渲染器来完成它。方法如下,

首先,创建一个名为no_selector.xml的文件并将其放置在Resources/layouts文件夹中(打包属性必须设置为AndroidResource)。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false" android:drawable="@android:color/transparent"/>
</selector>

之后为 ListView 组件创建自定义渲染器,

[assembly: ExportRenderer (typeof(ListView), typeof(NoRippleListViewRenderer))]
namespace Your.Own.Namespace
{
    public class NoRippleListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged (e);
            Control.SetSelector (Resource.Layout.no_selector);
        }
    }
}

如果找不到 no_selector 文件重建您的项目!

请注意,这会消除应用程序中所有 ListView 的波动。如果您只希望它针对一对夫妇,您可以更改 ExportRenderer 属性上的第一种类型(这确实需要您制作一个单独的 class 来扩展 ListView)。

https://gist.github.com/awatertrevi/d83787dbbf3de6ef0e0a344169d3c2fa

我认为您可以在没有自定义渲染器的情况下删除它。

您可以将 StackPanelBackgroundColor 设置为灰色或任何列表或页面的背景颜色。

<ListView HasUnevenRows="True" ItemsSource="{Binding NewsArticles}" IsPullToRefreshEnabled="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="10" BackgroundColor="Gray">
                    <!-- ... --> 
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

或者您可以使用 android 样式来更改列表视图样式:

AndroidManifest.xml

中设置主题
<application android:label="$safeprojectname$" android:theme="@style/myTheme"></application>

styles.xml

中创建主题
<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="myTheme" parent="@android:style/Theme.Material.Light">
        <item name="android:listViewStyle">@style/ListViewStyle.Light</item>
    </style>

    <style name="ListViewStyle.Light" parent="@android:style/Widget.ListView">
        <item name="android:listSelector">@android:color/transparent</item>
    </style>
</resources>