[英]onOptionsItemSelected in Menu is not clickable for item using actionLayout
I have an android app with options menu, and the first item is defined with "actionLayout":我有一个带有选项菜单的 android 应用程序,第一个项目是用“actionLayout”定义的:
menu.xml snippet: menu.xml 片段:
<item
android:id="@+id/Cart"
android:title="Cart"
app:actionLayout="@layout/custom_action_item_layout"
app:showAsAction="always"/>
<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings_white_24dp"
android:title="@string/settings"
app:showAsAction="ifRoom" />
custom_action_item_layout snippet: custom_action_item_layout 片段:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_shopping_cart_white_24dp"/>
<TextView
android:id="@+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="@drawable/badge_background"
android:gravity="center"
android:padding="1dp"
android:textColor="@android:color/white"
android:textSize="12sp"/>
</FrameLayout>
then I set the menu up:然后我设置了菜单:
override fun onCreateOptionsMenu(menu_: Menu): Boolean {
menu = menu_
val inflater = menuInflater
inflater.inflate(R.menu.uppermenu, menu)
var cart = menu!!.findItem(R.id.Cart);
var actionView = getActionView(cart);
textCartItemCount = actionView.findViewById(R.id.cart_badge) as TextView
return true
}
however, the然而
override fun onOptionsItemSelected(item: MenuItem): Boolean { }
is never called when I click on the menu item.当我单击菜单项时从不调用。
I looked up the solution for the sherlockactionbar from 2012 for java, but the solution does not apply well: onOptionsItemSelected not called when using actionLayout (SherlockActionBar)我从 2012 年为 java 查找了 sherlockactionbar 的解决方案,但该解决方案并不适用: 使用 actionLayout (SherlockActionBar) 时未调用 onOptionsItemSelected
It seems related to the action Layout of the item, Without this, it is working well.它似乎与项目的动作布局有关,没有这个,它运行良好。 how to make the onOptionsItemSelected working?
如何使 onOptionsItemSelected 工作?
override fun onCreateOptionsMenu(menu_: Menu): Boolean {
menu = menu_
menuInflater.inflate(R.menu.uppermenu, menu)
var cartMenuItem = menu!!.findItem(R.id.Cart)
cartMenuItem.actionView?.cart.setOnClickListener {
cartMenuItem.actionView?.cart_badge.text = "content update in the textview"
}
return true
}
The above snippet should perform onClick event for you上面的代码段应该为您执行 onClick 事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.