android - onOptionsItemSelected不能在 fragment 中工作

标签 android android-fragments kotlin android-toolbar

我试图在我的ViewPager Fragmets之一中放置一个菜单。当您单击选项之一时,不执行操作,仅关闭菜单。我在这里在StackOverFlow上寻找其他问题,但是它们并没有太大帮助。
如何解决此错误?

class CFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_c, container, false)
        val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
        toolbar.inflateMenu(R.menu.menu_c)


        //...

return view
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_c, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        if (item.itemId == R.id.optionOne) {
            Toast.makeText(requireActivity(), "One", Toast.LENGTH_SHORT).show()
            return true
        } else if (item.itemId == R.id.optionTwo) {
            startActivity(Intent(requireActivity(), Members::class.java))
            return true
        } else if (item.itemId == R.id.optionThree) {
            Toast.makeText(requireActivity(), "Three", Toast.LENGTH_SHORT).show()
            return true
        }else{
            return super.onOptionsItemSelected(item)
        }
    }
}

菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/optionOne"
        app:showAsAction="never"
        android:title="@string/optionOne">
    </item>
    <item android:id="@+id/optionTwo"
        android:title="@string/optionTwo">
    </item>
    <item android:id="@+id/optionThree"
        android:title="@string/optionThree">
    </item>
</menu>

最佳答案

当您尝试在工具栏布局中添加菜单项时,请执行此操作。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_c, container, false)
        val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
        toolbar.inflateMenu(R.menu.menu_c)
        toolbar.setOnMenuItemClickListener { item ->
            when (item.itemId) {
                R.id.optionOne -> {
                                       Toast.makeText(requireActivity(), "One", 
                                       Toast.LENGTH_SHORT).show()
                                       }
                R.id.optionTwo -> {
                                       startActivity(Intent(requireActivity(), 
                                       Members::class.java))
                                       }
              // your reset case
            }
            true
        }
      return view
    }

无需覆盖
onCreateOptionsMenu

关于android - onOptionsItemSelected不能在 fragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58345803/

相关文章:

java - 为什么这有效? (在主线程中生成值并在 Android 中的其他后台线程中使用它们)

Android MulticastSocket.joinGroup 不会触发发送 IGMP 消息

java - Exoplayer 库版本冲突

android - 以编程方式为 View 创建资源 ID

Android 11(仅限)FragmentManager 崩溃 - FragmentManager 已被销毁

android - 转到另一个 fragment 时, float 操作按钮不会消失

android - Flutter:如何启动和 android intent 并将数据返回到 flutter

android - 为什么当协程试图进入 Dispatchers.Main 时应用程序终止?

java - 安卓聊天客户端: socket connection

android - 使用 onBackPressed() 进行 fragment 转换