java - Are actions allowed in MVVM? Android -
if mvvm data binding , cannot view.dothis()
, otherwise it's mvp, how invoke actions on views?
suppose have view has snackbar
. view
controlled viewmodel
. how viewmodel
supposed show snackbar
without going snackbar.show()
?
in mvvm, viewmodel captures state of view. view observes viewmodel changes , updates itself. thus, communication between view & viewmodel happens through change of values (as against method calls in mvp).
since snackbar
global behaviour (like toast), can implemented @ activity/fragment level. so, can make messagehelper
interface , pass viewmodel dependency. activity implement , display snackbar
.
example:
however, possible there view specific behaviour cannot implemented @ activity level. such cases, can make use of databinding.observable
trigger event. example, lets want animate particular view. can create bindingadapter
@bindingadapter({"shaketrigger"}) public static void showsnackbar(view view, void trigger) { // animation here. add meaningful argument types control animation }
in xml, can apply using
<textview bind:shaketrigger="@{vm.shaketrigger}"/>
then, in viewmodel, can trigger shake using data binding apis. 1 way using baseobservable
can be:
public class configurationviewmodel extends baseobservable implements viewmodel { @bindable public final void shaketrigger = null; public void shake() { notifypropertychanged(br.shaketrigger); } }
if use rxjava, trigger implemented rx.observable
. can checkout library use rxjava data binding. https://github.com/manas-chaudhari/android-mvvm
Comments
Post a Comment