10

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. enter image description here

Thanks in advance :)

0

7 Answers 7

8

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 Images instead of Buttons 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.

4
  • 1
    When I click on image it doesn't change the listview state. It changes only when I click on the list other than image by mistake and it looks odd. I tried your solution it wasn't working.. changed the background color of view cell, it did the trick.. :) Thanks for your help..:) I wasn't aware of button having image property thanks for that too.. Sep 13, 2014 at 6:18
  • 1
    ItemSelected isn't nullable
    – Alexandre
    Oct 2, 2014 at 15:09
  • 2
    you mean .SelectedItem ItemSelected is the event. SelectedItem is the property. Mar 16, 2015 at 15:19
  • 2
    listview.SelectedItem = null;
    – Ajay Kumar
    Sep 26, 2015 at 7:44
7

Just put this in your custom theme:

<item name="android:colorActivatedHighlight">@android:color/transparent</item>
2
  • 2
    Thanks. It works. The default file where to add this line on the droid project is located in Resouces/values/styles.xml
    – eka808
    Mar 19, 2017 at 19:16
  • This worked fine... I will like to have the implement for the android project as well... Apr 4, 2021 at 21:17
7

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>
5

I'm using: ListView SelectionMode = "None" in Xamarin.Forms.

2
  • THIS supposed to be the accepted answer (this is the simplest and latest solution)
    – Zoli
    Feb 26, 2021 at 14:37
  • 1
    This deactivates the SelectionChanged functionality Apr 4, 2021 at 21:18
2

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...!

1

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.

1

I want to suggest you another solution. You can add:

IsEnabled="False"

in your listview widget. In my case this solution works well.

2
  • Thank you. Works great for me!
    – Max
    Oct 6, 2017 at 8:52
  • 2
    @TeoVr81 this will disable every event on the list not just selection e.g. you wont be able to scroll
    – Scarnet
    Jan 17, 2018 at 9:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.