├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── drawables.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ ├── drawable │ │ │ │ └── side_nav_bar.xml │ │ │ ├── values-v21 │ │ │ │ └── styles.xml │ │ │ ├── drawable-v21 │ │ │ │ ├── ic_menu_send.xml │ │ │ │ ├── ic_menu_slideshow.xml │ │ │ │ ├── ic_menu_gallery.xml │ │ │ │ ├── ic_menu_manage.xml │ │ │ │ ├── ic_menu_camera.xml │ │ │ │ └── ic_menu_share.xml │ │ │ ├── menu │ │ │ │ ├── activity_main_drawer.xml │ │ │ │ └── main.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── activity_two_way.xml │ │ │ │ ├── layout_two_way_databinding.xml │ │ │ │ ├── nav_header.xml │ │ │ │ ├── layout_event_updates.xml │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── net │ │ │ │ └── droidlabs │ │ │ │ └── deepdiveintoandroiddatabinding │ │ │ │ ├── services │ │ │ │ ├── ISendService.java │ │ │ │ ├── SendService.java │ │ │ │ └── Navigator.java │ │ │ │ ├── viewmodel │ │ │ │ ├── TwoWayViewModel.java │ │ │ │ └── MainViewModel.java │ │ │ │ ├── di │ │ │ │ ├── AppModule.java │ │ │ │ └── DiComponent.java │ │ │ │ ├── App.java │ │ │ │ ├── bindings │ │ │ │ ├── NavigationViewBindings.java │ │ │ │ ├── ColorPickerViewBindings.java │ │ │ │ └── EditTextBindings.java │ │ │ │ └── view │ │ │ │ ├── TwoWayBindingActivity.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── androidTest │ │ └── java │ │ │ └── net │ │ │ └── droidlabs │ │ │ └── deepdiveintoandroiddatabinding │ │ │ └── ApplicationTest.java │ └── test │ │ └── java │ │ └── net │ │ └── droidlabs │ │ └── deepdiveintoandroiddatabinding │ │ └── test │ │ └── MainViewModelTests.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/radzio/DeepDiveIntoAndroidDataBinding/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/radzio/DeepDiveIntoAndroidDataBinding/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/radzio/DeepDiveIntoAndroidDataBinding/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/radzio/DeepDiveIntoAndroidDataBinding/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/radzio/DeepDiveIntoAndroidDataBinding/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/radzio/DeepDiveIntoAndroidDataBinding/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/services/ISendService.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.services; 2 | 3 | public interface ISendService 4 | { 5 | void send(); 6 | 7 | void send(String text); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/side_nav_bar.xml: -------------------------------------------------------------------------------- 1 | 3 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ic_menu_send.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/menu/activity_main_drawer.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 160dp 5 | 6 | 16dp 7 | 16dp 8 | 16dp 9 | 10 | -------------------------------------------------------------------------------- /app/src/androidTest/java/net/droidlabs/deepdiveintoandroiddatabinding/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase 10 | { 11 | public ApplicationTest() 12 | { 13 | super(Application.class); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ic_menu_slideshow.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ic_menu_gallery.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ic_menu_manage.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/drawables.xml: -------------------------------------------------------------------------------- 1 | 2 | @android:drawable/ic_menu_camera 3 | @android:drawable/ic_menu_gallery 4 | @android:drawable/ic_menu_slideshow 5 | @android:drawable/ic_menu_manage 6 | @android:drawable/ic_menu_share 7 | @android:drawable/ic_menu_send 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/viewmodel/TwoWayViewModel.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.viewmodel; 2 | 3 | import android.databinding.BaseObservable; 4 | import android.databinding.Bindable; 5 | import net.droidlabs.deepdiveintoandroiddatabinding.BR; 6 | 7 | public class TwoWayViewModel extends BaseObservable 8 | { 9 | private int color; 10 | 11 | public void setColor(int color) 12 | { 13 | this.color = color; 14 | this.notifyPropertyChanged(BR.color); 15 | } 16 | 17 | @Bindable 18 | public int getColor() 19 | { 20 | return this.color; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ic_menu_camera.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/di/AppModule.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.di; 2 | 3 | import android.content.Context; 4 | import dagger.Module; 5 | import dagger.Provides; 6 | import net.droidlabs.deepdiveintoandroiddatabinding.services.ISendService; 7 | import net.droidlabs.deepdiveintoandroiddatabinding.services.SendService; 8 | 9 | @Module 10 | public class AppModule 11 | { 12 | private Context context; 13 | 14 | public AppModule(Context context) 15 | { 16 | this.context = context; 17 | } 18 | 19 | @Provides 20 | public ISendService provideSendService() 21 | { 22 | return new SendService(this.context); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Tools\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/ic_menu_share.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/services/SendService.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.services; 2 | 3 | import android.content.Context; 4 | import android.widget.Toast; 5 | 6 | public class SendService implements ISendService 7 | { 8 | private final Context context; 9 | 10 | public SendService(Context context) 11 | { 12 | this.context = context; 13 | } 14 | 15 | @Override 16 | public void send() 17 | { 18 | Toast.makeText(this.context, "Message was sent!", Toast.LENGTH_SHORT).show(); 19 | } 20 | 21 | @Override 22 | public void send(String text) 23 | { 24 | Toast.makeText(this.context, String.format("Message '%s' was sent!", text), Toast.LENGTH_SHORT).show(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android Data Binding Demo 3 | 4 | Open navigation drawer 5 | Close navigation drawer 6 | 7 | Settings 8 | Show more! 9 | Gratulations! 10 | Event Updates 11 | Two Way Data Binding 12 | Repeated Expression #1 13 | Repeated Expression #2 14 | Set defined color 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/App.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import net.droidlabs.deepdiveintoandroiddatabinding.di.DiComponent; 6 | 7 | public class App extends Application 8 | { 9 | private DiComponent component; 10 | 11 | @Override 12 | public void onCreate() 13 | { 14 | super.onCreate(); 15 | this.buildComponentAndInject(); 16 | } 17 | 18 | public static DiComponent component(Context context) 19 | { 20 | return ((App) context.getApplicationContext()).component; 21 | } 22 | 23 | private void buildComponentAndInject() 24 | { 25 | this.component = DiComponent.Initializer.init(this); 26 | this.component.inject(this); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/services/Navigator.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.services; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import net.droidlabs.deepdiveintoandroiddatabinding.R; 6 | import net.droidlabs.deepdiveintoandroiddatabinding.view.TwoWayBindingActivity; 7 | 8 | public class Navigator 9 | { 10 | private final Context context; 11 | 12 | public Navigator(Context context) 13 | { 14 | this.context = context; 15 | } 16 | 17 | public boolean onItemSelected(int id) 18 | { 19 | if(id == R.id.nav_custom_to_way_databinging) 20 | { 21 | this.context.startActivity(new Intent(this.context, TwoWayBindingActivity.class)); 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/di/DiComponent.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.di; 2 | 3 | import dagger.Component; 4 | import net.droidlabs.deepdiveintoandroiddatabinding.App; 5 | import net.droidlabs.deepdiveintoandroiddatabinding.view.MainActivity; 6 | 7 | import javax.inject.Singleton; 8 | 9 | @Singleton 10 | @Component(modules = { 11 | AppModule.class}) 12 | public interface DiComponent 13 | { 14 | void inject(App app); 15 | 16 | void inject(MainActivity mainActivity); 17 | 18 | final class Initializer 19 | { 20 | private Initializer() 21 | { 22 | 23 | } 24 | 25 | public static DiComponent init(App app) 26 | { 27 | return DaggerDiComponent.builder() 28 | .appModule(new AppModule(app)) 29 | .build(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/bindings/NavigationViewBindings.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.bindings; 2 | 3 | import android.databinding.BindingAdapter; 4 | import android.support.design.widget.NavigationView; 5 | import android.view.MenuItem; 6 | import net.droidlabs.deepdiveintoandroiddatabinding.services.Navigator; 7 | 8 | public class NavigationViewBindings 9 | { 10 | @BindingAdapter({"navigator"}) 11 | public static void bindNavigator(final NavigationView navigationView, final Navigator navigator) 12 | { 13 | navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() 14 | { 15 | @Override 16 | public boolean onNavigationItemSelected(MenuItem item) 17 | { 18 | return navigator.onItemSelected(item.getItemId()); 19 | } 20 | }); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/view/TwoWayBindingActivity.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.view; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import net.droidlabs.deepdiveintoandroiddatabinding.R; 8 | import net.droidlabs.deepdiveintoandroiddatabinding.databinding.ActivityTwoWayBinding; 9 | import net.droidlabs.deepdiveintoandroiddatabinding.viewmodel.TwoWayViewModel; 10 | 11 | public class TwoWayBindingActivity extends AppCompatActivity 12 | { 13 | private ActivityTwoWayBinding binding; 14 | 15 | @Override 16 | protected void onCreate(@Nullable Bundle savedInstanceState) 17 | { 18 | super.onCreate(savedInstanceState); 19 | this.binding = DataBindingUtil.setContentView(this, R.layout.activity_two_way); 20 | this.binding.setViewModel(new TwoWayViewModel()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/test/java/net/droidlabs/deepdiveintoandroiddatabinding/test/MainViewModelTests.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.test; 2 | 3 | import net.droidlabs.deepdiveintoandroiddatabinding.services.ISendService; 4 | import net.droidlabs.deepdiveintoandroiddatabinding.viewmodel.MainViewModel; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.mockito.Mock; 8 | import org.mockito.runners.MockitoJUnitRunner; 9 | 10 | import static org.mockito.Mockito.verify; 11 | 12 | @RunWith(MockitoJUnitRunner.class) 13 | public class MainViewModelTests 14 | { 15 | @Mock 16 | ISendService sendService; 17 | 18 | @Test 19 | public void mainViewModel_sendAction_sendService_send() 20 | { 21 | final MainViewModel viewModel = new MainViewModel(sendService); 22 | final String givenText = "my text"; 23 | 24 | viewModel.setTwoWayText(givenText); 25 | viewModel.sendAction(); 26 | 27 | verify(sendService).send(givenText); 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deep Dive Into Android Data Binding 2 | 3 | Demo app for Deep dive into Android Data Binding talk @ Droidcon Berlin 2016 4 | 5 | ![gif2](https://cloud.githubusercontent.com/assets/469111/16058354/6297c9d0-327e-11e6-9e0c-0aab0f08ba7a.gif) 6 | 7 | Examples for: 8 | 9 | * Build in two way data binding for edit texts 10 | * Two way data binding for custom view 11 | * Lambdas 12 | * Implied Event Updates 13 | * Repeated expressions 14 | * Special variables 15 | 16 | Presentation slides: 17 | * Speakerdeck: https://speakerdeck.com/radzio/deep-dive-into-android-data-binding 18 | * Slideshare: http://www.slideshare.net/radekpiekarz/deep-dive-into-android-data-binding 19 | 20 | RecyclerView bindings are available here: https://github.com/radzio/android-data-binding-recyclerview 21 | 22 | ### Recommended resources 23 | 24 | * [Advanced Data Binding - Google I/O 2016](https://www.youtube.com/watch?v=DAmMN7m3wLU) 25 | * [George Mount @ Medium](https://medium.com/@georgemount007) 26 | * [George Mount @ Wordpress.com](https://halfthought.wordpress.com/) 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | apply plugin: 'com.jakewharton.hugo' 4 | 5 | android { 6 | compileSdkVersion 23 7 | buildToolsVersion "23.0.3" 8 | 9 | dataBinding { 10 | enabled = true 11 | } 12 | 13 | defaultConfig { 14 | applicationId "net.droidlabs.deepdiveintoandroiddatabinding" 15 | minSdkVersion 16 16 | targetSdkVersion 23 17 | versionCode 1 18 | versionName "1.0" 19 | } 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | } 27 | 28 | dependencies { 29 | compile fileTree(dir: 'libs', include: ['*.jar']) 30 | testCompile 'junit:junit:4.12' 31 | compile 'com.android.support:appcompat-v7:23.4.0' 32 | compile 'com.android.support:design:23.4.0' 33 | 34 | compile 'com.google.dagger:dagger:2.4' 35 | apt 'com.google.dagger:dagger-compiler:2.4' 36 | 37 | compile "com.github.danielnilsson9:color-picker-view:1.4.0@aar" 38 | 39 | testCompile "junit:junit:4.12" 40 | testCompile "org.mockito:mockito-core:1.10.19" 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/viewmodel/MainViewModel.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.viewmodel; 2 | 3 | import android.databinding.BaseObservable; 4 | import android.databinding.Bindable; 5 | import android.view.View; 6 | import net.droidlabs.deepdiveintoandroiddatabinding.BR; 7 | import net.droidlabs.deepdiveintoandroiddatabinding.services.ISendService; 8 | 9 | import javax.inject.Inject; 10 | 11 | public class MainViewModel extends BaseObservable 12 | { 13 | private final ISendService sendService; 14 | 15 | private String twoWayText; 16 | 17 | @Inject 18 | public MainViewModel(ISendService sendService) 19 | { 20 | this.sendService = sendService; 21 | } 22 | 23 | public void sendAction() 24 | { 25 | this.sendService.send(twoWayText); 26 | } 27 | 28 | public void sendAction(View view) 29 | { 30 | this.sendService.send(); 31 | } 32 | 33 | public void setTwoWayText(String twoWayText) 34 | { 35 | this.twoWayText = twoWayText; 36 | this.notifyPropertyChanged(BR.twoWayText); 37 | } 38 | 39 | @Bindable 40 | public String getTwoWayText() 41 | { 42 | return this.twoWayText; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/net/droidlabs/deepdiveintoandroiddatabinding/bindings/ColorPickerViewBindings.java: -------------------------------------------------------------------------------- 1 | package net.droidlabs.deepdiveintoandroiddatabinding.bindings; 2 | 3 | import android.databinding.*; 4 | import com.github.danielnilsson9.colorpickerview.view.ColorPickerView; 5 | 6 | public abstract class ColorPickerViewBindings 7 | { 8 | @BindingAdapter("color") 9 | public static void setColor(ColorPickerView view, int color) 10 | { 11 | if (color != view.getColor()) 12 | { 13 | view.setColor(color); 14 | } 15 | } 16 | 17 | @InverseBindingAdapter(attribute = "color", event = "colorAttrChanged") 18 | public static int getColor(ColorPickerView view) 19 | { 20 | return view.getColor(); 21 | } 22 | 23 | @BindingAdapter("colorAttrChanged") 24 | public static void setColorListener(ColorPickerView view, final InverseBindingListener inverseBindingListener) 25 | { 26 | if (inverseBindingListener == null) 27 | { 28 | view.setOnColorChangedListener(null); 29 | } 30 | else 31 | { 32 | view.setOnColorChangedListener(new ColorPickerView.OnColorChangedListener() 33 | { 34 | @Override 35 | public void onColorChanged(int newColor) 36 | { 37 | inverseBindingListener.onChange(); 38 | } 39 | }); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_two_way.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 15 | 19 | 20 | 26 | 27 | 32 | 33 |