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.

android, architecture-components, kotlin, lifecycle, lifecycleowner, livedata, recyclerview, viewholder


Lifecycle Cells

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.

100% Kotlin ❤️

Download API License


Buy Me a Coffee at

GitHub Follow Twitter Follow

Table of Content


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.0'

Sample Project

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.

Usage Steps

  1. Make your ViewHolder extend LifecycleViewHolder:

    class MyViewHolder(itemView: View) : LifecycleViewHolder(itemView) {
  2. Make your Adapter extend LifecycleAdapter:

    class MyAdapter : LifecycleAdapter<MyViewHolder>() {
  3. Congratulations! 🙌 Your ViewHolder is now a Lifecycle-Aware Component 😋

NOTE: If you want to use the Paging Library, make your Adapter extend LifecyclePagedListAdapter instead:

class MyPagedListAdapter : LifecyclePagedListAdapter<MyViewHolder>() {

Using a custom Adapter

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) {
		//Notifies LifecycleViewHolder it has been attached

	override fun onViewDetachedFromWindow(holder: MyViewHolder) {
		//Notifies LifecycleViewHolder it has been detached

	override fun onViewRecycled(holder: MyViewHolder) {
		//Notifies LifecycleViewHolder it has been recycled

Observing LiveData

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

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.