![]() ![]() diffResult.dispatchUpdatesTo(recyclerView. Obviously we want the results main thread.Īfter calculating the diffResult we to the actual list updating here, clearing all the previous posts and adding the newly filtered ones: () (viewModel.filteredPosts)Īfter that, it’s just the matter of updating the RecyclerView. The lower the faster the updates.įinally we send the search event to the ViewModel and tell it to run the search on a thread other than the UI Thread so we don’t freeze the UI if the searching process is heavy or from a network. ![]() We don’t want to overflow our back-end with every single key stroke. debounce() on it to only fire the search if the user has actually stopped typing to see the result. textChanges() method, courtesy of the RxBindings library, to get a stream of our EditText updates and then use. class SimpleRecyclerAdapter(private val context: Context, private val posts: List) : RecyclerView.Adapter() Įxplanation: We use. ![]() data class Post( val id: Int, val title: String, val content: String )Īnd a simple RecyclerView adapter. The dependencies go as follows: implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation "io.reactivex.rxjava2:rxjava:2.1.9" implementation "io.reactivex.rxjava2:rxkotlin:2.2.0" implementation '2:rxbinding-kotlin:2.1.1' implementation ":extensions:1.1.1"īe sure to always use the latest versions of each library.įor the sake of this example I’ll be using the Post object to populate my RecyclerView with. We are going to use RxJava and RxBinding libraries for this example (I assume you are using them in your projects these days) and to sweeten things up we’ll be using the ViewModel package which is now a part of the Android JetPack toolbox.
0 Comments
Leave a Reply. |