Android data binding adapters for RecyclerView. This library let's you bind RecyclerView items directly from xml layout files while keeping your view models clean and tidy.
Library is distributed via jcenter repository. To include in your project, simply add dependency:
compile 'eu.rampsoftware:recycler-binding:0.1.3'
public class ItemViewModel extends BaseObservable {
private final String mTitle;
public ItemViewModel(String title) {
mTitle = title;
}
@Bindable
public String getTitle() {
return mTitle;
}
}
Contains data that you would like to display on each single item of the list.
public class MainActivityViewModel extends BaseObservable {
private ObservableList<ItemViewModel> mItems = new ObservableArrayList<>();
public MainActivityViewModel() {
initializeModel();
}
public ObservableList<ItemViewModel> getItems() {
return mItems;
}
public int getItemBindingId() {
return BR.model;
}
}
Contains collection of items to be displayed on your list. In addition, you have to provide information with name of your binding variable used in layout item. See below.
<!-- item_layout.xml -->
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="eu.rampsoftware.recyclerbinding.demo.model.ItemViewModel"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.tag, default=Item}"/>
</layout>
<!-- main_layout.xml -->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="eu.rampsoftware.recyclerbinding.demo.model.MainActivityViewModel" />
</data>
<android.support.v7.widget.RecyclerView
android:id="@+id/dashboard_challenge_section_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
bind:itemBindingId="@{model.itemBindingId}"
bind:itemLayout="@{@layout/item_layout}"
bind:items="@{model.items}" />
</layout>
Please, notice the additional bind attributes used in RecyclerView.