Hi,
When using context menu in list view (Xamarin Forms), when user long press item, it shows Context action menu bar.
If user don't want to take any action, then can click the back arrow button on top (at context action bar itself).
How to hide context action menu bar or cancel if user click outside context menu bar i-e- any where on screen or any other list view item
Thanks
<Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <ListView x:Name="listView" ItemSelected="OnItemSelected" ItemTapped="listView_ItemTapped"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.ContextActions> <MenuItem Clicked="OnMore" CommandParameter="{Binding .}" Text="More" /> <MenuItem Clicked="OnDelete" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" /> </ViewCell.ContextActions> <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal"> <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical"> <Label Text="{Binding .}" VerticalTextAlignment="Center" FontSize="Medium" /> <Label Text="hint: trigger a context action" VerticalTextAlignment="Center" FontSize="Small" /> </StackLayout> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> <Button x:Name="button" IsVisible="False" BackgroundColor="Transparent" Clicked="Button_Clicked_1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/> </Grid>
public ContextActionsXaml () { InitializeComponent (); items = new ObservableCollection<string> { "alpha", "beta", "gamma", "delta", "epsilon" }; //listView.ItemsSource = items; listView.SetBinding (ListView.ItemsSourceProperty, new Binding (".")); listView.BindingContext = items; MessagingCenter.Subscribe<object>(this, "Hi", (sender) => { button.IsVisible = true; }); } private void Button_Clicked_1(object sender, EventArgs e) { if (Device.RuntimePlatform == Device.Android) DependencyService.Get<IContextManager>().CloseLastContextMenu(); button.IsVisible = false; }
public interface IContextManager { void CloseLastContextMenu(); }
[assembly: Xamarin.Forms.Dependency(typeof(ContextManager))] namespace WorkingWithListview.Android { public class ContextManager : IContextManager { public static global::Android.Support.V7.View.ActionMode LastActionMode { get; set; } public void CloseLastContextMenu() { LastActionMode?.Finish(); } } }
public override global::Android.Support.V7.View.ActionMode StartSupportActionMode(global::Android.Support.V7.View.ActionMode.ICallback callback) { MessagingCenter.Send<object>(this, "Hi"); return ContextManager.LastActionMode = base.StartSupportActionMode(callback); } public override void OnSupportActionModeFinished(global::Android.Support.V7.View.ActionMode mode) { ContextManager.LastActionMode = null; base.OnSupportActionModeFinished(mode); }
Refer https://forums.xamarin.com/discussion/comment/308577/#Comment_308577 .
Answers
Disable Context Action click
ViewCell.ContextActions.Clear();
Xaml
MainPage
Interface in Forms
Implementation
MainActivity
Refer https://forums.xamarin.com/discussion/comment/308577/#Comment_308577 .
Hi @ColeX
Thanks It works only when we click outside listview (that transparent button appears below listview)
It wouldn't work when we click any where on screen, for example clicking an item on list view item should close that.
Check my project .
Thanks @ColeX
The difference was that listview was inside StackLayout, after moving it to inside Grid it Works.