6

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"));
            }
        }
    }
    
jsanalytics
  • 12,538
  • 4
  • 19
  • 40
maddin25
  • 793
  • 10
  • 19
  • Identifying when the longpress gesture has happened is probably a good way to do this. Have you looked at how Xlabs detected LongPress? https://github.com/XLabs/Xamarin-Forms-Labs/search?utf8=%E2%9C%93&q=longpress – Ben Reierson Apr 29 '17 at 05:08
  • You can use a Customrenderer or Effects to handle the item longpress manually ex: Control.ItemLongClick += Control_ItemLongClick; where you customize the look and feel – Dilmah Aug 23 '17 at 07:31

1 Answers1

1

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

Sharada Gururaj
  • 12,843
  • 1
  • 18
  • 45