带有自定义actionLayout的操作栏菜单项未调用OnOptionsItemSelected [英] OnOptionsItemSelected not being called for Action Bar Menu Item with custom actionLayout

查看:108
本文介绍了带有自定义actionLayout的操作栏菜单项未调用OnOptionsItemSelected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在操作栏中实现一个通知图标,以显示通知计数.

I'm trying to implement a notification icon in my actionbar to show the count of notifications. Something like

我为其添加了一个自定义布局文件 NotificationIcon.xml :

I've added a custom layout file for it NotificationIcon.xml:

  <!-- Menu Item Image -->
  <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="2dp"
      android:clickable="true"
      android:src="@drawable/notification" />

  <!-- Badge Count -->
  <TextView
      android:id="@+id/actionbar_notifcation_textview"
      android:layout_width="wrap_content"
      android:layout_height="20dp"
      android:minWidth="20dp"
      android:layout_alignParentRight="true"
      android:gravity="center_horizontal"
      android:background="@drawable/circle_green"
      android:fontFamily="sans-serif-black"
      android:textStyle="bold"
      android:text="0"
      android:textColor="#FFFFFF" />
</RelativeLayout>

并在我的菜单中将其用作 main_activity_actions.xml :

And used it in my menu as main_activity_actions.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
        android:id="@+id/action_add"
        android:title="@string/AddTag"
        android:icon="@+drawable/ic_action_new"
        android:showAsAction="always" />
  <item
        android:id="@+id/notification_icon"
        android:title="@string/PendingJobs"
        android:actionLayout="@layout/notificationIcon"
        android:icon="@+drawable/notification"
        android:showAsAction="always" />
  <item
        android:id="@+id/gps_status_icon"
        android:title="@string/GPS"
        android:icon="@+drawable/gps_grey"
        android:showAsAction="always" />
</menu>

UI看起来不错,但是未为通知图标调用OnOptionsItemSelected.对于其他两个来说,它工作正常.我在Google上搜索并找到了此链接: onOptionsItemSelected何时未调用使用自定义操作视图

The UI looks fine but the OnOptionsItemSelected is not being called for the notification icon. It works fine for the other two. I did google this and ound this link: onOptionsItemSelected not getting called when using custom action view

我试图在我的主要活动中实现它:

I tried to implement it in my main activity:

public override bool OnCreateOptionsMenu(IMenu menu)
        {
            actionBarMenu = menu;
            MenuInflater.Inflate(Resource.Menu.main_activity_actions, menu);

            var notificationMenuItem = menu.FindItem(Resource.Id.notification_icon);
            notificationMenuItem.ActionView.Click += (sender, args) => {
                this.OnOptionsItemSelected(notificationMenuItem);
            };

            return base.OnCreateOptionsMenu(menu);
        }

但是它对我不起作用.它从不触发click事件.请帮忙.

but it does not works for me. It never fires the click event. Please help.

推荐答案

您已使ImageButton可单击,然后为menuItem的布局编写了clickListener.您可以观察到,如果在ImageButton外部但在MenuItem布局内部单击,则侦听器将起作用.原因很简单,对于ImageButton的clickListener无关,设置Clickable = true时定义此按钮是否对单击事件作出反应.只需将Clickable = false设置为简单,您的按钮将不会对其单击事件作出反应,最终您的整个布局将对您定义的clickListener作出反应,然后您的问题将得到解决.

You had made ImageButton clickable, and then written the clickListener for the Layout of menuItem. You can observe that if you click outside the ImageButton but inside the MenuItem layout, your listener will work. The reason is simple clickListener for ImageButton has nothing to do, on setting Clickable = true defines whether this button reacts to click events. Just simply set Clickable = false, your button will not react to its click event, eventually your whole layout will react to your defined clickListener and then your problem will be solved.

这篇关于带有自定义actionLayout的操作栏菜单项未调用OnOptionsItemSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆