在 ActionBar 和片段中使用 SearchView 小部件

Use the SearchView widget in ActionBar and a fragment

我正在尝试使用 SearchView 小部件来过滤来自 RecyclerView 的数据。 在 activity 中,它就像一个魅力。 但是,在一个片段中,回调不会被触发。

这是我的片段:

class CowListFragment : Fragment(), SearchView.OnQueryTextListener {

    private val adapter = CowAdapter()

    private val args: CowListFragmentArgs by navArgs()

    private val viewModel: CowListViewModel by viewModels {
        InjectorUtils.provideCowViewModelFactory(this, args.livestockId)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = FragmentCowListBinding.inflate(inflater, container, false)
        context ?: return binding.root

        binding.cowRecyclerView.adapter = adapter
        subscribeUi(adapter)
        setHasOptionsMenu(true)

        return binding.root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_cow_list, menu)
        val searchItem = menu.findItem(R.id.search_cow)
        val searchView = searchItem.actionView as SearchView
        searchView.setOnQueryTextListener(this)
    }

    private fun subscribeUi(adapter: CowAdapter) {
        viewModel.cows.observe(viewLifecycleOwner) { cows ->
            adapter.submitList(cows)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.search_cow -> {
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onQueryTextSubmit(query: String?): Boolean {
        query?.let { viewModel.filter(it) }
        return true
    }

    override fun onQueryTextChange(query: String?): Boolean {
        query?.let { viewModel.filter(it) }
        return true
    }
}

这是我的片段布局:

<?xml version="1.0" encoding="utf-8"?>

<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">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/Theme.Bcs.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:contentInsetStartWithNavigation="0dp"
                app:layout_collapseMode="pin"
                app:menu="@menu/menu_cow_list"
                app:navigationIcon="@drawable/ic_detail_back"
                app:titleTextColor="?attr/colorOnSurface" />

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/cow_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingStart="@dimen/card_side_margin"
            android:paddingTop="@dimen/header_margin"
            android:paddingEnd="@dimen/card_side_margin"
            app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
            app:spanCount="@integer/grid_columns"
            tools:context=".MainActivity"
            tools:listitem="@layout/list_item_cow" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</layout>

还有菜单:

<?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/search_cow"
        android:icon="@drawable/ic_search"
        android:title="@string/search_title"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />

</menu>

有趣的事情:如果我在 SearchView class 中断点构造函数,我会看到创建了多个实例。看来我添加监听器的 SearchView 不是正确的。

您可以在此处获取项目的完整代码: Github linkhttps://github.com/Christian-Adventiel/body-condition-score-mobile

任何帮助将不胜感激。 谢谢!

在 onCreateView 函数中添加这个解决了问题:

(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)