在 onOptionsItemSelected 中获取特定的 MenuItem 并将其设置为 SHOW_AS_ACTION_NEVER

Get specific MenuItem inside onOptionsItemSelected and setting it as SHOW_AS_ACTION_NEVER

我有一个包含 4 个菜单项的菜单,单击其中一个时我想将它移到顶部(MenuItem.SHOW_AS_ACTION_ALWAYS) 而前一个 selected 应该向下(MenuItem.SHOW_AS_ACTION_NEVER )。我已经设法将 selected 添加到顶部,但我不知道如何将前一个 selected 向下移动。

我的想法是得到所有其他的并将它们设置为 MenuItem.SHOW_AS_ACTION_NEVER。我尝试 select 像这样

  val javaItem = findViewById<MenuItem>(R.id.javaMenuItem)

但它说它期待一个视图

这是我的菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/javaMenuItem"
        android:title="@string/javaItem" />
    <item
        android:id="@+id/pythonMenuItem"
        android:title="@string/pythonItem" />

    <item
        android:id="@+id/rubyMenuItem"
        android:title="@string/rubyItem" />
    <item
        android:id="@+id/javaScriptItem"
        android:title="@string/javaScriptItem" />
</menu>

这就是我所做的

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val notSelectedTextView = findViewById<TextView>(R.id.middleTextMessageTextView);
        notSelectedTextView.visibility = View.GONE

        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
            when (item.itemId) {
                R.id.javaMenuItem -> {
                    getDataFromApi("Java")
                    setListView();
                }
                R.id.pythonMenuItem -> {
                    getDataFromApi("Python")
                    setListView();
                }
                R.id.rubyMenuItem -> {
                    getDataFromApi("Ruby")
                    setListView();
                }
                R.id.javaScriptItem -> {
                    getDataFromApi("JavaScript")
                    setListView();
                }
            }
    
            return super.onOptionsItemSelected(item)
        }

您可以将菜单保存在本地字段中,对于onOptionsItemSelected()的每次调用,您可以迭代所有菜单项,并将它们设置为MenuItem.SHOW_AS_ACTION_NEVER

假设菜单名为 R.menu.my_menu,将菜单保存到本地变量:

var mMenu: Menu? = null
override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.my_menu, menu)
    mMenu = menu
    return true
}

然后迭代菜单项以将它们的操作设置为 SHOW_AS_ACTION_NEVER,然后再将点击的项设置为 SHOW_AS_ACTION_ALWAYS

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val notSelectedTextView = findViewById<TextView>(R.id.middleTextMessageTextView);
        notSelectedTextView.visibility = View.GONE

        // iterate over menu items
        for (i in 0 until mMenu?.size()!!) {
            val menuItem: MenuItem = mMenu!!.getItem(i)
            menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
        }

        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
            when (item.itemId) {
                R.id.javaMenuItem -> {
                    getDataFromApi("Java")
                    setListView();
                }
                R.id.pythonMenuItem -> {
                    getDataFromApi("Python")
                    setListView();
                }
                R.id.rubyMenuItem -> {
                    getDataFromApi("Ruby")
                    setListView();
                }
                R.id.javaScriptItem -> {
                    getDataFromApi("JavaScript")
                    setListView();
                }
            }
    
            return super.onOptionsItemSelected(item)
        }