片段中不可点击的选项菜单项

Not clickable options menu item in fragment

我有一个带工具栏的片段,我需要通过单击选项菜单项来处理导航。起初我以为问题出在导航上,但后来我认为它只是不可点击。我尝试使用 Toast 来查看它是否正常工作,但没有。 Layout screenshot

CocktailListFragment

class CocktailListFragment : Fragment() {

    private val viewModel: CocktailListViewModel by lazy {
        ViewModelProvider(this).get(CocktailListViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = CocktailListFragmentBinding.inflate(inflater)
       ...
        setHasOptionsMenu(true)

        return binding.root
    }

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

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        return when (item.itemId) {
            R.id.filter -> {
                Toast.makeText(context, "Filter clicked", Toast.LENGTH_LONG).show()
                true
            } else -> super.onOptionsItemSelected(item)
        }
    }
}

这个问题可以和layout联系起来吗?

filter_list.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="cocktail"
            type="com.example.coctaildb.cocktaillist.CocktailListViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                style="@style/Widget.AppCompat.Toolbar"
                android:background="@android:color/white"
                app:menu="@menu/filter_menu"
                app:title="@string/drinks"
                android:elevation="20dp"
                app:titleTextColor="@android:color/black"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>


        <androidx.recyclerview.widget.RecyclerView
            ... />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是我的菜单文件:

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/filter"
        android:icon="@drawable/ic_filter_icon"
        android:title="@string/filters"
        app:showAsAction="always" />
</menu>

尝试使用 OnMenuItemClickListener 方法处理您的点击事件:

class CocktailListFragment : Fragment() {

    private val viewModel: CocktailListViewModel by lazy {
        ViewModelProvider(this).get(CocktailListViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = CocktailListFragmentBinding.inflate(inflater)
       ...
        binding.root.toolbar.setOnMenuItemClickListener { item ->  
            when (item.itemId) {
                R.id.filter -> {
                    Toast.makeText(context, "Filter clicked", Toast.LENGTH_LONG).show()
                    true
                }
            }

            false
        }

        return binding.root
    }
}