Skip to content

Quick start with View Binder

Vitaly Vivchar edited this page Mar 6, 2020 · 12 revisions

NOTE: start from v3.0.0 ViewBinder was renamed to ViewRenderer

Step 1: Add ViewModel interface to your model class

public class YourModel implements ViewModel {
	String getTitle() {...} /* your method */
	int getTitleColor() {...} /* your method */
	Bitmap getImage() {...} /* your method */
	boolean isImageVisible() {...} /* your method */
	boolean isChecked() {...} /* 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" ... />
	<CheckBox android:id = "@+id/check" ... />
	<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.getImage())
			.setVisible(R.id.image, model.isImageVisible())
			.setOnClickListener(R.id.button, new OnClickListener() {...})
			.setChecked(R.id.check, model.isChecked())
			.find(R.id.custom, new ViewProvider<CustomView>() {...})
	);
}

Step 4: Initialize RendererRecyclerViewAdapter and register your ViewBinder

mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
mRecyclerViewAdapter.registerRenderer(getYourViewBinder());