Skip to content

Commit

Permalink
Optimize dropdown android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
enisn committed Jul 13, 2024
1 parent ac4628c commit 4410758
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/UraniumUI/Handlers/DropdownHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if ANDROID
using Android.Views;
using Android.Widget;
using Google.Android.Material.Button;
using Java.Interop;
using Microsoft.Maui.Handlers;
using UraniumUI.Controls;

Expand All @@ -16,27 +18,53 @@ protected override MaterialButton CreatePlatformView()
{
var button = base.CreatePlatformView();
button.Text = VirtualViewDropdown?.SelectedItem?.ToString();
button.Click += Button_Click;
return button;
}

private void Button_Click(object sender, EventArgs e)
{
var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;

var popupMenu = new Android.Widget.PopupMenu(activity, PlatformView, GravityFlags.Top);
var popupMenu = new Android.Widget.PopupMenu(activity, PlatformView, GravityFlags.Center);

if (VirtualViewDropdown.ItemsSource is not null)
{
foreach (var item in VirtualViewDropdown.ItemsSource)
{
popupMenu.Menu.Add(new Java.Lang.String(item.ToString()));
var menuItem = popupMenu.Menu.Add(new Java.Lang.String(item.ToString()));

menuItem.SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener((menuitem) =>
{
VirtualViewDropdown.SelectedItem = item;
}));
}
}

popupMenu.Show();
}

class MenuItemOnMenuItemClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener
{
private Action<IMenuItem> _onMenuItemClick;

public MenuItemOnMenuItemClickListener(Action<IMenuItem> onMenuItemClick)
{
_onMenuItemClick = onMenuItemClick;
}

public bool OnMenuItemClick(IMenuItem item)
{
_onMenuItemClick?.Invoke(item);
return true;
}
}

protected override void ConnectHandler(MaterialButton platformView)
{
base.ConnectHandler(platformView);
platformView.Click += Button_Click;
}

protected override void DisconnectHandler(MaterialButton platformView)
{
base.DisconnectHandler(platformView);
Expand Down

0 comments on commit 4410758

Please sign in to comment.