-
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
Step 1: Add the getID() method to your model:
public class EditViewModel implements ViewModel {
public int getID() {
return mID; /* uniqueID of your model */
}
}
Step 2: Create a ViewState for your ItemType:
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);
}
}
Step 3: Create ViewStateProvider for your ViewModel:
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 */
}
}
Step 4: Create ViewBinder for your ViewModel:
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);
}
}
Step 5: Register your ViewBinder:
adapter.registerRenderer(new EditViewBinder());