An Android library that provides a Lifecycle to any ViewHolder through the implementation of the LifecycleOwner interface, allowing it to interact with a Lifecycle-Aware Component.
During the Google I/O 2017, a new set of libraries where introduced under the name of Architecture Components. Among those various libraries, one of the most remarkables was the introduction of LiveData, an observable data holder class. In order to be able to observe it, a class implementing the LifecycleOwner interface must be provided.
Google just provided this functionality to Fragments and Activities out of the box since Support Library 26.1.0.
This library pretends to provide ViewHolder with these capabilities, giving to each RecyclerView's item its own Lifecycle and gaining all the advantages of being able to interact with any Lifecycle-Aware Component.
dependencies {
//...
implementation 'dev.sarquella.lifecyclecells:lifecyclecells:1.0.2'
}
A basic sample is given to demonstrate how the provided ViewHolder's Lifecycle can be used to interact with a Lifecycle-Aware Component such as a LiveData instance.
The library is composed by 3 main abstract classes:
-
LifecycleViewHolder: A simple abstract ViewHolder subclass that just implements the LifecycleOwner interface, with the methods to update its LifeCycle when necessary.
-
LifecycleAdapter: A simple abstract Adapter subclass that is in charge of calling the corresponding
LifecycleViewHolder
methods for updating its LifeCycle when needed. -
LifecyclePagedListAdapter: Works the same way as
LifecycleAdapter
, but it subclasses PagedListAdapter in order to be compatible with the Paging Library.
-
Make your ViewHolder extend
LifecycleViewHolder
:class MyViewHolder(itemView: View) : LifecycleViewHolder(itemView) { //... }
-
Make your Adapter extend
LifecycleAdapter
:class MyAdapter : LifecycleAdapter<MyViewHolder>() { //... }
-
Congratulations! 🙌 Your ViewHolder is now a LifecycleOwner 😋
NOTE: If you want to use the Paging Library, make your Adapter extend LifecyclePagedListAdapter
instead:
class MyPagedListAdapter : LifecyclePagedListAdapter<MyListType, MyViewHolder>(DIFF_CALLBACK) {
//...
}
If you are already extending any other custom Adapter and can not make it extend LifecycleAdapter
nor LifecyclePagedListAdapter
, you can still make it compatible with LifecycleViewHolder
by overriding the following Adapter's methods:
- onViewAttachedToWindow
- onViewDetachedFromWindow
- onViewRecycled
Override them as follows:
class MyAdapter : AnyCustomAdapter<MyViewHolder>() {
override fun onViewAttachedToWindow(holder: MyViewHolder) {
super.onViewAttachedToWindow(holder)
//Notifies LifecycleViewHolder it has been attached
holder.onAttached()
}
override fun onViewDetachedFromWindow(holder: MyViewHolder) {
super.onViewDetachedFromWindow(holder)
//Notifies LifecycleViewHolder it has been detached
holder.onDetached()
}
override fun onViewRecycled(holder: MyViewHolder) {
super.onViewRecycled(holder)
//Notifies LifecycleViewHolder it has been recycled
holder.onRecycled()
}
//...
}
There is no doubt that one of the main purposes for providing a Lifecycle to the ViewHolder class is to be able to use it for observing a LiveData instance.
In order to do so, we first need to obtain the LiveData instance. There are several ways of obtaining it and may vary on each situation. However, for most common cases, the recommended way is by obtaining it following the Google's proposed MVVM pattern. This way, the LiveData instance will be provided by the ViewModel.
Please, refer to this StackOverflow question to find out how to use a ViewModel with a ViewHolder.
Finally, just get the LiveData instance through the ViewModel to observe it from the ViewHolder extending from LifecycleViewHolder
:
class MyViewHolder(itemView: View) : LifecycleViewHolder(itemView) {
//...
fun observeLiveData() {
viewModel.liveDataInstance.observe(this, Observer {
//...
})
}
//...
}
Copyright 2019 Adrià Sarquella Farrés
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.