Skip to content

Commit 4e8d2da

Browse files
Pedro JesusPedro Jesus
authored andcommitted
android love
1 parent 05e74a9 commit 4e8d2da

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

samples/PJ.ContextActions.Sample/MainPage.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
Text="Isso é um header!" />
1515
</local:MyCV.Header>
1616

17+
<local:MyCV.Footer>
18+
<Label Text="HEllo I'm a Footer"/>
19+
</local:MyCV.Footer>
20+
1721
<local:MyCV.ContextActions>
1822
<local:MenuItem Clicked="MenuItem_Clicked" Text="Primeiro" />
1923
<local:MenuItem Command="{Binding ClickCommand}" Text="Segundo" />

samples/PJ.ContextActions.Sample/Platforms/Android/MyCollectionViewHandler.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Android.Views;
44
using AndroidX.RecyclerView.Widget;
55
using Microsoft.Maui.Controls.Handlers.Items;
6+
using Xamarin.Google.ErrorProne.Annotations;
67

78
namespace PJ.ContextActions.Sample.Platforms.Android;
89

@@ -65,13 +66,18 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
6566
}
6667
}
6768

68-
69+
6970
// TODO: IS this safe? I mean, there can be only one header?
7071
position -= itemsSource.HasHeader ? 1 : 0;
72+
var size = itemsSource.Count - (itemsSource.HasFooter ? 1 : 0) - (itemsSource.HasHeader ? 1 : 0);
7173

72-
holder.ItemView.Tag = position;
73-
var contextMenuListener = new ItemContextMenuListener(position);
74-
holder.ItemView.SetOnCreateContextMenuListener(contextMenuListener);
74+
if (position >= 0 && position < size)
75+
{
76+
var element = itemsSource.GetItem(position + 1);
77+
holder.ItemView.Tag = position;
78+
var contextMenuListener = new ItemContextMenuListener(element);
79+
holder.ItemView.SetOnCreateContextMenuListener(contextMenuListener);
80+
}
7581

7682
System.Diagnostics.Debug.WriteLine($"Set context menu listener for position {position}");
7783
}
@@ -91,11 +97,11 @@ public override void OnViewRecycled(Java.Lang.Object holder)
9197

9298
public class ItemContextMenuListener : Java.Lang.Object, global::Android.Views.View.IOnCreateContextMenuListener
9399
{
94-
readonly int position;
100+
readonly object element;
95101

96-
public ItemContextMenuListener(int position)
102+
public ItemContextMenuListener(object element)
97103
{
98-
this.position = position;
104+
this.element = element;
99105
}
100106

101107
public void OnCreateContextMenu(IContextMenu? menu, global::Android.Views.View? v, IContextMenuContextMenuInfo? menuInfo)
@@ -112,34 +118,38 @@ public void OnCreateContextMenu(IContextMenu? menu, global::Android.Views.View?
112118
foreach (var (index, item) in menuItems.Index())
113119
{
114120
var mItem = menu.Add(0, index + 1, index, item.Text)!;
115-
mItem.SetOnMenuItemClickListener(new MenuItemClickListener(position,item));
121+
mItem.SetOnMenuItemClickListener(new MenuItemClickListener(new(element, item)));
116122
}
117123

118-
System.Diagnostics.Debug.WriteLine($"Floating context menu created for position: {position} (listener-based, no Activity!)");
124+
System.Diagnostics.Debug.WriteLine($"Floating context menu created for position: {element} (listener-based, no Activity!)");
119125
}
120126
}
121127

122128
public class MenuItemClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener
123129
{
124-
readonly int position;
125-
readonly MenuItem menuItem;
130+
readonly CommandBag bag;
126131

127-
public MenuItemClickListener(int position, MenuItem item)
132+
public MenuItemClickListener(CommandBag bag)
128133
{
129-
this.menuItem = item;
134+
this.bag = bag;
130135
}
131136

132137
public bool OnMenuItemClick(IMenuItem? item)
133138
{
134139
if (item is null) return false;
135140

136-
menuItem.FireClicked();
141+
var menuItem = bag.item;
142+
var element = bag.cvItem;
137143

138-
if (menuItem.Command?.CanExecute(null) is true)
144+
menuItem.FireClicked(element);
145+
146+
if (menuItem.Command?.CanExecute(element) is true)
139147
{
140-
menuItem.Command?.Execute(null);
148+
menuItem.Command?.Execute(element);
141149
}
142150

143151
return true; // Consume the click event
144152
}
145-
}
153+
}
154+
155+
public record CommandBag(object cvItem, MenuItem item);

0 commit comments

Comments
 (0)