Posts

Showing posts from August, 2017

Android Data Binding : Dynamic RecyclerView Adapter

Image
One of the most common list design patterns that I encounter as Android developer is a list with a variation of the following:

Multiple row types. Expandable/Collapsible rows or sections. Load more/Scroll to load more. Delete rows/Sections. 
And all of these can be implemented in a generic manner with Android Data Binding library. You can write one adapter for all the RecycleViews in your app and use it to render all the common design patterns. Check out the source for the example in this tutorial here.

Final result:

Lets begin by defining our adapter:

public class RecyclerViewBindingAdapter extends RecyclerView.Adapter<RecyclerViewBindingAdapter.BindingViewHolder> { private ObservableList<AdapterDataItem> data; public RecyclerViewBindingAdapter(ObservableList<AdapterDataItem> data) { this.data = data; data.addOnListChangedCallback(new ObservableListCallback()); } @Override public BindingViewHolder onCreateViewHolder(ViewGroup par…

Android Data Binding : Tricks and Magic

Image
You can do a lot of awesome things while using the Android Data Binding library. Some will save you a lots of time and some will seem like magic. You can see the full source code for this tutorial here.
Custom XML attributes You can create custom xml attributes and add them to existing UI elements. For example if you ever had to use a custom font files in your application you would always have to set the type face programmatically as the TextView or its sub classes did not have an xml attribute to set the custom type face file from xml. With data binding you can implement your own attributes quick and easy. First lets define our binding adapter. 
public class AttributeBindings { @BindingAdapter({"bind:typeface"}) public static void setTypeFace(TextView view, TypeFaceType tft) { Log.i("AttributeBindings", "setting typeface:"+tft); Typeface typeface = null; switch (tft) { case NORMAL: typeface = …

Android Data Binding : How to Integrate Into Existing App

Image
Android Data Binding Library has been production ready for almost a year and you may be wondering if you can start using this great library in an existing project without introducing more bugs and without having to rewrite a lot of exiting code in your project. Yes you can, and its very easy to start! And why wouldn't you? Why wait to start a new project in few months or years and miss out on all the benefits of this library?! As the library offers significant time savings to developers and enforces good coding practices and as a result making your code cleaner and more maintainable. You can pull the the code for this tutorial here.

It is easy to begin. If your project is minimum API level 7 or higher and is using Android Plugin for Gradle 1.5.0-alpha1 or higher then you can simply enable the Data Binding library by adding the following to the app module's build.gradle file

android { .... dataBinding { enabled = true } }
And that's it! Now you can start …