Skip to content

Quick start with Simple Items

Vitaly Vivchar edited this page Dec 27, 2017 · 14 revisions

To create a Simple Item item you should:

1. Implement the ItemModel interface

public class SimpleModel implements ItemModel {

	public static final int TYPE = 12376;
	private String mName;

	public CategoryModel(String name) {
		mName = name;
	}

	@Override
	public int getType() {
		return TYPE;
	}

	public String getName() {
		return mName;
	}
}

2. Extend the ViewHolder class

public class SimpleViewHolder extends ViewHolder {

	public TextView mName;

	public SimpleViewHolder(View itemView) {
		super(itemView);
		mName = (TextView) itemView.findViewById(R.id.name);
	}
}

3. Extend the ViewRenderer class

public class SimpleViewRenderer extends ViewRenderer<SimpleModel, SimpleViewHolder> {

	public CategoryViewRenderer(int type, Context context) {
		super(type, context);
	}

	@Override
	public void bindView(SimpleModel model, SimpleViewHolder holder) {
		holder.mName.setText(model.getName());
	}

	@Override
	public SimpleViewHolder createViewHolder(ViewGroup parent) {
		return new SimpleViewHolder(inflate(R.layout.simple_item, parent));
	}
}

4. Create an instance of your ViewRenderer

ViewRenderer simpleRenderer = new SimpleViewRenderer(this);

5. Register your ViewRenderer in main adapter

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

Also you can register other your items:

mRecyclerViewAdapter.registerRenderer(simpleRenderer);
mRecyclerViewAdapter.registerRenderer(simpleRenderer2);
mRecyclerViewAdapter.registerRenderer(simpleRenderer3);
...