Skip to content
Davide Steduto edited this page Jun 25, 2016 · 22 revisions

###In this page

  • Create/Binding ViewHolders
    • Method A (new!)
    • Method B (classic)

###Create/Binding ViewHolders ####Method A (new!) Via Item interfaces. In this case you don't need to implement getItemViewType(), onCreateViewHolder() and onBindViewHolder() adapter methods, but getLayoutRes(), createViewHolder() and bindViewHolder() from inside the implementation of item interfaces (for the item type we need an int, the layoutResID is sufficient) .

Adopting item interfaces, it gives several benefits such as: Expandable items, runtime flags to enable/disable drag and swipe, to enable/disable the item itself and to allow/disallow the selection.

@Override
public int getLayoutRes() {
	return R.layout.recycler_expandable_row;
}

@Override
public ViewHolder createViewHolder(FlexibleAdapter adapter, LayoutInflater inflater, ViewGroup parent) {
	return new ViewHolder(inflater.inflate(getLayoutRes(), parent, false), adapter);
}

@Override
public void bindViewHolder(final FlexibleAdapter adapter, ViewHolder holder, int position, List payloads) {
	//Bind your VH
}

The ViewHolder class is an inner static class of the same item interface.

####Method B (classic) You override and implement getItemViewType(), onCreateViewHolder() and onBindViewHolder() adapter methods as usual.

Note: If you don't want to adopt item interfaces, you have to override the 3 Adapter methods, otherwise the Adapter enters in auto-mapping mode.

For multiple view types you will need to add a switch statement in both methods and cast the specific item where necessary.

ViewHolders are as well defined all in the same adapter class.

To be continued...

Clone this wiki locally