Skip to content

Quick start with View Binder

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

To create a Simple Item item you should:

1. Implement the ViewModel interface

public class SimpleModel implements ItemModel {
    //your methods
}

2. Create your xml item layout

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

3. Initialize Adapter and register the ViewBinder

mRecyclerViewAdapter = new RendererRecyclerViewAdapter(getContext());

mRecyclerViewAdapter.registerRenderer(new ViewBinder<>(
	R.layout.item_layout,
	SomeModel.class,
	(model, finder, payloads) -> finder
		.find(R.id.custom_view, (ViewProvider<CustomView>) customView -> { ... })
		.setOnClickListener(R.id.button, v -> { ... })
                .setText(R.id.textView, model.getText())
                .setTextColor(R.id.textView, model.getColor())
                .setImageBitmap(R.id.image, model.getBitmap())
));
mRecyclerViewAdapter.registerRenderer(...); /* you can use several types of cells */

/* regular code
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mRecyclerViewAdapter);

mRecyclerViewAdapter.setItems(getItems());
mRecyclerViewAdapter.notifyDataSetChanged();
...
*/