我正在尝试使用SearchView小部件来过滤来自RecyclerView的数据。在一项活动中,它就像一种魅力。但是,在片段中,回调不会被触发。
这是我的片段:
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类中中断构造函数,我会看到创建了多个实例。我添加侦听器的SearchView似乎不是正确的。
您可以在此处获取项目的完整代码:Githublinkhttps://github.com/Christian-Adventiel/body-condition-score-mobile
如有任何帮助,我们将不胜感激。谢谢
在onCreateView函数中添加此项解决了问题:
(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)