I have created listview using xamarin.forms, I am searching for a way not to highlight the viewcell while tapping on the listview.
please check on the image below.
Thanks in advance :)
on your ListView SelectedItem
event handler, you can do:
listview.SelectedItem = null;
that'll give you the on click highlight, but the state will be transient only.
In your case, I guess you'd like this because you're using 2 Image
s instead of Button
s for the arrows on the right, with a TapGestureRecognizer
. Do you know the Button
has an Image
property? When clicking on a Button
in a Cell
, the Cell
selected state shouldn't change.
Just put this in your custom theme:
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
I want to share another solution I really like, because it's simple, you have to implement it once and it's really easy to use in XAML.
First we have to implement our own behavior. That's quite simple:
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;
}
}
So we're just registering to the event when the behavior is set and unregister it when it's unset.
The event itself uses Stephane Delcroix approach.
Now everything you have to do in the ListView is to add the behavior like this:
<ListView ...>
<ListView.Behaviors>
<behaviors:DeselectItemBehavior />
</ListView.Behaviors>
I'm using: ListView SelectionMode = "None"
in Xamarin.Forms.
For disabling Listview cell selection highlight in both Android and iOS, this link was very helpful
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;
}
}
}
This seems to be the right & best way...!
You can't, you have to implement a custom render. If you just set selected item to null, it will remove the selected color. But you will first have selected the item and then deselected it again (multible events) but your did'nt see it :-). If you enabled tilt effect on Windows Phone, the tilt is still happen because of all the event!
But i would like to see the Xamarin Forms team implement a CanSelect properties on the listview.
I want to suggest you another solution. You can add:
IsEnabled="False"
in your listview widget. In my case this solution works well.