-
Notifications
You must be signed in to change notification settings - Fork 184
Quick start with ViewState
Vitaly Vivchar edited this page Feb 17, 2018
·
13 revisions
public class EditViewModel implements ViewModel {
public int getID() {
return mID; /* uniqueID of your model */
}
}
public class EditViewState implements ViewState<ViewHolder> {
private final String mEnteredText;
public EditViewState(ViewHolder holder) {
EditText editText = holder.getViewFinder().find(R.id.your_edit_text);
mEnteredText = editText.getText().toString();
}
@Override
public void restore(@NonNull final ViewHolder holder) {
holder.getViewFinder().setText(R.id.your_edit_text, mEnteredText);
}
}
public class EditViewStateProvider implements ViewStateProvider<EditViewModel, ViewHolder> {
@Override
public ViewState createViewState(@NonNull final ViewHolder holder) {
return new EditViewState(holder);
}
@Override
public int createViewStateID(@NonNull final EditViewModel model) {
return model.getID(); /* using the ID of ViewModel as a ID of ViewState */
}
}
public class EditViewBinder extends ViewBinder<EditViewModel> {
public EditViewBinder() {
this(
R.layout.your_edit_text_item_layout,
EditViewModel.class,
(Binder<EditViewModel>) (model, finder, payloads) -> {
/* your binding */
},
new EditViewStateProvider()
);
}
public EditViewBinder(final int layoutID,
@NonNull final Class<EditViewModel> type,
@NonNull final Binder<EditViewModel> binder,
@Nullable final ViewStateProvider<EditViewModel, ViewHolder> viewStateProvider) {
super(layoutID, type, binder, viewStateProvider);
}
}
adapter.registerRenderer(new EditViewBinder());