Giter VIP home page Giter VIP logo

sarquella / lifecyclecells Goto Github PK

View Code? Open in Web Editor NEW
21.0 1.0 0.0 303 KB

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.

License: Apache License 2.0

Kotlin 100.00%
android kotlin viewholder recyclerview lifecycle lifecycleowner livedata architecture-components

lifecyclecells's Introduction

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 ❤️

Maven Central API License

Follow

Buy Me a Coffee at ko-fi.com

GitHub Follow Twitter Follow

Table of Content

Motivation

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.

Download

dependencies {
    //...
    implementation 'dev.sarquella.lifecyclecells:lifecyclecells:1.0.2'
}

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.

Usage

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 LifecycleOwner 😋


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

class MyPagedListAdapter : LifecyclePagedListAdapter<MyListType, MyViewHolder>(DIFF_CALLBACK) {
	//...
}

Using a custom Adapter (Less Recommended)

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()
	}
    
	//...
}

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 {
	      //...
	    })
	}
	//...
} 

License

LICENSE

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.

lifecyclecells's People

Contributors

sarquella avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.