Xamarin.Forms 不可点击的 ListView(去除选择涟漪效应) [英] Xamarin.Forms untappable ListView (remove selection ripple effect)

查看:23
本文介绍了Xamarin.Forms 不可点击的 ListView(去除选择涟漪效应)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Xaml:

 <ListView.ItemTemplate><数据模板><ViewCell><StackLayout Padding="10"><Label Text="{Binding Title}" Horizo​​ntalOptions="Center" FontAttributes="Bold"/><Image Source="{Binding ImageUrl}" IsVisible="{Binding HasImage}"/><Label Text="{绑定内容}"></Label></StackLayout></ViewCell></数据模板></ListView.ItemTemplate></ListView>

如何消除涟漪效应?

解决方案

所以经过很长很长的时间我们想通了,您可以使用自定义渲染器来完成它.这是方法,

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

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

[程序集:ExportRenderer (typeof(ListView), typeof(NoRippleListViewRenderer))]命名空间 Your.Own.Namespace{公共类 NoRippleListViewRenderer : ListViewRenderer{protected override void OnElementChanged (ElementChangedEventArgs e){base.OnElementChanged (e);Control.SetSelector (Resource.Layout.no_selector);}}}

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

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

https://gist.github.com/awatertrevi/d83787dbbf3de6ef0e0a342p>

I have a ListView with a custom ViewCell that displays articles. However when you select a item, it shows the material design ripple/selection 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>

How do I remove the ripple effect?

解决方案

So after a long, long time we figured it out, you can accomplish it with a custom renderer. Here is how,

First, create a file called no_selector.xml and place it in the Resources/layouts folder (the packaging properties must be set to 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>

After that create a custom renderer for the ListView component,

[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);
        }
    }
}

If the no_selector file can't be found rebuild your project!

Be aware of the fact that this removes the ripple for all the ListViews in your application. If you only want it to target a couple you can change the first type on the ExportRenderer attribute (this does require you to make a separate class that extends ListView).

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

这篇关于Xamarin.Forms 不可点击的 ListView(去除选择涟漪效应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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