1- using Android . Content ;
1+ using System . Security . Principal ;
2+ using Android . Content ;
23using Android . Views ;
34using AndroidX . RecyclerView . Widget ;
45using Microsoft . Maui . Controls . Handlers . Items ;
56
67namespace PJ . ContextActions . Sample . Platforms . Android ;
8+
79class MyCVHandler : CollectionViewHandler
810{
911 protected override RecyclerView CreatePlatformView ( )
@@ -28,8 +30,13 @@ public MyRecyclerView(Context context, Func<IItemsLayout> getItemsLayout, Func<G
2830public class MyViewAdapter : ReorderableItemsViewAdapter < ReorderableItemsView , IGroupableItemsViewSource >
2931{
3032 IGroupableItemsViewSource itemsSource = default ! ;
33+ ReorderableItemsView collectionView ;
34+
35+ internal static MenuItem [ ] ? MenuItems ;
36+
3137 public MyViewAdapter ( ReorderableItemsView reorderableItemsView , Func < Microsoft . Maui . Controls . View , Context , ItemContentView > ? createView = null ) : base ( reorderableItemsView , createView )
3238 {
39+ collectionView = reorderableItemsView ;
3340 }
3441
3542 protected override IGroupableItemsViewSource CreateItemsSource ( )
@@ -41,6 +48,24 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
4148 {
4249 base . OnBindViewHolder ( holder , position ) ;
4350
51+ if ( collectionView is not MyCV myCV )
52+ {
53+ return ;
54+ }
55+
56+ var contextActions = myCV . ContextActions ;
57+ if ( MenuItems is null && contextActions . Count > 0 )
58+ {
59+ MenuItems = new MenuItem [ contextActions . Count ] ;
60+
61+ foreach ( var ( index , item ) in contextActions . Index ( ) )
62+ {
63+ item . BindingContext = myCV . BindingContext ;
64+ MenuItems [ index ] = item ;
65+ }
66+ }
67+
68+
4469 // TODO: IS this safe? I mean, there can be only one header?
4570 position -= itemsSource . HasHeader ? 1 : 0 ;
4671
@@ -75,20 +100,20 @@ public ItemContextMenuListener(int position)
75100
76101 public void OnCreateContextMenu ( IContextMenu ? menu , global ::Android . Views . View ? v , IContextMenuContextMenuInfo ? menuInfo )
77102 {
78- if ( menu == null || v is null ) return ;
103+ if ( menu is null || v is null ) return ;
79104
80- menu . SetHeaderTitle ( $ "Item { position } ") ;
81-
82- var editItem = menu . Add ( 0 , 1 , 0 , "Edit Item" ) ! ;
83- var deleteItem = menu . Add ( 0 , 2 , 1 , "Delete Item" ) ! ;
84- var shareItem = menu . Add ( 0 , 3 , 2 , "Share Item" ) ! ;
85- var addItem = menu . Add ( 0 , 4 , 3 , "Add Item" ) ! ;
105+ if ( MyViewAdapter . MenuItems is null )
106+ {
107+ return ;
108+ }
86109
110+ var menuItems = MyViewAdapter . MenuItems ;
87111
88- editItem . SetOnMenuItemClickListener ( new MenuItemClickListener ( position , "Edit" ) ) ;
89- deleteItem . SetOnMenuItemClickListener ( new MenuItemClickListener ( position , "Delete" ) ) ;
90- shareItem . SetOnMenuItemClickListener ( new MenuItemClickListener ( position , "Share" ) ) ;
91- addItem . SetOnMenuItemClickListener ( new MenuItemClickListener ( position , "Add" ) ) ;
112+ foreach ( var ( index , item ) in menuItems . Index ( ) )
113+ {
114+ var mItem = menu . Add ( 0 , index + 1 , index , item . Text ) ! ;
115+ mItem . SetOnMenuItemClickListener ( new MenuItemClickListener ( position , item ) ) ;
116+ }
92117
93118 System . Diagnostics . Debug . WriteLine ( $ "Floating context menu created for position: { position } (listener-based, no Activity!)") ;
94119 }
@@ -97,39 +122,22 @@ public void OnCreateContextMenu(IContextMenu? menu, global::Android.Views.View?
97122public class MenuItemClickListener : Java . Lang . Object , IMenuItemOnMenuItemClickListener
98123{
99124 readonly int position ;
100- readonly string action ;
125+ readonly MenuItem menuItem ;
101126
102- public MenuItemClickListener ( int position , string action )
127+ public MenuItemClickListener ( int position , MenuItem item )
103128 {
104- this . position = position ;
105- this . action = action ;
129+ this . menuItem = item ;
106130 }
107131
108132 public bool OnMenuItemClick ( IMenuItem ? item )
109133 {
110- if ( item == null ) return false ;
134+ if ( item is null ) return false ;
111135
112- System . Diagnostics . Debug . WriteLine ( $ " { action } selected for item at position { position } " ) ;
136+ menuItem . FireClicked ( ) ;
113137
114- // Handle the action based on the type
115- switch ( action )
138+ if ( menuItem . Command ? . CanExecute ( null ) is true )
116139 {
117- case "Edit" :
118- // Handle edit action
119- System . Diagnostics . Debug . WriteLine ( $ "Handling edit for position { position } ") ;
120- break ;
121- case "Delete" :
122- // Handle delete action
123- System . Diagnostics . Debug . WriteLine ( $ "Handling delete for position { position } ") ;
124- break ;
125- case "Share" :
126- // Handle share action
127- System . Diagnostics . Debug . WriteLine ( $ "Handling share for position { position } ") ;
128- break ;
129- case "Add" :
130- // Handle add action
131- System . Diagnostics . Debug . WriteLine ( $ "Handling add for position { position } ") ;
132- break ;
140+ menuItem . Command ? . Execute ( null ) ;
133141 }
134142
135143 return true ; // Consume the click event
0 commit comments