Xamarin.Forms ListView change Cell on ContextMenu open
Asked Answered
H

1

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"));
            }
        }
    }
    
Helm answered 28/4, 2017 at 22:34 Comment(2)
Identifying when the longpress gesture has happened is probably a good way to do this. Have you looked at how Xlabs detected LongPress? github.com/XLabs/Xamarin-Forms-Labs/…Cockcroft
You can use a Customrenderer or Effects to handle the item longpress manually ex: Control.ItemLongClick += Control_ItemLongClick; where you customize the look and feelDaegal
P
2

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

Pico answered 5/9, 2017 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.