├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── styles.xml │ │ │ │ ├── colors.xml │ │ │ │ └── dbinding_config.xml │ │ │ ├── drawable-xhdpi │ │ │ │ ├── kale.png │ │ │ │ ├── header.jpg │ │ │ │ ├── hit_man.jpg │ │ │ │ ├── speed_icon.png │ │ │ │ ├── star_craft.jpg │ │ │ │ ├── watch_dogs.jpg │ │ │ │ ├── hit_man_icon.png │ │ │ │ ├── assassins_creed.jpg │ │ │ │ ├── star_craft_icon.png │ │ │ │ ├── watch_dogs_icon.png │ │ │ │ └── assassins_creed_icon.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 │ │ │ └── layout │ │ │ │ ├── game_list_activity.xml │ │ │ │ ├── header_layout.xml │ │ │ │ ├── user_detail_activity.xml │ │ │ │ ├── game_item.xml │ │ │ │ └── game_detail_activity.xml │ │ ├── java │ │ │ ├── vm │ │ │ │ ├── ignored │ │ │ │ │ └── IgnoreViewModel.java │ │ │ │ └── EventViewModel.java │ │ │ └── kale │ │ │ │ └── db │ │ │ │ ├── model │ │ │ │ └── NewsInfo.java │ │ │ │ ├── network │ │ │ │ └── NetworkService.java │ │ │ │ ├── base │ │ │ │ ├── BaseActivity.java │ │ │ │ └── BaseAdapterItem.java │ │ │ │ ├── GameListPresenter.java │ │ │ │ ├── GameListActivity.java │ │ │ │ ├── GameDetailActivity.java │ │ │ │ ├── UserDetailActivity.java │ │ │ │ └── GameItem.java │ │ └── AndroidManifest.xml │ └── androidTest │ │ └── java │ │ └── kale │ │ └── dbinding │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── apt ├── .gitignore ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── services │ │ │ └── javax.annotation.processing.Processor │ │ └── java │ │ └── kale │ │ └── dbinding │ │ ├── VmGenerator.java │ │ └── VmGenerateProcessor.java └── build.gradle ├── dbindinglib ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ └── values │ │ │ │ ├── strings.xml │ │ │ │ ├── attr.xml │ │ │ │ └── dbinding_config.xml │ │ ├── java │ │ │ └── kale │ │ │ │ └── dbinding │ │ │ │ ├── adapters │ │ │ │ ├── ViewAttrAdapter.java │ │ │ │ ├── RecyclerViewAttrAdapter.java │ │ │ │ ├── EditTextAttrAdapter.java │ │ │ │ ├── ImageViewAttrAdapter.java │ │ │ │ └── TextViewAttrAdapter.java │ │ │ │ ├── util │ │ │ │ ├── BitmapUtil.java │ │ │ │ └── ReflectUtil.java │ │ │ │ ├── SerializableViewModel.java │ │ │ │ ├── BaseViewModel.java │ │ │ │ ├── data │ │ │ │ ├── ObservableBitmap.java │ │ │ │ ├── ObservableCharSequence.java │ │ │ │ └── ObservableArrayList.java │ │ │ │ ├── ViewModelStorage.java │ │ │ │ └── DBinding.java │ │ └── AndroidManifest.xml │ ├── androidTest │ │ └── java │ │ │ └── kale │ │ │ └── dbinding │ │ │ └── ApplicationTest.java │ └── test │ │ └── java │ │ └── kale │ │ ├── dbinding │ │ └── ViewModelStorageTest.java │ │ └── test │ │ └── plugin │ │ └── PluginTypeTest.java ├── proguard-rules.pro └── build.gradle ├── pluginlib ├── .gitignore ├── src │ ├── main │ │ └── java │ │ │ ├── android │ │ │ ├── graphics │ │ │ │ └── Bitmap.java │ │ │ ├── support │ │ │ │ ├── v4 │ │ │ │ │ └── view │ │ │ │ │ │ └── PagerAdapter.java │ │ │ │ └── v7 │ │ │ │ │ └── widget │ │ │ │ │ └── RecyclerView.java │ │ │ ├── widget │ │ │ │ └── ListAdapter.java │ │ │ └── view │ │ │ │ └── View.java │ │ │ └── kale │ │ │ └── dbinding │ │ │ ├── model │ │ │ ├── SimpleField.java │ │ │ └── SimpleClass.java │ │ │ ├── Test.java │ │ │ ├── util │ │ │ ├── LetterUtil.java │ │ │ └── FileHelper.java │ │ │ ├── parser │ │ │ ├── TypeFinder.java │ │ │ └── ViewModelGenerator.java │ │ │ ├── CodeTemple.java │ │ │ └── GenViewModel.java │ └── test │ │ └── java │ │ └── kale │ │ └── dbinding │ │ └── util │ │ └── LetterUtilTest.java └── build.gradle ├── settings.gradle ├── pic ├── 01.jpg ├── 02.jpg ├── doc.png ├── logo.jpg ├── gen_vm.gif └── preview.png ├── plugin ├── README.md └── lib │ └── pluginlib.jar ├── Plugin ├── DBindPlugin.zip ├── resources │ └── META-INF │ │ └── plugin.xml └── src │ └── kale │ └── dbinding │ └── plugin │ └── ViewModelGenerator.java ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .gitattributes ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /apt/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /dbindinglib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /pluginlib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':dbindinglib', ':pluginlib' 2 | -------------------------------------------------------------------------------- /pic/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/pic/01.jpg -------------------------------------------------------------------------------- /pic/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/pic/02.jpg -------------------------------------------------------------------------------- /pic/doc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/pic/doc.png -------------------------------------------------------------------------------- /pic/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/pic/logo.jpg -------------------------------------------------------------------------------- /pic/gen_vm.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/pic/gen_vm.gif -------------------------------------------------------------------------------- /pic/preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/pic/preview.png -------------------------------------------------------------------------------- /plugin/README.md: -------------------------------------------------------------------------------- 1 | # Idea Plugin Module 2 | 3 | 此module是idea的插件工程,非android工程,请用idea打开。 -------------------------------------------------------------------------------- /Plugin/DBindPlugin.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/Plugin/DBindPlugin.zip -------------------------------------------------------------------------------- /plugin/lib/pluginlib.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/plugin/lib/pluginlib.jar -------------------------------------------------------------------------------- /apt/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | kale.dbinding.VmGenerateProcessor -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DBinding 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /pluginlib/src/main/java/android/graphics/Bitmap.java: -------------------------------------------------------------------------------- 1 | package android.graphics; 2 | 3 | public class Bitmap { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/kale.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/kale.png -------------------------------------------------------------------------------- /dbindinglib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DBindingLib 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/header.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/header.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/hit_man.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/hit_man.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/speed_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/speed_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/star_craft.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/star_craft.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/watch_dogs.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/watch_dogs.jpg -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/hit_man_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/hit_man_icon.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/assassins_creed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/assassins_creed.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/star_craft_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/star_craft_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/watch_dogs_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/watch_dogs_icon.png -------------------------------------------------------------------------------- /pluginlib/src/main/java/android/support/v4/view/PagerAdapter.java: -------------------------------------------------------------------------------- 1 | package android.support.v4.view; 2 | 3 | public class PagerAdapter { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/assassins_creed_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaleai/DBinding/HEAD/app/src/main/res/drawable-xhdpi/assassins_creed_icon.png -------------------------------------------------------------------------------- /app/src/main/java/vm/ignored/IgnoreViewModel.java: -------------------------------------------------------------------------------- 1 | package vm.ignored; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2016/2/19 6 | */ 7 | public class IgnoreViewModel { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/android/widget/ListAdapter.java: -------------------------------------------------------------------------------- 1 | package android.widget; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2016/1/30 6 | */ 7 | public class ListAdapter { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/android/support/v7/widget/RecyclerView.java: -------------------------------------------------------------------------------- 1 | package android.support.v7.widget; 2 | 3 | public class RecyclerView { 4 | 5 | public static class Adapter { 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .idea/ 10 | app/src/debug 11 | *.class 12 | Plugin/out/ 13 | -------------------------------------------------------------------------------- /apt/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(include: ['*.jar'], dir: 'libs') 5 | compile project(':pluginlib') 6 | } 7 | 8 | sourceCompatibility = "1.7" 9 | targetCompatibility = "1.7" 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Aug 07 11:24:31 CST 2016 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.14.1-all.zip 7 | -------------------------------------------------------------------------------- /dbindinglib/src/main/res/values/attr.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/adapters/ViewAttrAdapter.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.adapters; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2016/2/5 6 | * {@link android.databinding.adapters.ViewBindingAdapter} 7 | */ 8 | public class ViewAttrAdapter { 9 | 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/android/view/View.java: -------------------------------------------------------------------------------- 1 | package android.view; 2 | 3 | public class View { 4 | 5 | public interface OnClickListener { 6 | void onClick(View v); 7 | } 8 | 9 | public interface OnLongClickListener { 10 | boolean onLongClick(View v); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /dbindinglib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /pluginlib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | // thanks for http://stackoverflow.com/questions/24662801/bad-class-file-magic-or-version 4 | sourceCompatibility = 1.8 5 | targetCompatibility = 1.8 6 | 7 | 8 | dependencies { 9 | // 如果引入了android包,插件会自动剔除它的,所以对于android中的类,我专门在android这个包中写了空类。 10 | testCompile 'junit:junit:4.12' 11 | } -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/adapters/RecyclerViewAttrAdapter.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.adapters; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2016/1/30 6 | */ 7 | public class RecyclerViewAttrAdapter { 8 | 9 | /* 10 | @BindingAdapter({"rcvAdapter"}) 11 | public static void setAdapter(RecyclerView view, RecyclerView.Adapter adapter) { 12 | view.setAdapter(adapter); 13 | } 14 | */ 15 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/kale/dbinding/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 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 | super(Application.class); 13 | } 14 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /apt/src/main/java/kale/dbinding/VmGenerator.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * @author Kale 10 | * @date 2016/8/7 11 | */ 12 | @Target(ElementType.TYPE) 13 | @Retention(RetentionPolicy.CLASS) 14 | public @interface VmGenerator { 15 | 16 | String value(); 17 | } 18 | -------------------------------------------------------------------------------- /dbindinglib/src/androidTest/java/kale/dbinding/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 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 | super(Application.class); 13 | } 14 | } -------------------------------------------------------------------------------- /dbindinglib/src/main/res/values/dbinding_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | java.lang.CharSequence 9 | 10 | 14 | 15 | java.lang.CharSequence 16 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #ffffff 7 | #000000 8 | #333232 9 | #e9e10e 10 | #585858 11 | 12 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/kale/dbinding/model/SimpleField.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.model; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2015/12/23 6 | */ 7 | public class SimpleField { 8 | 9 | public String type; 10 | 11 | public String name; 12 | 13 | public SimpleField(String type, String name) { 14 | if (type == null) { 15 | type = Object.class.getCanonicalName(); 16 | } 17 | this.type = type; 18 | this.name = name; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/util/BitmapUtil.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.util; 2 | 3 | import android.content.res.Resources; 4 | import android.graphics.Bitmap; 5 | import android.graphics.drawable.BitmapDrawable; 6 | import android.graphics.drawable.Drawable; 7 | 8 | /** 9 | * @author Kale 10 | * @date 2016/1/5 11 | */ 12 | public class BitmapUtil { 13 | 14 | public static Drawable bitmap2Drawable(Resources res, Bitmap bitmap) { 15 | return new BitmapDrawable(res, bitmap); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/res/values/dbinding_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | java.lang.CharSequence 9 | 10 | 14 | 15 | java.lang.CharSequence 16 | android.graphics.Bitmap 17 | 18 | 19 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/SerializableViewModel.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 2 | 3 | import java.io.Serializable; 4 | 5 | import android.support.annotation.Nullable; 6 | 7 | /** 8 | * @author Kale 9 | * @date 2016/3/2 10 | */ 11 | public class SerializableViewModel implements Serializable { 12 | 13 | private int index; 14 | 15 | public SerializableViewModel(int index) { 16 | this.index = index; 17 | } 18 | 19 | public 20 | @Nullable T toViewModel() { 21 | return (T) ViewModelStorage.getInstance().getViewModel(index); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/adapters/EditTextAttrAdapter.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.adapters; 2 | 3 | import android.databinding.BindingAdapter; 4 | import android.widget.EditText; 5 | 6 | import kale.dbinding.data.ObservableCharSequence; 7 | 8 | /** 9 | * @author Kale 10 | * @date 2016/1/15 11 | * 12 | * 如果是editText,那么设置text会改变text的字段,并且可以通过监听text字段来得到用户输入时的text。来达到双向绑定的功能 13 | */ 14 | public class EditTextAttrAdapter { 15 | 16 | @BindingAdapter("android:text") 17 | public static void setText(EditText view, ObservableCharSequence text) { 18 | view.setText(text.get()); 19 | // TextViewBindingAdapter.setText(view, text.get()); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/kale/dbinding/Test.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * @author Kale 7 | * @date 2016/1/31 8 | */ 9 | public class Test { 10 | 11 | private static final String ROOT = System.getProperty("user.dir"); 12 | 13 | public static void main(String[] args) { 14 | System.out.print("===== start "); 15 | long start = System.currentTimeMillis(); 16 | String moduleDir = ROOT + File.separator + "app" + File.separator; 17 | 18 | GenViewModel.generateViewModel(moduleDir); 19 | 20 | System.out.println("===== end =====> Total time is: " + (System.currentTimeMillis() - start) + "ms"); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/vm/EventViewModel.java: -------------------------------------------------------------------------------- 1 | package vm; 2 | 3 | import android.databinding.Bindable; 4 | 5 | import com.android.databinding.library.baseAdapters.BR; 6 | 7 | import kale.dbinding.BaseViewModel; 8 | 9 | /** Generated code from DBinding. Do not modify! */ 10 | public class EventViewModel extends BaseViewModel { 11 | 12 | private android.view.View.OnClickListener onClick; 13 | 14 | public void setOnClick(android.view.View.OnClickListener onClick) { 15 | this.onClick = onClick; 16 | notifyPropertyChanged(BR.onClick); 17 | } 18 | 19 | @Bindable 20 | public android.view.View.OnClickListener getOnClick() { 21 | return this.onClick; 22 | } 23 | } -------------------------------------------------------------------------------- /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 H:\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/java/kale/db/model/NewsInfo.java: -------------------------------------------------------------------------------- 1 | package kale.db.model; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2016/1/6 6 | */ 7 | public class NewsInfo { 8 | 9 | /** 10 | * 新闻的标题 11 | */ 12 | public String title; 13 | 14 | /** 15 | * 新闻的点赞文字 16 | */ 17 | public String isLikeText; 18 | 19 | /** 20 | * 其他的字段 21 | */ 22 | public NewsDetail newsDetail; 23 | 24 | public int[] picResIdArr; // 业务模型中支持了多个图片,但实际可能只需要显示一张 25 | 26 | public static class NewsDetail { 27 | public String content; 28 | 29 | } 30 | 31 | public NewsInfo(String title, String isLikeText, int[] picResIdArr) { 32 | this.title = title; 33 | this.isLikeText = isLikeText; 34 | this.picResIdArr = picResIdArr; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/adapters/ImageViewAttrAdapter.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.adapters; 2 | 3 | import android.databinding.BindingAdapter; 4 | import android.graphics.Bitmap; 5 | import android.widget.ImageView; 6 | 7 | import kale.dbinding.data.ObservableBitmap; 8 | 9 | /** 10 | * @author Kale 11 | * @date 2016/1/5 12 | * {@link android.databinding.adapters.ImageViewBindingAdapter} 13 | */ 14 | public class ImageViewAttrAdapter { 15 | 16 | @BindingAdapter("android:src") 17 | public static void setSrc(ImageView view, Bitmap bitmap) { 18 | view.setImageBitmap(bitmap); 19 | } 20 | 21 | @BindingAdapter("android:src") 22 | public static void setSrc(ImageView view, ObservableBitmap bitmap) { 23 | view.setImageBitmap(bitmap.get()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /dbindinglib/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 H:\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 | 19 | -keepclasseswithmembers class * extends android.databinding.ViewDataBinding{ 20 | ; 21 | } 22 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/kale/db/network/NetworkService.java: -------------------------------------------------------------------------------- 1 | package kale.db.network; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import kale.db.R; 7 | import kale.db.model.NewsInfo; 8 | 9 | /** 10 | * @author Kale 11 | * @date 2016/2/19 12 | */ 13 | public class NetworkService { 14 | 15 | /** 16 | * 为了说明的简单,这里模拟网络加载数据的步骤,还没做网络层。 17 | */ 18 | public static List loadDataFromNetwork() { 19 | ArrayList list = new ArrayList<>(); 20 | list.add(new NewsInfo("Hitman 47", "Like", new int[]{R.drawable.hit_man, R.drawable.hit_man_icon})); 21 | list.add(new NewsInfo("Assassins Creed", "Like", new int[]{R.drawable.assassins_creed, R.drawable.assassins_creed_icon})); 22 | list.add(new NewsInfo("StarCraft Ⅱ", "Like", new int[]{R.drawable.star_craft,R.drawable.star_craft_icon})); 23 | list.add(new NewsInfo("Watch Dogs", "Like", new int[]{R.drawable.watch_dogs, R.drawable.watch_dogs_icon})); 24 | return list; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/kale/dbinding/model/SimpleClass.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Kale 8 | * @date 2015/12/24 9 | */ 10 | public class SimpleClass { 11 | 12 | public String packageName; 13 | 14 | public String fullName; 15 | 16 | public String simpleName; 17 | 18 | public List fields; 19 | 20 | public String content; 21 | 22 | public String clsVarName; 23 | 24 | /** 25 | * @param fullName com.kale.vm.ViewModel 26 | */ 27 | public SimpleClass(String fullName) { 28 | fields = new ArrayList<>(); 29 | String[] fullNames = fullName.split("\\."); 30 | 31 | this.simpleName = fullNames[fullNames.length - 1]; // ViewModel 32 | int i = fullName.indexOf(simpleName); 33 | this.packageName = fullName.substring(0, i - 1); // com.kale.vm 34 | 35 | this.fullName = fullName; // com.kale.vm.ViewModel 36 | } 37 | 38 | public static void main(String[] args) { 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /dbindinglib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion '25.0.0' 6 | android.enforceUniquePackageName = true 7 | 8 | 9 | defaultConfig { 10 | minSdkVersion 14 11 | targetSdkVersion 24 12 | } 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 | provided 'com.android.support:support-annotations:23.1.1' 28 | // provided 'com.android.support:recyclerview-v7:24.0.0' 29 | 30 | /*provided ('com.android.databinding:library:1.1'){ 31 | transitive = true 32 | exclude group: 'com.android.support' 33 | }*/ 34 | 35 | testCompile project(':pluginlib') 36 | testCompile 'com.android.support:design:24.0.0' 37 | testCompile 'com.android.support:cardview-v7:24.0.0' 38 | testCompile 'junit:junit:4.12' 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/res/layout/game_list_activity.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 14 | 15 | 16 | 17 | 23 | 24 | 28 | 29 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/BaseViewModel.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 2 | 3 | import android.databinding.BaseObservable; 4 | import android.databinding.Observable; 5 | 6 | /** 7 | * @author Kale 8 | * @date 2016/1/9 9 | */ 10 | public abstract class BaseViewModel extends BaseObservable { 11 | 12 | /** 13 | * convert index to {@link SerializableViewModel} 14 | */ 15 | public SerializableViewModel toSerializable() { 16 | int index = ViewModelStorage.getInstance().putViewModel(this); 17 | return new SerializableViewModel<>(index); 18 | } 19 | 20 | public synchronized Observable.OnPropertyChangedCallback addOnValueChangedCallback(final OnValueChangedCallback callback) { 21 | Observable.OnPropertyChangedCallback cb = new Observable.OnPropertyChangedCallback() { 22 | @Override 23 | public void onPropertyChanged(Observable sender, int propertyId) { 24 | callback.onChanged(propertyId); 25 | } 26 | }; 27 | addOnPropertyChangedCallback(cb); 28 | return cb; 29 | } 30 | 31 | public interface OnValueChangedCallback { 32 | 33 | void onChanged(int propertyId); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'me.tatarka.retrolambda' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion '25.0.0' 7 | 8 | defaultConfig { 9 | applicationId "kale.dbinding" 10 | minSdkVersion 14 11 | targetSdkVersion 25 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | compileOptions { 24 | encoding "UTF-8" 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | 29 | dataBinding { 30 | enabled = true 31 | } 32 | 33 | } 34 | 35 | tasks.whenTaskAdded { task -> 36 | println 'task name: ' + task.name 37 | } 38 | 39 | dependencies { 40 | compile fileTree(include: ['*.jar'], dir: 'libs') 41 | compile project(':dbindinglib') 42 | 43 | compile 'com.android.support:appcompat-v7:25.1.0' 44 | compile 'com.android.support:recyclerview-v7:25.1.0' 45 | compile 'com.github.tianzhijiexian:CommonAdapter:1.1.9' 46 | 47 | annotationProcessor "org.lzh.compiler.parceler:parceler-compiler:0.6" 48 | compile "org.lzh.compiler.parceler:parceler-api:0.6" 49 | } 50 | -------------------------------------------------------------------------------- /pluginlib/src/main/java/kale/dbinding/util/LetterUtil.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.util; 2 | 3 | /** 4 | * @author Kale 5 | * @date 2016/1/20 6 | */ 7 | public class LetterUtil { 8 | 9 | /** 10 | * make first letter to uppercase 11 | * abc -> Abc 12 | */ 13 | public static String getUpperLetter(String str) { 14 | if (str == null || str.isEmpty()) { 15 | return str; 16 | } 17 | return str.substring(0, 1).toUpperCase() + str.substring(1); 18 | } 19 | 20 | /** 21 | * make first letter to lowercase 22 | * Abc -> abc 23 | */ 24 | public static String getLowerLetter(String str) { 25 | if (str == null || str.isEmpty()) { 26 | return str; 27 | } 28 | return str.substring(0, 1).toLowerCase() + str.substring(1); 29 | } 30 | 31 | /** 32 | * {kale} - > kale 33 | */ 34 | public static String getSubString(String originStr, String begin) { 35 | int beginIndex = originStr.indexOf(begin) + begin.length(); 36 | return originStr.substring(beginIndex); 37 | } 38 | 39 | /** 40 | * -kale -> kale 41 | */ 42 | public static String getSubString(String originStr, String begin, String end) { 43 | int beginIndex = originStr.indexOf(begin) + begin.length(); 44 | int endIndex = originStr.indexOf(end); 45 | return originStr.substring(beginIndex, endIndex); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/util/ReflectUtil.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.util; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import java.lang.reflect.Field; 6 | import java.lang.reflect.InvocationTargetException; 7 | import java.lang.reflect.Method; 8 | 9 | /** 10 | * @author Kale 11 | * @date 2016/1/12 12 | */ 13 | public class ReflectUtil { 14 | 15 | /** 16 | * like {@code return new T();} 17 | */ 18 | public static @NonNull T newInstance(Class clz) { 19 | try { 20 | return clz.newInstance(); 21 | } catch (Exception e) { 22 | e.printStackTrace(); 23 | } 24 | return null; // will not be null 25 | } 26 | 27 | /** 28 | * like {@code i = value;} 29 | */ 30 | public static void setFieldValue(@NonNull Object target, Field field ,Object value) { 31 | field.setAccessible(true); 32 | try { 33 | field.set(target, value); // init fields 34 | } catch (IllegalAccessException e) { 35 | e.printStackTrace(); 36 | } 37 | } 38 | 39 | public static void invokeMethod(Method method,Object obj,Object... params) { 40 | try { 41 | method.invoke(obj,params); 42 | } catch (IllegalAccessException e) { 43 | e.printStackTrace(); 44 | } catch (InvocationTargetException e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /pluginlib/src/test/java/kale/dbinding/util/LetterUtilTest.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.util; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | import static org.junit.Assert.assertNull; 7 | 8 | /** 9 | * @author Kale 10 | * @date 2016/4/12 11 | */ 12 | public class LetterUtilTest { 13 | 14 | @Test 15 | public void testGetUpperLetter() throws Exception { 16 | String str; 17 | str = LetterUtil.getUpperLetter("abc"); 18 | assertEquals("Abc", str); 19 | 20 | str= LetterUtil.getUpperLetter("aBc"); 21 | assertEquals("ABc", str); 22 | 23 | str = LetterUtil.getUpperLetter(null); 24 | assertNull(str); 25 | 26 | str = LetterUtil.getUpperLetter(""); 27 | assertEquals("", str); 28 | } 29 | 30 | @Test 31 | public void testGetLowerLetter() throws Exception { 32 | String str; 33 | str = LetterUtil.getLowerLetter("abc"); 34 | assertEquals("abc", str); 35 | 36 | str= LetterUtil.getLowerLetter("aBc"); 37 | assertEquals("aBc", str); 38 | 39 | str= LetterUtil.getLowerLetter("ABc"); 40 | assertEquals("aBc", str); 41 | 42 | str = LetterUtil.getLowerLetter(null); 43 | assertNull(str); 44 | 45 | str = LetterUtil.getLowerLetter(""); 46 | assertEquals("", str); 47 | } 48 | 49 | @Test 50 | public void testGetSubString() throws Exception { 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /app/src/main/java/kale/db/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package kale.db.base; 2 | 3 | import android.app.Activity; 4 | import android.databinding.ViewDataBinding; 5 | import android.os.Bundle; 6 | import android.support.annotation.LayoutRes; 7 | import android.support.annotation.Nullable; 8 | import android.support.v7.app.AppCompatActivity; 9 | 10 | import com.lzh.compiler.parceler.Parceler; 11 | 12 | import kale.dbinding.DBinding; 13 | import vm.EventViewModel; 14 | 15 | /** 16 | * @author Kale 17 | * @date 2016/7/5 18 | */ 19 | 20 | public abstract class BaseActivity extends AppCompatActivity { 21 | 22 | protected EventViewModel viewEvents = new EventViewModel(); 23 | 24 | protected T b; 25 | 26 | @Override 27 | protected void onCreate(@Nullable Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | Parceler.injectToEntity(this, getIntent()); 30 | bindViews(); 31 | beforeSetViews(); 32 | setViews(); 33 | doTransaction(); 34 | } 35 | 36 | protected Activity getActivity() { 37 | return this; 38 | } 39 | 40 | @LayoutRes 41 | protected abstract int getLayoutResId(); 42 | 43 | protected void bindViews() { 44 | b = DBinding.bind(this, getLayoutResId()); 45 | } 46 | 47 | protected abstract void beforeSetViews(); 48 | 49 | protected abstract void setViews(); 50 | 51 | protected abstract void doTransaction(); 52 | } 53 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/data/ObservableBitmap.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.data; 2 | 3 | import android.databinding.ObservableField; 4 | import android.graphics.Bitmap; 5 | import android.os.Parcel; 6 | import android.os.Parcelable; 7 | 8 | import kale.dbinding.ViewModelStorage; 9 | 10 | /** 11 | * @author Kale 12 | * @date 2016/12/19 13 | */ 14 | public class ObservableBitmap extends ObservableField implements Parcelable { 15 | 16 | public ObservableBitmap() { 17 | super(); 18 | } 19 | 20 | public ObservableBitmap(Bitmap value) { 21 | super(value); 22 | } 23 | 24 | public static ObservableBitmap create(Bitmap bitmap) { 25 | return new ObservableBitmap(bitmap); 26 | } 27 | 28 | public static ObservableBitmap create() { 29 | return new ObservableBitmap(); 30 | } 31 | 32 | @Override 33 | public int describeContents() { 34 | return 0; 35 | } 36 | 37 | @Override 38 | public void writeToParcel(Parcel dest, int i) { 39 | int index = ViewModelStorage.getInstance().putViewModel(this); 40 | dest.writeInt(index); 41 | } 42 | 43 | public static final Creator CREATOR = new Creator() { 44 | @Override 45 | public ObservableBitmap createFromParcel(Parcel source) { 46 | int index = source.readInt(); 47 | return (ObservableBitmap) ViewModelStorage.getInstance().getViewModel(index); 48 | } 49 | 50 | @Override 51 | public ObservableBitmap[] newArray(int size) { 52 | return new ObservableBitmap[size]; 53 | } 54 | }; 55 | } 56 | -------------------------------------------------------------------------------- /Plugin/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | kale.dbinding.plugin 3 | DBinding Plugin 4 | 1.0 5 | kale 6 | 7 | Quickly to generate ViewModel for DBinging.
9 |
10 | ● Support for DataBinding and DBinding.
11 |
DBinding 12 | 13 | ]]>
14 | 15 | 17 | most HTML tags may be used 18 | ]]> 19 | 20 | 21 | 22 | 23 | 24 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
-------------------------------------------------------------------------------- /app/src/main/res/layout/header_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 11 | 12 | 16 | 17 | 21 | 22 | 23 | 28 | 29 | 39 | 40 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/data/ObservableCharSequence.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding.data; 2 | 3 | import java.io.Serializable; 4 | 5 | import android.databinding.ObservableField; 6 | import android.os.Parcel; 7 | import android.os.Parcelable; 8 | 9 | import kale.dbinding.ViewModelStorage; 10 | 11 | /** 12 | * @author Kale 13 | * @date 2016/12/19 14 | */ 15 | public class ObservableCharSequence extends ObservableField implements Parcelable, Serializable { 16 | 17 | public ObservableCharSequence() { 18 | super(); 19 | } 20 | 21 | public ObservableCharSequence(CharSequence value) { 22 | super(value); 23 | } 24 | 25 | public static ObservableCharSequence create() { 26 | return new ObservableCharSequence(); 27 | } 28 | 29 | public static ObservableCharSequence create(CharSequence value) { 30 | return new ObservableCharSequence(value); 31 | } 32 | 33 | @Override 34 | public int describeContents() { 35 | return 0; 36 | } 37 | 38 | @Override 39 | public void writeToParcel(Parcel dest, int i) { 40 | int index = ViewModelStorage.getInstance().putViewModel(this); 41 | dest.writeInt(index); 42 | } 43 | 44 | public static final Parcelable.Creator CREATOR 45 | = new Parcelable.Creator() { 46 | 47 | @Override 48 | public ObservableCharSequence createFromParcel(Parcel source) { 49 | int index = source.readInt(); 50 | return (ObservableCharSequence) ViewModelStorage.getInstance().getViewModel(index); 51 | } 52 | 53 | @Override 54 | public ObservableCharSequence[] newArray(int size) { 55 | return new ObservableCharSequence[size]; 56 | } 57 | }; 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/kale/db/GameListPresenter.java: -------------------------------------------------------------------------------- 1 | package kale.db; 2 | 3 | import java.util.List; 4 | 5 | import android.app.Activity; 6 | import android.graphics.BitmapFactory; 7 | import android.support.annotation.NonNull; 8 | import android.support.v7.widget.RecyclerView; 9 | 10 | import kale.adapter.CommonRcvAdapter; 11 | import kale.adapter.item.AdapterItem; 12 | import kale.db.model.NewsInfo; 13 | import kale.db.network.NetworkService; 14 | import kale.dbinding.data.ObservableBitmap; 15 | import kale.dbinding.data.ObservableCharSequence; 16 | import kale.dbinding.data.ObservableArrayList; 17 | 18 | /** 19 | * @author Kale 20 | * @date 2015/12/24 21 | */ 22 | class GameListPresenter { 23 | 24 | private final ObservableArrayList mList; // 界面的数据对象 25 | 26 | GameListPresenter() { 27 | mList = new ObservableArrayList<>(); 28 | } 29 | 30 | /** 31 | * 这个当然可以放在构造方法中进行,我这里为了说明view层调用p的方法,强制加入了一个回调。 32 | */ 33 | boolean init(final Activity activity, ObservableCharSequence name, ObservableBitmap bmp) { 34 | name.set("Need for Speed"); 35 | // 一般图片加载都是用url,这里为了简单用了bitmap 36 | bmp.set(BitmapFactory.decodeResource(activity.getResources(), R.drawable.speed_icon)); 37 | return true; 38 | } 39 | 40 | void loadData() { 41 | List data = NetworkService.loadDataFromNetwork(); 42 | // load data from network 43 | mList.addAll(0, data); // don't need to call notifyDataSetChanged() 44 | } 45 | 46 | RecyclerView.Adapter getAdapter() { 47 | return new CommonRcvAdapter(mList) { 48 | @NonNull 49 | @Override 50 | public AdapterItem createItem(Object o) { 51 | return new GameItem(); 52 | } 53 | }; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /dbindinglib/src/main/java/kale/dbinding/ViewModelStorage.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 2 | 3 | import java.util.Map; 4 | 5 | import android.databinding.BaseObservable; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.support.v4.util.ArrayMap; 9 | 10 | /** 11 | * @author Kale 12 | * @date 2016/1/22 13 | */ 14 | public class ViewModelStorage { 15 | 16 | private static ViewModelStorage instance; 17 | 18 | private final Map mMap = new ArrayMap<>(); 19 | 20 | public static ViewModelStorage getInstance() { 21 | if (instance == null) { 22 | instance = new ViewModelStorage(); 23 | } 24 | return instance; 25 | } 26 | 27 | private ViewModelStorage() { 28 | 29 | } 30 | 31 | Map getMap() { 32 | return mMap; 33 | } 34 | 35 | public int putViewModel(@NonNull BaseObservable viewModel) { 36 | if (mMap.containsValue(viewModel)) { 37 | for (Map.Entry entry : mMap.entrySet()) { 38 | if (entry.getValue() == viewModel) { 39 | return entry.getKey(); 40 | } 41 | } 42 | return 0; 43 | } else { 44 | int index = mMap.size(); 45 | mMap.put(index, viewModel); 46 | return index; 47 | } 48 | } 49 | 50 | public 51 | @Nullable 52 | BaseObservable getViewModel(int key) { 53 | if (mMap.size() == 0) { 54 | return null; 55 | } else { 56 | return mMap.remove(key); 57 | } 58 | } 59 | 60 | int getRealSize() { 61 | int size = 0; 62 | for (Map.Entry entry : mMap.entrySet()) { 63 | if (entry.getValue() != null) { 64 | size++; 65 | } 66 | } 67 | return size; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/kale/db/GameListActivity.java: -------------------------------------------------------------------------------- 1 | package kale.db; 2 | 3 | import android.support.v7.widget.LinearLayoutManager; 4 | import android.widget.Toast; 5 | 6 | import kale.db.base.BaseActivity; 7 | import kale.db.databinding.GameListActivityBinding; 8 | import kale.db.databinding.HeaderLayoutBinding; 9 | import kale.dbinding.DBinding; 10 | import kale.dbinding.data.ObservableBitmap; 11 | import kale.dbinding.data.ObservableCharSequence; 12 | 13 | public class GameListActivity extends BaseActivity { 14 | 15 | private GameListPresenter mPresenter; 16 | 17 | private HeaderLayoutBinding mBind; 18 | 19 | @Override 20 | protected int getLayoutResId() { 21 | return R.layout.game_list_activity; 22 | } 23 | 24 | @Override 25 | protected void beforeSetViews() { 26 | mBind = DBinding.bind(findViewById(R.id.header_layout)); 27 | mBind.setEvent(viewEvents); 28 | mBind.setName(ObservableCharSequence.create("default value")); 29 | mBind.setPic(ObservableBitmap.create()); 30 | 31 | mPresenter = new GameListPresenter(); 32 | } 33 | 34 | protected void setViews() { 35 | viewEvents.setOnClick(v -> { 36 | if (v == b.headerLayout.headPicIv) { 37 | new UserDetailActivityDispatcher() 38 | .setName(mBind.getName()) 39 | .setPic(mBind.getPic()) 40 | .start(getActivity()); 41 | } 42 | }); 43 | 44 | b.mainRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); 45 | b.mainRv.setAdapter(mPresenter.getAdapter()); 46 | } 47 | 48 | protected void doTransaction() { 49 | if (mPresenter.init(this, mBind.getName(), mBind.getPic())) { 50 | Toast.makeText(GameListActivity.this, "Init Completed", Toast.LENGTH_SHORT).show(); 51 | } 52 | mPresenter.loadData(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /apt/src/main/java/kale/dbinding/VmGenerateProcessor.java: -------------------------------------------------------------------------------- 1 | package kale.dbinding; 2 | 3 | import java.io.File; 4 | import java.util.Set; 5 | 6 | import javax.annotation.processing.AbstractProcessor; 7 | import javax.annotation.processing.RoundEnvironment; 8 | import javax.annotation.processing.SupportedAnnotationTypes; 9 | import javax.lang.model.element.Element; 10 | import javax.lang.model.element.TypeElement; 11 | import javax.tools.Diagnostic; 12 | 13 | /** 14 | * @author Kale 15 | * @date 2016/8/7 16 | */ 17 | @SupportedAnnotationTypes({"kale.dbinding.VmGenerator"}) 18 | public class VmGenerateProcessor extends AbstractProcessor { 19 | 20 | private static final String ROOT = System.getProperty("user.dir"); 21 | 22 | @Override 23 | public boolean process(Set annotations, RoundEnvironment roundEnv) { 24 | if (annotations.iterator().hasNext()) { 25 | TypeElement ele = annotations.iterator().next(); 26 | if (roundEnv.getElementsAnnotatedWith(ele).iterator().hasNext()) { 27 | Element next = roundEnv.getElementsAnnotatedWith(ele).iterator().next(); 28 | 29 | VmGenerator annotation = next.getAnnotation(VmGenerator.class); 30 | 31 | if (annotation != null) { 32 | String moduleDir = ROOT + File.separator + "app" + File.separator; 33 | 34 | // log(moduleDir); 35 | 36 | // String path = annotation.value(); 37 | GenViewModel.generateViewModel(moduleDir); 38 | } 39 | 40 | } 41 | } 42 | return false; 43 | } 44 | /* 45 | @Override 46 | public Set getSupportedAnnotationTypes() { 47 | return new HashSet<>(Collections.singletonList( 48 | VmGenerator.class.getCanonicalName())); 49 | }*/ 50 | 51 | private void log(String msg) { 52 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "======" + msg); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/kale/db/GameDetailActivity.java: -------------------------------------------------------------------------------- 1 | package kale.db; 2 | 3 | import android.graphics.BitmapFactory; 4 | import android.widget.Toast; 5 | 6 | import com.lzh.compiler.parceler.annotation.Arg; 7 | import com.lzh.compiler.parceler.annotation.Dispatcher; 8 | 9 | import kale.db.base.BaseActivity; 10 | import kale.db.databinding.GameDetailActivityBinding; 11 | import kale.dbinding.data.ObservableBitmap; 12 | import kale.dbinding.data.ObservableCharSequence; 13 | 14 | /** 15 | * @author Kale 16 | * @date 2016/1/27 17 | */ 18 | @Dispatcher 19 | public class GameDetailActivity extends BaseActivity { 20 | 21 | @Arg 22 | int picResId; 23 | 24 | @Arg 25 | String title; 26 | 27 | @Arg 28 | ObservableBitmap icon; 29 | 30 | @Arg 31 | ObservableCharSequence isLikedText; 32 | 33 | @Override 34 | protected int getLayoutResId() { 35 | return R.layout.game_detail_activity; 36 | } 37 | 38 | @Override 39 | protected void beforeSetViews() { 40 | b.setTitle(title); 41 | b.setIcon(icon); 42 | b.setIsLikedText(isLikedText); 43 | b.setEvent(viewEvents); 44 | } 45 | 46 | @Override 47 | protected void setViews() { 48 | changeText(); 49 | b.picIv.setImageBitmap(BitmapFactory.decodeResource(getResources(), picResId)); 50 | 51 | viewEvents.setOnClick(v -> { 52 | if (v == b.likeBtn) { 53 | Toast.makeText(GameDetailActivity.this, "Thank you~", Toast.LENGTH_SHORT).show(); 54 | isLikedText.set(GameItem.LIKED); 55 | changeText(); 56 | } 57 | }); 58 | } 59 | 60 | private void changeText() { 61 | boolean isLiked = isLikedText.get().equals(GameItem.LIKED); 62 | b.likeBtn.setTextColor(getResources().getColor(isLiked ? R.color.dark_gray : R.color.white)); 63 | b.likeBtn.setEnabled(!isLiked); 64 | } 65 | 66 | @Override 67 | protected void doTransaction() { 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/res/layout/user_detail_activity.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 22 | 23 | 24 | 25 | 30 | 31 | 40 | 41 | 50 | 51 |