写在前面的话
前面学习Android架构 MVC 以及MVP 有架构这块有一个简单的认识和总结 本来打算在MVP这块终止 过渡到MVVM的学习。结果参考google todoapp等几个demo。发现这方面东西很多。发现了很多新东西 譬如
我们常用的是OOP编程(即 面向对象编程),那么 什么是 AOP编程(Aspect Oriented Programming 中译 面向切向编程)? 它与插件化开发 组件化开发 有什么联系?(关于插件化 组件化等这方面我还没有认真了解过 只是在了解热修复的时候 大概听说过 最初热修复出来是为了app模块化升级以及解决少量bug 总是对这一块 不了解 所以表达的很模糊 后面学习之后 会做更正 // todo)。
Guide to App Architecture Google 官方推出应用开发架构指南里面提到的 A collection of samples using the Architecture Components:
Room
Lifecycle-aware components
ViewModels
LiveData
...
由上 存在很多疑惑 慢慢学习 并揭开这些面纱。参考 android-architecture-components 继续在 ONE上面做改动.
android-architecture-components
Android Architecture Components Basic Sample 包含三部分:
Room
ViewModels
LiveData
下载demo 编译 BasicSample正常 运行报错: Error:(31, 17) 警告: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation
annotation processor argument OR set exportSchema to false. google 一下 解决方案 如 issues 12
<code>
Try to export the schema by adding this to your app/build.gradle
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}
}
}
</code>
P.S. 看别人真是一种享受 细节:
<code>
ext {
buildToolsVersion = "25.0.2"
supportLibVersion = "25.3.1"
runnerVersion = "0.5"
rulesVersion = "0.5"
espressoVersion = "2.2.2"
archLifecycleVersion = "1.0.0-alpha1"
archRoomVersion = "1.0.0-alpha1"
}
引用: compile 'com.android.support:appcompat-v7:'+rootProject.supportLibVersion;
</code>
环境配置
首先在工程根目录的build.gradle中添加一下内容:
<code>
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
</code>
<code>
// For Lifecycles, LiveData, and ViewModel Room
compile 'android.arch.lifecycle:extensions:' + rootProject.archLifecycleVersion;
compile 'android.arch.persistence.room:runtime:' + rootProject.archRoomVersion;
annotationProcessor "android.arch.lifecycle:compiler:" + rootProject.archLifecycleVersion;
annotationProcessor "android.arch.persistence.room:compiler:" + rootProject.archRoomVersion;
ext {
buildToolsVersion = "25.0.2"
supportLibVersion = "25.2.0"
archLifecycleVersion = "1.0.0-alpha1"
archRoomVersion = "1.0.0-alpha1"
}
</code>
具体见代码 SampleReadingFragment中
<code>
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleReadingViewModel viewModel = ViewModelProviders.of(this).get(SampleReadingViewModel.class);
subscribeUi(viewModel);
}
private void subscribeUi(SampleReadingViewModel viewModel) {
viewModel.getData().observe(this, dataEntities -> {
if(dataEntities!=null){
mList = dataEntities;
fragmentAdapter.setData(mList);
}
});
}
</code>
注意 public SampleReadingViewModel(Application app) 里面的Application不要写成了你自己集成了那个。(踩坑。。运行了好一会 找不到原因)
关于Lifecycle更多了解 后续会陆续补充。个人感觉将MVP更加简化的同时 将各个模块之间完全分离开来 具体见项目ONE SampleReadingFragment
参考:
developer lifecycle
developer common_architectural_principles
BasicSample
欢迎加群:
QQ 群 521039620
提意见:
QQ:1831808374
github