android-data-binding-command
android-data-binding-command copied to clipboard
Android Data Binding Command
Command design pattern implementation for Android Data Binding
Demo

How to start?
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.radzio:android-data-binding-command:1.0.4'
}
Share this release:
Make sure that you are using Android Data Binding
- In your main build.gradle you have gradle plugin 1.5.0 or newer:
classpath 'com.android.tools.build:gradle:1.5.0'
- In your app build.gradle you've enabled databinding:
dataBinding {
enabled = true
}
Add to your code
Modify your layout, add bind:command="@{viewModel.myCommand}"
<android.support.design.widget.FloatingActionButton
bind:command="@{viewModel.myCommand}"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"/>
Add in your binding class
public ICommand myCommand = new Command()
{
@Override
public void execute()
{
MainActivityViewModel.this.view.showMessage("Hello from Command!");
}
};
Command features
Each command implements ICommand interface
public interface ICommand extends Observable
{
@Bindable
boolean isEnabled();
void isEnabled(boolean isEnabled);
@Bindable
boolean isRefreshing();
void isRefreshing(boolean isRefreshing);
void execute();
}
You can use isEnabled and isRefreshing properties in your java code and xml layouts:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_below="@+id/button"
bind:command="@{viewModel.enableButtonCommand}"
android:text="@{viewModel.buttonCommand.enabled ? `Disable Timer` : `Enable Timer`}"
android:enabled="@{viewModel.buttonCommand.refreshing == false}"
android:layout_centerHorizontal="true"/>