如何在片段(Kotlin)中使用searchView小部件



我正在尝试在片段中使用searchView小部件。我找不到实现它的方法。使用Activity实现searchView更容易,但我不知道如何在Kotlin的片段中实现它。我试过这种方法,但似乎不起作用(只在活动中起作用(。我是安卓系统的初学者,非常感谢这里的帮助,我已经挣扎了几天了。

class Homepage : Fragment() {
private var layoutManager : RecyclerView.LayoutManager? = null
private var _binding : FragmentHomepageBinding? = null
private val binding  get() = _binding!!
private val shoe = DataSource.shoes
val arrayList = ArrayList<Shoe>()
val displayList = ArrayList<Shoe>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentHomepageBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val myAdapter = ShoeAdapter(displayList)
layoutManager = GridLayoutManager(this.context,2)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = myAdapter
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.main_menu,menu)
val menuItem = menu.findItem(R.id.search)
val searchView = menuItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
if(newText!!.isNotEmpty()){
displayList.clear()
val search = newText.lowercase(Locale.getDefault())
arrayList.forEach {
if(it.name.lowercase(Locale.getDefault()).contains(search)){
displayList.add(it)
}
}
recyclerView.adapter!!.notifyDataSetChanged()
}
else{
displayList.clear()
displayList.addAll(arrayList)
recyclerView.adapter!!.notifyDataSetChanged()
}
return true
}
})

super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return super.onOptionsItemSelected(item)
}

}```

我以前使用过它,它工作正常。

class BooksFragment : Fragment(), SearchView.OnQueryTextListener {
private lateinit var binding: FragmentBooksBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentBooksBinding.inflate(inflater, container, false)
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_books, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.toolbar_menu,menu)
val item = menu.findItem(R.id.search_book)
val searchView = item.actionView as SearchView
searchView.setOnQueryTextListener(this)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onQueryTextSubmit(query: String): Boolean {
// search button submit
return true
}
override fun onQueryTextChange(newText: String): Boolean {
// search text changed
return true
}

相关内容

  • 没有找到相关文章

最新更新