A lightweight library aiming to speed up Android app development by leveraging the new Android Data Binding and taking the best from the Model-View-ViewModel design pattern.
onSaveInstanceState()
or using retained Fragments.android:onClick="@{viewModel.onClickedPlayButton}"
you will never have to set an OnClickListener
anymore. Also, each ViewModel extends BaseObservable
so you have a choice between using BaseObservable approach or ObservableField approach within the DataBinding. (see Data Binding Guide)The framework extensively uses Java Generics to provide a type-safe link between Activity/Fragment and ViewModel and its binding.
ViewModel instances are stored in a global static Map and reattached automatically to corresponding Activity/Fragment. When there is no need for the ViewModel anymore (Activity finished) the instance is destroyed.
compile 'cz.kinst.jakub:viewmodelbinding:2.0.0'
Don't forget to enable Data Binding in your module:
android {
dataBinding.enabled = true
}
MainActivity.java
public class MainActivity extends ViewModelActivity<ActivityMainBinding, MainViewModel> {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setupViewModel(R.layout.activity_main, MainViewModel.class);
super.onCreate(savedInstanceState);
}
// handle Activity related stuff here - Options menu, Toolbar, Window config, etc.
}
activity_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="cz.kinst.jakub.sample.viewmodelbinding.MainViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_padding"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={viewModel.name}"
android:inputType="textPersonName|textCapWords"
android:hint="@string/hint_enter_your_name" />
</android.support.design.widget.TextInputLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:animateLayoutChanges="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
android:textColor="@color/colorPrimary"
android:text="@{@string/hello(viewModel.name)}"
app:show="@{viewModel.name != null && !viewModel.name.empty}"
tools:text="@string/hello" />
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="@{() -> viewModel.showDialog()}"
android:text="@string/button_dialog_fragment"
style="@style/Widget.AppCompat.Button.Colored" />
</LinearLayout>
</layout>
MainViewModel.java
public class MainViewModel extends ViewModel {
public final ObservableField<String> name = new ObservableField<>();
@Override
public void onViewModelCreated() {
super.onViewModelCreated();
// Do API calls etc.
}
@Override
public void onViewAttached(boolean firstAttachment) {
super.onViewAttached(firstAttachment);
// manipulate with the view
}
}
To deploy new screens even faster, use the included Android Studio Template (revision 2
)
Copy the template folder to Android Studio templates folder (/Applications/Android Studio.app/Contents/plugins/android/lib/templates/
on Mac) OR run the following command to download and install the template automatically
curl -o viewmodelbinding.zip -Lk https://github.com/jakubkinst/Android-ViewModelBinding/archive/master.zip && unzip viewmodelbinding.zip && cp -af Android-ViewModelBinding-master/extras/AndroidStudioTemplate/templates/. "/Applications/Android Studio.app/Contents/plugins/android/lib/templates/" && rm -r Android-ViewModelBinding-master && rm viewmodelbinding.zip
File>New>ViewModelBinding>ViewModelBinding Screen
action to add a new screengetApplicationContext()
which returns Context at all times (even if View is not attached at the moment)app:show
, app:hide
, app:invisible
)setupViewModel()
before super.onCreate()
)onViewModelInitialized()
callback to Activity/Fragment to be able to setup ViewModel before onViewModelCreated()
is called (example: feeding ViewModel with Extras/Arguments - see ArgumentDialogFragment
in sample)view
and appropriate type)startActivityForResult()
runOnUiThread()
are performed after onViewAttached()
method is called when there are some left in the queuerunOnUiThread()
- if the ViewModel is not attached to an Activity/Fragment at the time, the Runnable will be executed once it is attached againgetString()
method taking formatting arguments to ViewModelRetrofitCallViewModel
extension for handling Retrofit calls (see the source)isRunning()
method to ViewModel telling if Activity/Fragment is in RUNNING state (in between onResume()
and onPause()
)runOnUiThread()
, postDelayed()
and getRootView()
methods to ViewModelViewModelDialogFragment
getBinding()
is now public in ViewModelBR.viewModel
as long as the name ov the binding variable is viewModel
onViewModelCreated()
callback in ViewModelonModelRemoved()
onViewModelDestroyed()
callback in ViewModelgetResources()
convenience method to ViewModelThe library was inspired by a great AndroidViewModel library by Inloop
Copyright 2015 Jakub Kinst & Stepan Sanda
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.