我是 Kotlin 编程语言的新手,处理 recyclerView 有点难以理解,所以我使用了 Groupie 库,但是没有足够的文档或教程来说明如何处理这个库。
直截了当我有一个回收器视图包含我添加到适配器的数据,我想使用 searchView 过滤该数据,我不知道如何从 groupAdapter 或项目中获取列表或文本在它们里面执行过滤。
这是我的片段的 XML 布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".fragment.SearchFragment"
android:background="@color/backgroundGray">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/include4"
layout="@layout/fragment_search_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview_search"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="@null"
android:background="@color/backgroundGray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include4"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/search_row"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
seachView XML 布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.SearchView 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="wrap_content"
android:id="@+id/searchView"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
app:queryHint="Looking For Stores.."
app:iconifiedByDefault="false"
android:layoutDirection="rtl"
android:clickable="true"
android:elevation="4dp"
android:gravity="end"
app:queryBackground="@null"
android:textAlignment="textEnd"
tools:ignore="KeyboardInaccessibleWidget,UnusedAttribute"
android:background="@drawable/searchview_background">
</androidx.appcompat.widget.SearchView>
这是我的片段类
class SearchFragment : Fragment() {
lateinit var searchView: android.widget.SearchView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_search, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
searchView = view.findViewById(R.id.searchView)
val adapter = GroupAdapter<GroupieViewHolder>()
recycleview_search.adapter = StoresItems()
adapter.add(Stores(R.drawable.ic_kfc, "KFC", 15.581010, 32.569032))
adapter.add(Stores(R.drawable.ic_pizzahut, "Pizza Hut", 15.581050, 32.569002))
adapter.add(Stores(R.drawable.ic_dominos, "Dominos Pizza", 15.581560, 32.512002))
adapter.setOnItemClickListener { item, view ->
val storesItem = item as Stores
val intent = Intent(view.context, OrderActivity::class.java)
intent.putExtra("storename", storesItem.storename)
startActivity(intent)
}
searchView.setOnCloseListener(object: SearchView.OnQueryTextListener,
SearchView.OnCloseListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
return false
}
})
}
}
class Stores(val img: Int, val storename: String , val latitude: Double, val longitude: Double) : Item<GroupieViewHolder>() {
override fun getLayout(): Int {
return R.layout.search_row
}
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.itemView.storeName.text = storename
viewHolder.itemView.storeImage.setImageResource(img)
}
}