[英]How to disable selection highlight in listview xamarin.forms on android
我已经使用 xamarin.forms 创建了列表视图,我正在寻找一种在点击列表视图时不突出显示视单元的方法。
请检查下面的图片。
提前致谢 :)
在您的 ListView SelectedItem
事件处理程序上,您可以执行以下操作:
listview.SelectedItem = null;
这会给你点击突出显示,但状态只会是瞬态的。
在您的情况下,我猜您会喜欢这个,因为您使用 2 Image
s 而不是Button
s 作为右侧的箭头,并带有TapGestureRecognizer
。 你知道Button
有一个Image
属性吗? 单击Cell
的Button
时, Cell
选择状态不应改变。
只需将其放在您的自定义主题中:
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
我想分享另一个我非常喜欢的解决方案,因为它很简单,您只需实现一次,并且在 XAML 中非常容易使用。
首先,我们必须实现我们自己的行为。 这很简单:
public class DeselectItemBehavior : Behavior<ListView>
{
protected override void OnAttachedTo(ListView bindable)
{
base.OnAttachedTo(bindable);
bindable.ItemSelected += ListView_ItemSelected;
}
protected override void OnDetachingFrom(ListView bindable)
{
base.OnDetachingFrom(bindable);
bindable.ItemSelected -= ListView_ItemSelected;
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
((ListView)sender).SelectedItem = null;
}
}
所以我们只是在设置行为时注册到事件,并在未设置时取消注册。
事件本身使用 Stephane Delcroix 方法。
现在你在 ListView 中要做的就是添加这样的行为:
<ListView ...>
<ListView.Behaviors>
<behaviors:DeselectItemBehavior />
</ListView.Behaviors>
对于在 Android 和 iOS 中禁用 Listview 单元格选择突出显示,此链接非常有用
https://gist.github.com/jessejiang0214/63b29b3166330c6fc083
项目名称.Android/Resources/values/styles.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme.Holo.Light">
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
</style>
</resources>
CustomAllViewCellRendereriOS.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof(ViewCell), typeof(MyAPP.iOS.CustomAllViewCellRendereriOS))]
namespace MyAPP.iOS
{
public class CustomAllViewCellRendereriOS : ViewCellRenderer
{
public override UIKit.UITableViewCell GetCell (Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
{
var cell = base.GetCell (item, reusableCell, tv);
if (cell != null)
cell.SelectionStyle = UIKit.UITableViewCellSelectionStyle.None;
return cell;
}
}
}
这似乎是正确和最好的方式......!
我在 Xamarin.Forms 中使用: ListView SelectionMode = "None"
。
你不能,你必须实现自定义渲染。 如果您只是将所选项目设置为空,它将删除所选颜色。 但是您将首先选择该项目,然后再次取消选择它(多重事件),但您没有看到它:-)。 如果您在 Windows Phone 上启用倾斜效果,由于所有事件,倾斜仍然发生!
但我希望看到 Xamarin Forms 团队在列表视图上实现 CanSelect 属性。
我想建议你另一种解决方案。 你可以加:
IsEnabled="False"
在您的列表视图小部件中。 就我而言,此解决方案效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.