Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] ListView ContextActions don't show up in modal page #21261

Open
DanielGramsJTL opened this issue Mar 18, 2024 · 4 comments · May be fixed by #26854
Open

[Android] ListView ContextActions don't show up in modal page #21261

DanielGramsJTL opened this issue Mar 18, 2024 · 4 comments · May be fixed by #26854
Labels
area-controls-modal platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@DanielGramsJTL
Copy link

DanielGramsJTL commented Mar 18, 2024

Description

I have a ListView with ViewCell as base in the DataTemplate. The ViewCell has ContextActions. The ListView is part of a page that is shown with Navigation.PushModalAsync. On Android, the context actions don't show up in the NavigationBar.

Steps to Reproduce

  1. Create a new MAUI project
  2. Add modal page with ListView and context actions
  3. Add await this.Navigation.PushModalAsync(new NavigationPage(new ModalPage())); as button action on the main page
  4. Start app, open modal and long click on list item
  5. The context actions don't show up in the NavigationBar

Link to public reproduction project repository

https://github.com/DanielGramsJTL/MauiModalContextActions

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No.

Relevant log output

No response

@DanielGramsJTL DanielGramsJTL added the t/bug Something isn't working label Mar 18, 2024
@ninachen03 ninachen03 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Mar 18, 2024
@ninachen03
Copy link

Verified this issue with Visual Studio 17.10.0 Preview 2. Can repro it on android platform with sample project.
Below is windows platform. Have Favorite MenuItem
image
This is android platform --> No favorite MenuItem
image

@jefh531
Copy link

jefh531 commented Jun 27, 2024

I have the same issue with Visual Studio 17.11.0 Preview 2.1

@guyvaio
Copy link

guyvaio commented Jun 27, 2024

One more regression and known unfixed bug in MAUI.

@samhouts samhouts removed s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 3, 2024
@guyvaio
Copy link

guyvaio commented Jul 10, 2024

For those who don't want to get stuck on this bug, here is a ListView class that works in modal. All context menus appear in a DisplayActionSheet. It's not optimal but it works.

namespace MyNameSpace
{
    public class ListViewInModal : ListView
    {
        public ListViewInModal() 
            :base(ListViewCachingStrategy.RetainElement) // CachingStrategy removed from XAML
        { }

#if ANDROID
        protected override void SetupContent(Cell cell, int index)
        {
            cell.Tapped -= Cell_Tapped;
            cell.Tapped += Cell_Tapped;
            base.SetupContent(cell, index);
        }

        private ViewCell currentCell;
        private Page hostingPage;

        private void Cell_Tapped(object sender, EventArgs e) // triggered by NotifyRowTapped
        {
            currentCell = sender as ViewCell;
        }

        protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();
            Android.Widget.ListView nativeListView = Handler.PlatformView as Android.Widget.ListView;
            if (nativeListView != null)
            {
                Element element = this;
                while (element?.Parent != null)
                {
                    if (element.Parent is Page page)
                    {
                        hostingPage = page;
                        break;
                    }
                    element = element.Parent;
                }

                if (hostingPage != null)
                {
                    nativeListView.LongClickable = true;
                    nativeListView.ItemLongClick += NativeListView_ItemLongClick;
                }
            }
        }

        private async void NativeListView_ItemLongClick(object sender, Android.Widget.AdapterView.ItemLongClickEventArgs e)
        {
            currentCell = null;
            NotifyRowTapped(e.Position - 1); // will set currentCell via ViewCell_Tapped

            if (currentCell?.ContextActions?.Any(ca => ca.IsEnabled) ?? false)
            {
                string contextActionDestructive = currentCell.ContextActions.FirstOrDefault(ca => ca.IsEnabled && ca.IsDestructive)?.Text;
                string[] contextActionsOthers = currentCell.ContextActions.Where(ca => ca.IsEnabled && !ca.IsDestructive).Select(ca => ca.Text).ToArray();
                string selectedContextAction = await hostingPage.DisplayActionSheet("", "Cancel", contextActionDestructive, contextActionsOthers);
                MenuItem selectedMenuItem = currentCell.ContextActions.FirstOrDefault(ca => ca.Text == selectedContextAction);
                if (selectedMenuItem != null)
                {
                    MethodInfo mi = typeof(MenuItem).GetMethod("OnClicked", BindingFlags.NonPublic | BindingFlags.Instance);
                    mi?.Invoke(selectedMenuItem, []);
                }
            }
            e.Handled = true;
            currentCell = null;
        }

#endif
    }
}

@samhouts samhouts added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 10, 2024
@kubaflo kubaflo linked a pull request Dec 29, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-modal platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants