Posts

Showing posts with the label recycler view

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 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 RecycleView s 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 BindingV...

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: typefa...