listview.SelectedItem = null;
这将给你点击高亮效果,但状态仅是短暂的。
我猜你会喜欢这个,因为你在右侧使用了两个 Image 来代替 Button,并使用了 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>
ListView SelectionMode = "None"。
针对在Android和iOS中禁用Listview单元格选择高亮的问题,这个链接非常有帮助。
https://gist.github.com/jessejiang0214/63b29b3166330c6fc083
在ProjectName.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;
}
}
}
最初的回答似乎是正确且最佳的方式...!这似乎是正确且最佳的方式...!
我想给你建议另一种解决方法。你可以添加:
IsEnabled="False"
在您的listview小部件中。 在我的情况下,这个解决方案很有效。