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.
For information on how to get started with View Binding, take a look at our Getting Started guide.
Bugs or feature requests should be submitted at our GitHub Issues section.
- Data Binding Android Data Binding is great and if you're not, you should start using it today.
- You don't need to care about screen rotation (configuration change) at all.
Most of the screen lifecycle is moved to ViewModel where the lifecycle is dramatically easier to understand and to use. The ViewModel instance outlives it's Activity/Fragment during configuration change so no more hassle with
onSaveInstanceState()
or using retained Fragments. - ViewModel as the only variable in the layout
ViewModel serves as the data provider in layout's binding as well as handler for click or other methods common fro Data Binding. With a construct like
android:onClick="@{viewBinding.onClickedPlayButton}"
you will never have to set anOnClickListener
anymore. Also, each ViewModel extendsBaseObservable
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.
View Binding for Android is available through Google's Maven Repository. To use it:
-
Open the
build.gradle
file for your application. -
Make sure that the
repositories
section includes Google's Maven Repositorygoogle()
. For example:allprojects { repositories { google() jcenter() } }
-
Add the library to the
dependencies
section:dependencies { // ... implementation 'com.zeoflow:view-binding:<version>' // ... }
MainActivity.java
public class MainActivity extends BindAppActivity<ActivityMainBinding, MainViewBinding>
{
//..
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
//..
setupViewBinding(R.layout.activity_main, MainViewBinding.class);
//..
super.onCreate(savedInstanceState);
//..
MainViewBinding mMainViewBinding = getViewBinding();
//..
}
//..
}
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="viewBinding"
type="com.zeoflow.view.binding.sample.MainViewBinding" />
</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="@={viewBinding.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(viewBinding.name)}"
app:show="@{viewBinding.name != null && !viewBinding.name.empty}"
tools:text="@string/hello" />
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="@{() -> viewBinding.showDialog()}"
android:text="@string/button_dialog_fragment"
style="@style/Widget.AppCompat.Button.Colored" />
</LinearLayout>
</layout>
MainViewModel.java
public class MainViewBinding extends ViewBinding
{
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
}
}
Copyright 2020 ZeoFlow
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.