[英]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.