android-mvvm

Best practices and patterns in ViewModel + Data Binding. Is ObservableField in ViewModel OK?

微笑、不失礼 提交于 2019-12-04 09:53:12
Looking through the samples I seen 2 approaches to MVVM using Android Architecture Components. First approach: ViewModel provides LiveData Activity subscribes to LiveData When observer called Activity is setting data to ViewModel ObservableField . Whole ViewModel is passed to binding. In xml you just set ObservableField as value <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" app:visibleGone="@{viewmodel.listLoading}"/> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefresh" android:layout_width="match

How to bind a view model variable with custom class type

ぐ巨炮叔叔 提交于 2019-12-04 06:50:51
问题 What works? XML: name="viewModel" type="com. . . . .MyViewModel" /> ... ... ... <android.support.v7.widget.RecyclerView android:id="@+id/feeds_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" app:items="@{viewModel.feeds}" /> MyViewModel class: private String[] feeds; ... ... public MyViewModel() { String[] feeds = new String[] {"foo", "bar"}; setFeeds(feeds); } @Bindable public String[] getFeeds() { return feeds; }

Using LiveData with Data Binding

风格不统一 提交于 2019-11-29 01:12:33
With the stabilization of Android Architecture Components I started updating all my basic ViewModel s to the new implementation of ViewModel . In my understanding, the usage of LiveData is recommended to hold the Model class since it handles the lifecycle better. I like using Data Binding because it makes the code clearer in Java/Kotlin side and it is not needed to "watch" the value changes to update the UI. However the layout using Data Binding only watch data changes if the Model (or the ViewModel) extends BaseObservable and LiveData does not. I understand the one of the main objectives of

Using LiveData with Data Binding

ⅰ亾dé卋堺 提交于 2019-11-27 15:43:08
问题 With the stabilization of Android Architecture Components I started updating all my basic ViewModel s to the new implementation ofViewModel. In my understanding, the usage of LiveData is recommended to hold the Model class since it handles the lifecycle better. I like using Data Binding because it makes the code clearer in Java/Kotlin side and it is not needed to "watch" the value changes to update the UI. However the layout using Data Binding only watch data changes if the Model (or the