├── settings.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── app ├── src │ ├── main │ │ ├── assets │ │ │ └── fonts │ │ │ │ └── zhuanti.ttf │ │ ├── res │ │ │ ├── drawable │ │ │ │ └── ic_launcher.png │ │ │ ├── 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 │ │ │ │ ├── styles.xml │ │ │ │ └── strings.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── card.xml │ │ │ │ ├── activity_main.xml │ │ │ │ ├── include.xml │ │ │ │ ├── recycler_item.xml │ │ │ │ ├── activity_view_stub.xml │ │ │ │ ├── activity_normal_object.xml │ │ │ │ ├── activity_observer.xml │ │ │ │ ├── activity_observer_field.xml │ │ │ │ ├── list_item.xml │ │ │ │ ├── activity_attribute_setters.xml │ │ │ │ ├── activity_converters.xml │ │ │ │ ├── activity_event.xml │ │ │ │ ├── activity_observer_collection.xml │ │ │ │ ├── activity_demo.xml │ │ │ │ ├── activity_two_way.xml │ │ │ │ └── activity_combine.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── connorlin │ │ │ │ └── databinding │ │ │ │ ├── task │ │ │ │ └── Task.java │ │ │ │ ├── listener │ │ │ │ └── ColorChangeListener.java │ │ │ │ ├── handler │ │ │ │ ├── ContextHandler.java │ │ │ │ ├── Presenter.java │ │ │ │ ├── EventHandler.java │ │ │ │ └── DemoHandler.java │ │ │ │ ├── utils │ │ │ │ ├── MyStringUtils.java │ │ │ │ └── Utils.java │ │ │ │ ├── model │ │ │ │ ├── ObservableFieldContact.java │ │ │ │ ├── Contact.java │ │ │ │ ├── RecyclerItem.java │ │ │ │ ├── TwoWayModel.java │ │ │ │ ├── User.java │ │ │ │ ├── ObservableContact.java │ │ │ │ └── FontFamily.java │ │ │ │ ├── context │ │ │ │ ├── EventActivity.java │ │ │ │ ├── AttributeSettersActivity.java │ │ │ │ ├── BaseActivity.java │ │ │ │ ├── ObserverActivity.java │ │ │ │ ├── NormalObjectActivity.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── ObserverFieldActivity.java │ │ │ │ ├── DemoActivity.java │ │ │ │ ├── TwoWayActivity.java │ │ │ │ ├── CombineActivity.java │ │ │ │ ├── ViewStubActivity.java │ │ │ │ ├── ConvertersActivity.java │ │ │ │ └── ObserverCollectionActivity.java │ │ │ │ ├── view │ │ │ │ ├── Card.java │ │ │ │ └── TwoWayView.java │ │ │ │ └── adapter │ │ │ │ ├── ListAdapter.java │ │ │ │ └── RecyclerAdapter.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── connorlin │ │ │ └── databinding │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── connorlin │ │ └── databinding │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── gradle.properties ├── gradlew.bat ├── gradlew └── README.md /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .idea 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | /local.properties -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/assets/fonts/zhuanti.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/assets/fonts/zhuanti.ttf -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/res/drawable/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConnorLin/DataBindingDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /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/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/task/Task.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.task; 2 | 3 | import android.util.Log; 4 | 5 | /** 6 | * ClassName: Task 7 | * Description: 8 | * Author: connorlin 9 | * Date: Created on 2016-7-1. 10 | */ 11 | public class Task implements Runnable { 12 | 13 | @Override 14 | public void run() { 15 | Log.i("connor", "Task run"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/listener/ColorChangeListener.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.listener; 2 | 3 | import com.connorlin.databinding.view.TwoWayView; 4 | 5 | /** 6 | * ClassName: ColorChangeListener 7 | * Description: 8 | * Author: connorlin 9 | * Date: Created on 2016/7/2. 10 | */ 11 | public interface ColorChangeListener { 12 | void onColorChange(TwoWayView view, int color); 13 | } 14 | -------------------------------------------------------------------------------- /app/src/test/java/com/connorlin/databinding/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/connorlin/databinding/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/handler/ContextHandler.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.handler; 2 | 3 | import android.content.Context; 4 | 5 | import com.connorlin.databinding.R; 6 | 7 | /** 8 | * ClassName: ContextHandler 9 | * Description: 10 | * Author: connorlin 11 | * Date: Created on 2016-6-30. 12 | */ 13 | public class ContextHandler { 14 | 15 | public String loadString(Context context) { 16 | return context.getResources().getString(R.string.string_from_context); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/utils/MyStringUtils.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.utils; 2 | 3 | /** 4 | * ClassName: MyStringUtils 5 | * Description: 6 | * Author: connorlin 7 | * Date: Created on 2016-6-29. 8 | */ 9 | public class MyStringUtils { 10 | 11 | // 首字母大写 12 | public static String capitalize(final String word) { 13 | if (word.length() > 1) { 14 | return String.valueOf(word.charAt(0)).toUpperCase() + word.substring(1); 15 | } 16 | return word; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/handler/Presenter.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.handler; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | 6 | import com.connorlin.databinding.model.RecyclerItem; 7 | 8 | /** 9 | * ClassName: Presentr 10 | * Description: 11 | * Author: connorlin 12 | * Date: Created on 2016-7-1. 13 | */ 14 | public class Presenter { 15 | 16 | public void onTypeClick(Context context, RecyclerItem recyclerItem) { 17 | Intent intent = new Intent(recyclerItem.getAction()); 18 | context.startActivity(intent); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/model/ObservableFieldContact.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.model; 2 | 3 | import android.databinding.ObservableField; 4 | 5 | /** 6 | * ClassName: PlainContact 7 | * Description: 8 | * Author: connorlin 9 | * Date: Created on 2016-6-29. 10 | */ 11 | public class ObservableFieldContact { 12 | public ObservableField mName = new ObservableField<>(); 13 | public ObservableField mPhone = new ObservableField<>(); 14 | 15 | public ObservableFieldContact(String name, String phone) { 16 | mName.set(name); 17 | mPhone.set(phone); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/card.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 16 | 17 | 21 | 22 | -------------------------------------------------------------------------------- /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 /Users/connorlin/Program/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/java/com/connorlin/databinding/model/Contact.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.model; 2 | 3 | /** 4 | * ClassName: BaseContact 5 | * Description: 6 | * Author: connorlin 7 | * Date: Created on 2016-6-29. 8 | */ 9 | public class Contact { 10 | 11 | private String mName; 12 | private String mPhone; 13 | 14 | public Contact(String name, String phone) { 15 | mName = name; 16 | mPhone = phone; 17 | } 18 | 19 | public String getName() { 20 | return mName; 21 | } 22 | 23 | public void setName(String name) { 24 | mName = name; 25 | } 26 | 27 | public String getPhone() { 28 | return mPhone; 29 | } 30 | 31 | public void setPhone(String phone) { 32 | mPhone = phone; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/model/RecyclerItem.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.model; 2 | 3 | /** 4 | * ClassName: Item 5 | * Description: 6 | * Author: connorlin 7 | * Date: Created on 2016-7-1. 8 | */ 9 | public class RecyclerItem { 10 | 11 | private String mType; 12 | private String mAction; 13 | 14 | public RecyclerItem(String type, String action) { 15 | mType = type; 16 | mAction = action; 17 | } 18 | 19 | public String getType() { 20 | return mType; 21 | } 22 | 23 | public void setType(String type) { 24 | mType = type; 25 | } 26 | 27 | public String getAction() { 28 | return mAction; 29 | } 30 | 31 | public void setAction(String action) { 32 | mAction = action; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/include.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 15 | 16 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/model/TwoWayModel.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.model; 2 | 3 | import android.databinding.ObservableInt; 4 | import android.graphics.Color; 5 | 6 | import com.connorlin.databinding.listener.ColorChangeListener; 7 | import com.connorlin.databinding.view.TwoWayView; 8 | 9 | /** 10 | * ClassName: TwoWayHandler 11 | * Description: 12 | * Author: connorlin 13 | * Date: Created on 2016/7/2. 14 | */ 15 | public class TwoWayModel { 16 | 17 | public ObservableInt mColor = new ObservableInt(Color.GRAY); 18 | 19 | public ColorChangeListener onColorChangeListener() { 20 | return new ColorChangeListener() { 21 | @Override 22 | public void onColorChange(TwoWayView view, int color) { 23 | view.setBackgroundColor(Color.YELLOW); 24 | } 25 | }; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/context/EventActivity.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.context; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Bundle; 5 | 6 | import com.connorlin.databinding.R; 7 | import com.connorlin.databinding.databinding.ActivityEventBinding; 8 | import com.connorlin.databinding.handler.EventHandler; 9 | import com.connorlin.databinding.task.Task; 10 | 11 | public class EventActivity extends BaseActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | // setContentView(R.layout.activity_event); 17 | ActivityEventBinding binding = 18 | DataBindingUtil.setContentView(this, R.layout.activity_event); 19 | 20 | binding.setHandler(new EventHandler(this)); 21 | binding.setTask(new Task()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.connorlin.databinding" 9 | minSdkVersion 17 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | dataBinding { 22 | enabled true 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | testCompile 'junit:junit:4.12' 29 | compile 'com.android.support:appcompat-v7:23.4.0' 30 | compile 'com.android.support:recyclerview-v7:24.0.0' 31 | compile 'com.github.bumptech.glide:glide:3.7.0' 32 | } 33 | -------------------------------------------------------------------------------- /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/java/com/connorlin/databinding/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.utils; 2 | 3 | import android.databinding.BindingAdapter; 4 | import android.graphics.drawable.Drawable; 5 | import android.widget.ImageView; 6 | import android.widget.TextView; 7 | 8 | import com.bumptech.glide.Glide; 9 | import com.connorlin.databinding.model.FontFamily; 10 | 11 | /** 12 | * ClassName: Utils 13 | * Description: 14 | * Author: connorlin 15 | * Date: Created on 2016/7/2. 16 | */ 17 | public class Utils { 18 | 19 | // 使用注解,无需手动调用此函数 20 | @BindingAdapter({"imageUrl", "error"}) 21 | public static void loadImage(ImageView view, String url, Drawable error) { 22 | Glide.with(view.getContext()).load(url).error(error).into(view); 23 | } 24 | 25 | @BindingAdapter({"font"}) 26 | public static void setFont(TextView textView, String fontName) { 27 | textView.setTypeface(FontFamily.getInstance().getFont(fontName, textView.getContext())); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/context/AttributeSettersActivity.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.context; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Bundle; 5 | 6 | import com.connorlin.databinding.R; 7 | import com.connorlin.databinding.databinding.ActivityAttributeSettersBinding; 8 | import com.connorlin.databinding.model.User; 9 | 10 | public class AttributeSettersActivity extends BaseActivity { 11 | 12 | private ActivityAttributeSettersBinding mBinding; 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | // setContentView(R.layout.activity_attribute_setters); 18 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_attribute_setters); 19 | 20 | User user = new User("Connor", "Lin", 28); 21 | mBinding.setUser(user); 22 | 23 | mBinding.setUrl("http://connorlin.github.io/images/avatar.jpg"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/connorlin/databinding/context/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.connorlin.databinding.context; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBar; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.MenuItem; 7 | 8 | public class BaseActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | 14 | ActionBar actionBar = getSupportActionBar(); 15 | if (actionBar != null) { 16 | actionBar.setDisplayHomeAsUpEnabled(true); 17 | actionBar.setDisplayShowTitleEnabled(true); 18 | } 19 | } 20 | 21 | @Override 22 | public boolean onOptionsItemSelected(MenuItem item) { 23 | switch (item.getItemId()) { 24 | case android.R.id.home: 25 | finish(); 26 | break; 27 | } 28 | return super.onOptionsItemSelected(item); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/recycler_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 11 | 12 | 13 | 17 | 18 |