-
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 getID()
method to your model:
public class EditViewModel implements ViewModel {
public int getID() {
return mID; /* uniqueID of your model */
}
}
Step 2: Create ViewState
for your ViewModel
:
import com.github.vivchar.rendererrecyclerviewadapter.ViewHolder;
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);
}
}
Note: if you use CompositeViewBinder
then you should extend CompositeViewState
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
:
private ViewBinder getYourViewBinder() {
return new ViewBinder<>(
R.layout.your_edit_text_item_layout,
EditViewModel.class,
(model, finder, payloads) -> {
/* your binding */
},
new EditViewStateProvider()
);
}
Step 5: Register your ViewBinder
:
adapter.registerRenderer(getEditViewBinder());