Xamarin.Forms无法使用ListView(删除选择涟漪效果)

时间:2016-02-23 19:18:21

标签: c# android xaml listview xamarin.forms

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

Ripple effect

的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 个答案:

答案 0 :(得分:16)

因此,经过很长一段时间我们发现它,您可以使用自定义渲染器完成它。这是怎么回事,

首先,创建一个名为 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文件重建您的项目!

请注意这样可以消除应用程序中所有 ListViews的涟漪。如果您只希望它定位一对,您可以更改ExportRenderer属性的第一个类型(这需要您创建一个扩展ListView的单独类)。

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

答案 1 :(得分:2)

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

您可以将BackgroundColor的{​​{1}}设置为灰色或列表或页面的背景颜色。

StackPanel

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

AndroidManifest.xml

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

styles.xml

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