Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms ListView change Cell on ContextMenu open

I use Xamarin.Forms to define a ListView. This ListView defines some ContextActions on inside the ViewCell. Depending on the platform, these context actions are then presented to the user. In Android, this is triggered by long-pressing the specific item. Sadly, this Item will not be (properly) highlighted, as can be seen in this screenshot (I long-pressed Third Item, sadly I can't yet embed images).

enter image description here

Is there a way to modify the Cell when the context menu opens? Specifically asking for a solution for Android, but a general answer is welcome as well. The goal eventually is to improve highlighting, e.g. by changing the cell's background color. Modifying the cell, when one ContextAction is pressed, is not what I am looking for.

I browsed through the source code of Xamarin.Forms and thought about somehow inheriting from e.g. the ViewCell class, but couldn't find an event or command that would be triggered / called upon long-pressing an item. I have set up a simple repository to which illustrates the behavior: GitHub repository

The most important code snippets

  • ListView definition in XAML

    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewContextMenu" x:Class="ListViewContextMenu.ListViewContextMenuPage">
        <ListView x:Name="MyListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Text="Action" Command="{Binding OnAction}" CommandParameter="{Binding .}"/>
                        </ViewCell.ContextActions>
                        <Label Text="{Binding Name}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage>
    
  • MyItem definition (MVVM)

    using System.Diagnostics;
    using Xamarin.Forms;
    
    namespace ListViewContextMenu
    {
        public class MyItem
        {
            public string Name { get; set; }
            public Command OnAction { get; set; }
    
            public MyItem()
            {
                OnAction = new Command((obj) => Debug.WriteLine($"Item {obj.ToString()} clicked"));
            }
        }
    }
    
like image 456
patzm Avatar asked Apr 28 '17 22:04

patzm


1 Answers

No need for custom renderer - you can simply add following tag(s) to styles.xml (location: Android project > Resources > values > styles.xml)

<style name="MyTheme" parent="MyTheme.Base">
  <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
</style>
<color name="ListViewHighlighted">#A8A8A8</color>

More details can be found at this post.

enter image description here

like image 98
Sharada Gururaj Avatar answered Oct 11 '22 18:10

Sharada Gururaj