【发布时间】:2014-11-06 16:18:10
【问题描述】:
我使用 xamarin.forms 创建了列表视图,我正在寻找一种在点击列表视图时不突出显示视图单元的方法。
请查看下图。
提前致谢:)
【问题讨论】:
标签: android listview xamarin.forms
我使用 xamarin.forms 创建了列表视图,我正在寻找一种在点击列表视图时不突出显示视图单元的方法。
请查看下图。
提前致谢:)
【问题讨论】:
标签: android listview xamarin.forms
在您的 ListView SelectedItem 事件处理程序上,您可以这样做:
listview.SelectedItem = null;
这将为您提供点击突出显示,但状态将只是暂时的。
在你的情况下,我想你会喜欢这个,因为你使用 2 Images 而不是 Buttons 作为右边的箭头,TapGestureRecognizer。你知道Button 有一个Image 属性吗?在Cell 中单击Button 时,Cell 的选定状态不应更改。
【讨论】:
只需将其放入您的自定义主题中即可:
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
【讨论】:
你不能,你必须实现一个自定义渲染。如果您只是将选定项目设置为空,它将删除选定的颜色。但是您将首先选择该项目,然后再次取消选择它(多个事件)但您没有看到它:-)。如果您在 Windows Phone 上启用了倾斜效果,由于所有事件,倾斜仍然会发生!
但我希望看到 Xamarin Forms 团队在列表视图上实现 CanSelect 属性。
【讨论】:
我想建议您另一种解决方案。 您可以添加:
IsEnabled="False"
在您的列表视图小部件中。 就我而言,这个解决方案效果很好。
【讨论】:
对于在 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;
}
}
}
这似乎是正确和最好的方式......!
【讨论】:
我想分享另一个我非常喜欢的解决方案,因为它很简单,你必须实现一次,而且在 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>
【讨论】:
我在 Xamarin.Forms 中使用:ListView SelectionMode = "None"。
【讨论】: