Posts

Showing posts with the label ViewModel

Android Architecture Components meets Data Binding : Lifecycle awareness!

Image
Finally Android Data Binding library got updated to play nice with Android Architecture Components library and became life-cycle aware! This means that any changes to the LiveData objects will be reflected in the layouts... automagically! :) * Tested on Android Studio 3.1 - canary 6 Whats new? To make the binding life-cycle aware you need to pass the life-cycle owner to the ViewDataBinding by calling the  setLifecycleOwner method. The ViewModel: Our view model consists of the LiveData object that is simply and observable data container, where the observer gets notified of data changes. The model: Our model has one field that is used to store user's name. A POJO. The layout: -Out layout consists of a text field that is bound to the user's name. And will be updated when our LiveData container posts a change. -We also have an EditText that is two-way bound to the user's name, meaning when text gets entered, the name is updated and vise-versa, when the use...

Android Lists Made Easy: DataBinding + RecyclerViewBindingAdapter Library

Image
RecyclerViewBindingAdapter Library This library provides a powerful yet reusable RecyclerView adapter that leverages ObservableArrayList and DataBinding to simplify your life. Tell the adapter which layouts your models map to and then just modify your observable list and the adapter takes care of the rest. Its that simple! Check out the library and example app source code here . Step 1: Add  jitpack  repository to you project's  build.gradle file Step 2: Enable Android data binding and add library dependency in the module's build.gradle file that you will be using the library in. You will need data binding to create layouts bound to  ViewModel s . The adapter dynamically creates all the rows and manages recycling of the layouts. Step 3: Create  ObservableArrayList  that will hold all the view models  and  initialize EasyRecyclerAdapter with it. Set up the mappings between the layouts you intend to show in the list ...

Android Architecture Components : ViewModel

Image
ViewModel : UI-Related Data Object One of the biggest challenges for an Android developer is keeping data across configuration changes such as screen rotation or during the recreation of Activity / Fragments after its been destroyed and recreated by the framework. Luckily the new Android Architecture Components library has ViewModel class that is intended to store and manage UI-related data so that the data survives configuration changes.  Benefits Keeps data across UI recreation No need to repeat API calls and Database queries on UI recreation  Keeps  Activity / Fragments clean since work is now delegated to ViewModels Share ViewModels between Fragments Subclass ViewModel To create a view-model just subclass ViewModel . View-models are responsible to provide data and keep reference to it. Our TimerViewModel lazily initializes TimerLiveData which is a LiveData implementation of a timer that counts seconds, read more about TimerLiveData a...