[英]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
我認為你可以在沒有自定義渲染器的情況下將其刪除。
您可以將StackPanel
的BackgroundColor
設置為灰色或列表或頁面的背景顏色。
<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.