繁体   English   中英

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

[英]Xamarin.Forms untappable ListView (remove selection ripple effect)

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

连锁反应

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属性的第一个类型(这需要您创建一个扩展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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM