DBinding是一个基于DataBinding的巧妙、灵活的mvvm框架。解决了两个页面的数据自动同步,跨线程更新UI等疑难杂症。意在未开发者提供一个结构清晰、方便分工合作、bug率低、代码量少的轻量级框架。
##一、优点
- 完全能代替findViewById,自动建立layout文件的java抽象类
- 提供了完全代替R.id的方案,解决多个view共用同一id而出现的定位难的问题
- 不用担心线程切换的问题,更新UI的操作会自动移动到主线程中执行
- 两个页面的数据可以共通,减少了乏味的多页面回调
- 提供了能代替部分
onActivityResult、广播代码的方案(不用定义key值了) - 分层清晰,UI层和逻辑层耦合度降低,能快速定位问题,真正能减少bug量
- 数据自动绑定,直接对ViewData进行操作即可更新UI
- 可通过插件自动生成ViewModel,0.9秒内可建立可供一个项目用的所有ViewModel
- 支持对一个或多个数据的监听,可极细力度地完成ui逻辑
- 减少类的全局变量,有一个binding类就可以了
- 节约定义view变量的时间,不用思考某个view到底应该定义在局部还是全局的
另:除了以上优点外,就没啥牛逼的地方了。。。
##三、添加依赖(暂未完成)
1.在项目外层的build.gradle中添加JitPack仓库
repositories {
maven {
url "https://jitpack.io"
}
}
2.在项目中添加依赖
compile 'com.github.tianzhijiexian:DBinding:Latest release''
##四、已知问题 ①因为增加了一个viewModel,所以可能会有一点点重。但是相比起databinding推荐的xml中写java逻辑的写法来看,此框架的复杂度要低很多。
②目前对于android中默认属性的支持还不是很完全,但大家可以共同完善它。
完善的方式:
- 通过自定义属性对的方式对插件进行扩展
- 对pluginLib这个module中的kale.dbinding.parser.TypeFinder中添加case代码,并提交pr
- 对dbindingTest这个module中TypeTest进行测试,看是否有未支持的属性
##五、使用方式 1.编写Layout文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<!-- 定义viewData: private org.kale.vd.UserViewModel user -->
<variable type="org.kale.vd.UserViewModel" name="user"/>
</data>
<!-- 绑定数据: textView.setText(user.name) -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="user.name"/>
</layout> 2.编写java代码:
Activity:
private UserViewModel mUserVm = new UserViewModel();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBinding.bindViewModel(this, R.layout.activity_main, mUserVm); // 将vm和layout进行绑定
mUserVm.setName("漩涡鸣人"); // textview中就会自动渲染出文字了
}##六、详尽文档
##开发者
Jack Tony: developer_kale@foxmail.com
Copyright 2015 Jack Tony
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.