我有一个片段想要在其中添加SearchView。我已经在菜单中找到了它,它的可单击内容以及所有内容,但是我发现要实际搜索的所有指南要么已经过时,要么就不是我真正需要的。
这是我的片段
class ViewFragment : Fragment () {
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: ViewFragmentBinding = DataBindingUtil.inflate(
inflater, R.layout.view_fragment, container, false)
val application = requireNotNull(this.activity).application
setHasOptionsMenu(true)
binding.setLifecycleOwner(this)
viewFragmentViewModel.navigateToEdit.observe(this, Observer {translation ->
translation?.let {
this.findNavController().navigate(
ViewFragmentDirections
.actionViewFragmentToEditFragment(translation)
)
viewFragmentViewModel.onEditNavigated()
}
})
val adapter = TranslationAdapter(TranslationListener { translationID ->
viewFragmentViewModel.onTranslationClicked(translationID)
})
binding.translationList.adapter = adapter
viewFragmentViewModel.translations.observe(viewLifecycleOwner, Observer {
it?.let {
adapter.submitList(it)
}
})
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater){
inflater.inflate(R.menu.search_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
这是我的搜索菜单
<menu 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.widget.SearchView"
android:title="Search"/>
</menu>
谁能告诉我如何设置所有内容,以便可以访问用户输入的搜索字符串并将新列表提交到RecycleView?或者只是指向正确的方向!
提前谢谢!
编辑:此处更新。我有一个MutableLiveData,在其中放置查询并对其进行观察,以便可以对其观察功能进行操作。我要更新的ViewModel中有一个变量translationList。
class ViewFragment : Fragment (), SearchView.OnQueryTextListener {
private var searchText = MutableLiveData<String>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
val adapter = TranslationAdapter(TranslationListener { translationID ->
viewFragmentViewModel.onTranslationClicked(translationID)
})
binding.translationList.adapter = adapter
viewFragmentViewModel.translations.observe(viewLifecycleOwner, Observer {
it?.let {
adapter.submitList(it)
}
})
searchText.observe(this, Observer {searchString ->
searchString?.let {
if(searchString == ""){
adapter.submitList(viewFragmentViewModel.translations.value)
}
else{
adapter.submitList(viewFragmentViewModel.searchTranslations(searchString))
}
}
})
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater){
inflater.inflate(R.menu.search_menu, menu)
val menuItem = menu.findItem(R.id.action_search)
val searchView = menuItem.actionView as SearchView
searchView.setOnQueryTextListener(this)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onQueryTextChange(newText: String?): Boolean {
searchText.value = newText
return true
}
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
}
对于结束语,由于我已经有了一个列表,其中包含了我要搜索的数据库中的所有对象,因此使用过滤器可以更轻松地遍历该列表。
答案 0 :(得分:0)
步骤-1:在您的SearchView.OnQueryTextListener
中实现ViewFragment
class ViewFragment : Fragment(), SearchView.OnQueryTextListener {
...
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
//Do your operation here
return true
}
}
步骤-2:从菜单中获取SearchView
并使用监听器进行初始化
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.search_menu, menu)
val meniItem = menu?.findItem(R.id.action_search)
val searchView = meniItem?.actionView as SearchView?
searchView?.setOnQueryTextListener(this)
super.onCreateOptionsMenu(menu, inflater)
}
步骤-3:在onQueryTextChange
内进行搜索查询并进行操作
更新:如下更改实施方式:
fun searchTranslations(searchString: String) : List<Translation>? {
return database.getTranslationsBySearch(searchString).value
}
还有道
@Query("SELECT * FROM word_translation_table WHERE word_ger LIKE '% :string %' OR word_es LIKE '% :string %' ")
fun getTranslationsBySearch(string: String): LiveData<List<Translation>>