未使用自定义actionLayout为Action Bar菜单项调用OnOptionsItemSelected

时间:2023-01-16 12:29:02

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

我正在尝试在操作栏中实现通知图标以显示通知计数。就像是

未使用自定义actionLayout为Action Bar菜单项调用OnOptionsItemSelected

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. It works fine for the other two. I did google this and ound this link: onOptionsItemSelected not getting called when using custom action view

UI看起来很好,但没有为通知图标调用OnOptionsItemSelected。它适用于其他两个。我确实google了这个链接:onOptionsItemSelected在使用自定义操作视图时没有被调用

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.

但它对我不起作用。它永远不会触发click事件。请帮忙。

2 个解决方案

#1


0  

@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 :)

使用此代码......希望它有帮助:)

#2


0  

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”事情......我强硬的标准值是真的吗?

#1


0  

@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 :)

使用此代码......希望它有帮助:)

#2


0  

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”事情......我强硬的标准值是真的吗?