【问题标题】:Xamarin.Forms untappable ListView (remove selection ripple effect)Xamarin.Forms 不可点击的 ListView(移除选择涟漪效应)
【发布时间】:2016-06-05 19:17:27
【问题描述】:

我有一个带有自定义 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>

如何消除涟漪效应?

【问题讨论】:

    标签: c# android xaml listview xamarin.forms


    【解决方案1】:

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

    首先,创建一个名为 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

    【讨论】:

    • 我们尝试完全按照这种方式进行操作,但收到了 NotFoundException。我们多次重建和清理解决方案。 BuildAction 设置为 AndroidResources,我们可以在生成的 APK 中看到 xml-File
    • 太棒了,先生!完美运行。
    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2019-12-07
      • 2020-03-10
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2022-12-22
      相关资源
      最近更新 更多