https://github.com/zeoflow/view-binding
A lightweight library aiming to speed up Android app development by leveraging the new Android Data Binding together with the Model-View-ViewModel design pattern.
https://github.com/zeoflow/view-binding
android-library data-binding lifecycle view-binding viewmodel zeoflow zeoflow-library
Last synced: 4 months ago
JSON representation
A lightweight library aiming to speed up Android app development by leveraging the new Android Data Binding together with the Model-View-ViewModel design pattern.
- Host: GitHub
- URL: https://github.com/zeoflow/view-binding
- Owner: zeoflow
- License: apache-2.0
- Created: 2020-08-25T09:52:04.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-10T08:59:09.000Z (about 5 years ago)
- Last Synced: 2024-05-20T20:07:22.223Z (almost 2 years ago)
- Topics: android-library, data-binding, lifecycle, view-binding, viewmodel, zeoflow, zeoflow-library
- Language: Java
- Homepage: https://zeoflow.github.io/view-binding/
- Size: 357 KB
- Stars: 13
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: docs/contributing.md
- License: LICENSE
Awesome Lists containing this project
README
# View Binding
## Intro
A lightweight library aiming to speed up Android app development by leveraging the new [Android Data Binding](http://developer.android.com/tools/data-binding/guide.html) and taking the best from the [Model-View-ViewModel](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel) design pattern.
## Getting Started
For information on how to get started with View Binding,
take a look at our [Getting Started](docs/getting-started.md) guide.
## Submitting Bugs or Feature Requests
Bugs or feature requests should be submitted at our [GitHub Issues section](https://github.com/zeoflow/view-binding/issues).
## Why should I use it?
1. **Data Binding**
Android Data Binding is great and if you're not, you should start using it today.
2. **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.
3. **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 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](http://developer.android.com/tools/data-binding/guide.html))
## How does it work?
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.
### 1. Depend on our library
View Binding for Android is available through Google's Maven Repository.
To use it:
1. Open the `build.gradle` file for your application.
2. Make sure that the `repositories` section includes Google's Maven Repository
`google()`. For example:
```groovy
allprojects {
repositories {
google()
jcenter()
}
}
```
3. Add the library to the `dependencies` section:
```groovy
dependencies {
// ...
implementation 'com.zeoflow:view-binding:'
// ...
}
```
### 2. Activity/Fragment Class
`MainActivity.java`
```java
public class MainActivity extends BindAppActivity
{
//..
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
//..
setupViewBinding(R.layout.activity_main, MainViewBinding.class);
//..
super.onCreate(savedInstanceState);
//..
MainViewBinding mMainViewBinding = getViewBinding();
//..
}
//..
}
```
### 3. Activity/Fragment Layout
`activity_main.xml`
```xml
```
### 4. ViewModel
`MainViewModel.java`
```java
public class MainViewBinding extends ViewBinding
{
public final ObservableField 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
}
}
```
## License
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.