All Projects → Sarquella → LifecycleCells

Sarquella / LifecycleCells

Licence: Apache-2.0 License
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.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to LifecycleCells

Elements
⚒ Modular components for RecyclerView development enforcing clean, reusable and testable code, with built-in support for paging and complex hierarchies of data.
Stars: ✭ 75 (+294.74%)
Mutual labels:  recyclerview, livedata, architecture-components
Readhubclient
Readhub客户端
Stars: ✭ 44 (+131.58%)
Mutual labels:  lifecycle, livedata, architecture-components
Simple-Notes-Kotlin-App
✍️ Simple Note Making App use mvvm architecture , dagger , coroutines and navigation component. Features includes 🗒️ create , edit and ❌ delete notes
Stars: ✭ 40 (+110.53%)
Mutual labels:  recyclerview, livedata, architecture-components
Aacomponents
基于google Android Architecture Components 封装实现组件式MVP快速开发框架
Stars: ✭ 66 (+247.37%)
Mutual labels:  lifecycle, livedata, architecture-components
Flexibleadapter
Fast and versatile Adapter for RecyclerView which regroups several features into one library to considerably improve the user experience :-)
Stars: ✭ 3,482 (+18226.32%)
Mutual labels:  recyclerview, viewholder, livedata
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (+1047.37%)
Mutual labels:  lifecycle, livedata, architecture-components
Tdcapp
Sample app which access the TDC (The Developer's Conference) REST API.
Stars: ✭ 55 (+189.47%)
Mutual labels:  lifecycle, livedata, architecture-components
Base Mvvm
App built to showcase basic Android View components like ViewPager, RecyclerView(homogeneous and heterogeneous items), NavigationDrawer, Animated Vector Drawables, Collapsing Toolbar Layout etc. housed in a MVVM architecture
Stars: ✭ 18 (-5.26%)
Mutual labels:  recyclerview, livedata, architecture-components
TeamManagerApp
A sample app structure using the MVVM architecture LiveData, RxJava, ViewModel, Room and the Navigation Arch Components.
Stars: ✭ 36 (+89.47%)
Mutual labels:  lifecycle, livedata, architecture-components
Android-MVVM-News-App
MVVM News Application with clean code architecture & android jetpack components.
Stars: ✭ 38 (+100%)
Mutual labels:  livedata, architecture-components
KTAndroidArchitecture
A Kotlin android architecture with Google Architecture Components
Stars: ✭ 33 (+73.68%)
Mutual labels:  livedata, architecture-components
AndroidCleanArchitecture
Android Project with clean android architecture contain Dagger, Retrofit, Retrofit, Android archtecture components, LiveData with MVVM architecture
Stars: ✭ 22 (+15.79%)
Mutual labels:  livedata, architecture-components
ReactiveLiveData
Transformation functions for LiveData
Stars: ✭ 80 (+321.05%)
Mutual labels:  livedata, architecture-components
Pursuit-Core-Android
Pursuit Core Android
Stars: ✭ 45 (+136.84%)
Mutual labels:  recyclerview, livedata
ParkingDemo
Taipei City Parking Lot Information Query System Demo
Stars: ✭ 18 (-5.26%)
Mutual labels:  recyclerview, lifecycle
LiveData-DataBinding-Kotlin
Sample to practice LiveData + DataBinding
Stars: ✭ 89 (+368.42%)
Mutual labels:  livedata, architecture-components
android-recyclerview-binding
RecyclerView + Data Binding + LiveData Sample
Stars: ✭ 45 (+136.84%)
Mutual labels:  recyclerview, livedata
LiveAdapter
Auto updating RecyclerView Adapter for LiveData
Stars: ✭ 50 (+163.16%)
Mutual labels:  viewholder, livedata
Superhero-App
🦸🏻‍♂️🦹🏻‍♀️Superhero app built with Kotlin, ViewModel, LiveData, ViewBinding, Room, and Hilt
Stars: ✭ 27 (+42.11%)
Mutual labels:  livedata, architecture-components
movies
An example approach for modularization, reactive clean architecture and persistancy.
Stars: ✭ 110 (+478.95%)
Mutual labels:  livedata, architecture-components

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].