Last active
February 13, 2019 16:47
-
-
Save AlonsoFloo/aaa6ddc1760fe59fa7262324e61931ad to your computer and use it in GitHub Desktop.
FastAdapter MVVM generic class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AbstractBindableItem<Item extends AbstractBindableItem & IItem & IClickable, VH extends FastBindableViewHolder<Item, Binding>, VM extends AndroidViewModel, Binding extends ViewDataBinding> extends AbstractItem<Item, VH> { | |
@NotNull | |
private VM viewModel; | |
public AbstractBindableItem(@NotNull VM viewModel) { | |
super(); | |
this.viewModel = viewModel; | |
} | |
@NotNull | |
public VM getViewModel() { | |
return viewModel; | |
} | |
@Nullable | |
public abstract Binding getBinding(@NotNull View itemView); | |
public abstract void dataBind(@NotNull Binding binding); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleFormItem extends AbstractBindableItem<ExampleFormItem, FastBindableViewHolder<ExampleFormItem, LayoutItemExampleFormBinding>, ExampleFormViewModel, LayoutItemExampleFormBinding> { | |
public ExampleFormItem(@NotNull ExampleFormViewModel viewModel) { | |
super(viewModel); | |
} | |
@Nullable | |
@Override | |
public LayoutItemExampleFormBinding getBinding(@NotNull View itemView) { | |
return DataBindingUtil.bind(itemView); | |
} | |
@Override | |
public void dataBind(@NotNull LayoutItemExampleFormBinding binding) { | |
binding.setViewModel(getViewModel()); | |
} | |
@NonNull | |
@Override | |
public FastBindableViewHolder<ExampleFormItem, LayoutItemExampleFormBinding> getViewHolder(View v) { | |
return new FastBindableViewHolder<>(v,this); | |
} | |
@Override | |
public int getType() { | |
return R.id.example_form_id; | |
} | |
@Override | |
public int getLayoutRes() { | |
return R.layout.layout_item_example_form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FastBindableViewHolder<Item extends AbstractBindableItem & IItem & IClickable, Binding extends ViewDataBinding> extends FastAdapter.ViewHolder<Item> { | |
@Nullable | |
private Binding binding; | |
public FastBindableViewHolder(@NotNull View itemView, @NotNull Item item) { | |
super(itemView); | |
binding = (Binding) item.getBinding(itemView); | |
} | |
@Override | |
public void bindView(@NotNull Item item, @NotNull List<Object> payloads) { | |
if (binding != null) { | |
item.dataBind(binding); | |
binding.executePendingBindings(); | |
} | |
} | |
@Override | |
public void unbindView(@NotNull Item item) { | |
if (binding != null) { | |
binding.unbind(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment