onOptionsItemSelected 在片段中不起作用
onOptionsItemSelected not working in fragment
我正在尝试将菜单放入我的一个 ViewPager Fragmets 中。当您单击其中一个选项时,不会执行操作,只会关闭菜单。我在 Whosebug 上寻找其他问题,但它们对我帮助不大。
我该如何解决这个错误?
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>
我不知道您的应用主题或布局文件,但问题可能是您膨胀了工具栏:
val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
toolbar.inflateMenu(R.menu.menu_c)
此外,在您的代码中:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_c, menu)
super.onCreateOptionsMenu(menu, inflater)
}
这也会使您的菜单膨胀。
虽然 override fun onOptionsItemSelected(item: MenuItem): Boolean 只适用于第二个菜单,它可能在你膨胀的工具栏下面。
您没有在工具栏上设置 onMenuItemClickListener。
因此,如果您想使用在 onCreateView 中膨胀的工具栏,您必须实现:
toolbar.setOnMenuItemClickListener { /*TODO*/ }
当您尝试在工具栏布局中膨胀菜单项时,请执行此操作。
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
我正在尝试将菜单放入我的一个 ViewPager Fragmets 中。当您单击其中一个选项时,不会执行操作,只会关闭菜单。我在 Whosebug 上寻找其他问题,但它们对我帮助不大。 我该如何解决这个错误?
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>
我不知道您的应用主题或布局文件,但问题可能是您膨胀了工具栏:
val toolbar = view.findViewById(R.id.toolbarCFragement) as Toolbar
toolbar.inflateMenu(R.menu.menu_c)
此外,在您的代码中:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_c, menu)
super.onCreateOptionsMenu(menu, inflater)
}
这也会使您的菜单膨胀。 虽然 override fun onOptionsItemSelected(item: MenuItem): Boolean 只适用于第二个菜单,它可能在你膨胀的工具栏下面。 您没有在工具栏上设置 onMenuItemClickListener。
因此,如果您想使用在 onCreateView 中膨胀的工具栏,您必须实现:
toolbar.setOnMenuItemClickListener { /*TODO*/ }
当您尝试在工具栏布局中膨胀菜单项时,请执行此操作。
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