Xamarin.Forms不可点击的ListView(去除选择涟漪效果)

24

我有一个使用自定义ViewCell的ListView,用于显示文章。但是当您选择一个项目时,它会显示材料设计的波纹/选择效果。

波纹效果

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>

如何去掉涟漪效果?

2个回答

18

经过漫长的时间,我们终于找到了方法,你可以通过自定义渲染器来实现它。以下是方法:

首先,创建一个名为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的波纹效果。如果你只想针对几个ListView进行操作,可以更改ExportRenderer属性上的第一个类型(这需要你创建一个扩展ListView的单独类)。

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


我们尝试按照这个方式去做,但是收到了一个NotFoundException的错误。我们多次重建和清理了解决方案。BuildAction设置为AndroidResources,我们可以在生成的APK中看到xml文件。 - Chris
太棒了先生!运行非常流畅。 - Jammer

2

我认为您可以不使用自定义渲染器来删除它。

您可以将您的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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接