A Presenter is used to generate {@link View}s and bind Objects to them on
demand. It is closely related to concept of an {@link
android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, but is
not position-based.
A trivial Presenter that takes a string and renders it into a {@link
android.widget.TextView TextView}:
public class StringTextViewPresenter extends Presenter {
// This class does not need a custom ViewHolder, since it does not use
// a complex layout.
{@literal @}Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
return new ViewHolder(new TextView(parent.getContext()));
}
{@literal @}Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
String str = (String) item;
TextView textView = (TextView) viewHolder.mView;
textView.setText(item);
}
{@literal @}Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
// Nothing to unbind for TextView, but if this viewHolder had
// allocated bitmaps, they can be released here.
}
}
|