I got it working like this:
Fragment:
class DiAtomicMoleculesFragment : Fragment() {
private lateinit var binding: FragmentDiatomicMoleculesBinding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentDiatomicMoleculesBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerAdapter = DiAtomicMoleculesAdapter(onClickListener)
binding.diRecyclerView.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
adapter = recyclerAdapter
}
val viewModel = ViewModelProvider(this).get(DiAtomicMoleculesViewModel::class.java)
viewModel.getDiAtomicMoleculesByName().observe(viewLifecycleOwner, Observer { items ->
recyclerAdapter.setData(items)
})
binding.viewModel = viewModel
}
}
layout.xml:
<?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">
<data>
<variable
name="viewModel"
type="com.mychemistry.viewmodel.DiAtomicMoleculesViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/di_search_box"
android:layout_width="0dp"
android:layout_height="50dp"
android:gravity="center_vertical"
android:hint="@string/search"
android:paddingStart="10dp"
android:paddingEnd="5dp"
android:singleLine="true"
android:textColor="@android:color/black"
android:textColorHint="@android:color/darker_gray"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onTextChanged="@{(text, start, before, count) -> viewModel.onTextChange(text)}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/di_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/di_search_box" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
视图模型:
class DiAtomicMoleculesViewModel : ViewModel() {
private val allLiveData = AppDatabase.getInstance().getDiAtomicMoleculeDao().getAll()
private val filterLiveData = MutableLiveData<String>()
private val searchByLiveData = Transformations.switchMap(filterLiveData, ::filter)
fun getDiAtomicMolecules(): LiveData<List<DiAtomicMolecule>> {
return allLiveData
}
private fun filter(text: String): LiveData<List<DiAtomicMolecule>> {
return AppDatabase.getInstance().getDiAtomicMoleculeDao().find(text)
}
fun getDiAtomicMoleculesByName(): LiveData<List<DiAtomicMolecule>> {
return searchByLiveData
}
fun onTextChange(e: Editable?) {
filterLiveData.value = e?.toString()
}
fun onTextChange(text: CharSequence?) {
filterLiveData.value = text?.toString()
}
}