I'm new to the Kotlin programing language and Dealing with the recyclerView is kinda hard to understand so I have used Groupie Library, But there is no enough documentation or tutorials on how to deal with this library.
coming straight to the point I have a recycler view contains data that I added to the adapter, and I want to filter that data using the searchView, I don't know how to get the list or the Text out of that groupAdapter or the items that inside of them to perform filter.
here is the XML Layout of my Fragment
<?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 Layout
<?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>
and this is my Fragment Class
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)
}
}