Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable selection highlight in listview xamarin.forms on android

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 :)

like image 949
Femil Shajin Avatar asked Sep 12 '14 13:09

Femil Shajin


4 Answers

Just put this in your custom theme:

<item name="android:colorActivatedHighlight">@android:color/transparent</item>
like image 184
Marian Avatar answered Oct 13 '22 22:10

Marian


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

like image 41
michal Avatar answered Oct 11 '22 23:10

michal


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.

like image 8
Stephane Delcroix Avatar answered Oct 13 '22 20:10

Stephane Delcroix


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>
like image 7
Gabriel Weidmann Avatar answered Oct 13 '22 20:10

Gabriel Weidmann