简体   繁体   English

OnOptionsItemSelected 没有为具有自定义 actionLayout 的操作栏菜单项调用

[英]OnOptionsItemSelected not being called for Action Bar Menu Item with custom actionLayout

I'm trying to implement a notification icon in my actionbar to show the count of notifications.我正在尝试在我的操作栏中实现一个通知图标来显示通知计数。 Something like就像是

在此处输入图片说明

I've added a custom layout file for it NotificationIcon.xml :我为它添加了一个自定义布局文件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>

And used it in my menu as main_activity_actions.xml :并在我的菜单中使用它作为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>

The UI looks fine but the OnOptionsItemSelected is not being called for the notification icon. UI 看起来不错,但没有为通知图标调用 OnOptionsItemSelected。 It works fine for the other two.它适用于其他两个。 I did google this and ound this link: onOptionsItemSelected not getting called when using custom action view我确实在谷歌上搜索了这个链接: 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);
        }

but it does not works for me.但它对我不起作用。 It never fires the click event.它永远不会触发点击事件。 Please help.请帮忙。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();

                inflater.inflate(R.menu.main_activity_actions, menu);

        final View notification_icon= menu.findItem(R.id.notification_icon).getActionView();


        notification_icon.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
// write ur code here
}

        });
        return super.onCreateOptionsMenu(menu);
    }

use this code......hope it helps :)使用此代码......希望它有帮助:)

i struggle with the same Problem and found following Solution:我遇到了同样的问题,并找到了以下解决方案:

First of all: i really don't know why but if you delete the首先:我真的不知道为什么但如果你删除

android:clickable="true"

then ... and ONLY then you get the click event!!!然后......只有这样你才能得到点击事件!!!

Second: you have to set the Listener...第二:你必须设置监听器......

...    
item.getActionView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do Your Stuff.....
        }
});

May someone can Explain why this is with the "android:clickable" Thing... i tough the standard value IS true??有人可以解释为什么这是与“android:clickable”的事情......我坚持标准值是真的?

You had made ImageButton clickable, and then written the clickListener for the Layout of menuItem.您已将 ImageButton 设为可点击,然后为 menuItem 的布局编写了 clickListener。 You can observe that if you click outside the ImageButton but inside the MenuItem layout, your listener will work.您可以观察到,如果您在 ImageButton 外部但在 MenuItem 布局内部单击,您的侦听器将起作用。 The reason is simple clickListener for ImageButton has nothing to do, on setting Clickable = true defines whether this button reacts to click events.原因很简单,ImageButton 的 clickListener 无关,设置 Clickable = true 定义此按钮是否对单击事件做出反应。 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.只需简单地设置 Clickable = false,您的按钮将不会对其点击事件做出反应,最终您的整个布局将对您定义的 clickListener 做出反应,然后您的问题就会得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击设置了actionLayout的菜单项时未调用onOptionsItemSelected() - onOptionsItemSelected() not called when clicking on menu item which has an actionLayout set on it 菜单中的 onOptionsItemSelected 对于使用 actionLayout 的项目不可点击 - onOptionsItemSelected in Menu is not clickable for item using actionLayout 自定义菜单图标未在onOptionsItemSelected中调用 - custom menu icon not being called in onOptionsItemSelected Android操作栏菜单项,actionLayout无法正常工作 - Android action bar menu item with actionLayout not working properly 操作栏子菜单不与actionLayout一起使用 - Action bar sub menu not working with actionLayout 使用actionLayout属性时,为什么不能单击我的操作栏菜单项? - Why can't I click my action bar menu item when using actionLayout property? 使用actionLayout(SherlockActionBar)时未调用onOptionsItemSelected - onOptionsItemSelected not called when using actionLayout (SherlockActionBar) 如何在单击操作项时停止从先前添加的Fragment中调用onOptionsItemSelected? - How to stop onOptionsItemSelected being called from previously added Fragment on click of action item? 操作栏自定义组件(自定义开关)未与actionLayout一起显示 - Action bar custom component (a custom switch) is not showing with actionLayout 使用actionLayout触摸菜单项上的反馈 - Touch feedback on menu item with actionLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM