Skip to content

Quick start with View Binder

Vitaly Vivchar edited this page Jan 27, 2018 · 12 revisions

Step 1: Add ViewModel interface to your model class

public class YourModel implements ViewModel {
    String getTitle() {...} //your method
    int getTitleColor() {...} //your method
    String getImageBitmap() {...} //your method
}

Step 2: Create your you_model_layout.xml

<LinearLayout ... >
	<TextView android:id = "@+id/title" ... />
	<ImageView android:id = "@+id/image" ... />
	<Button android:id = "@+id/button" ... />
	<CustomView android:id = "@+id/custom" ... />
</LinearLayout>

Step 3: Create your ViewBinder

private ViewRenderer getYourViewBinder() {
    return new ViewBinder<>(
        R.layout.you_model_layout, //your item layout
        YourModel.class, //your model class
        (model, finder, payloads) -> finder
                .setText(R.id.title, model.getTitle())
                .setTextColor(R.id.text, model.getTitleColor())
                .setImageBitmap(R.id.image, model.getImageBitmap())
                .setOnClickListener(R.id.button, new OnClickListener() {...})
                .find(R.id.custom, new ViewProvider<CustomView>() {...})
        );
}

Step 4: Initialize RendererRecyclerViewAdapter and register your ViewBinder

mRecyclerViewAdapter = new RendererRecyclerViewAdapter(getContext());
mRecyclerViewAdapter.registerRenderer(getYourViewBinder());
//regular code