-
不会像
MVC
一样导致Activity
中代码量巨大,也不会像MVP
一样出现大量的View
和Presenter
接口。项目结构更加低耦合。 -
更低的耦合把各个模块分开开发,分开测试,可以分给不同的开发人员来完成。
示例项目架构分析
1.MVVM组件化示例项目架构图
2.目录结构:
各模块和彼此之间的关系解释
lib_opensource
:第三方build.gradle
依赖,本项目主要有support、lifecycle、room、fresco、retrofit、okhttp、RxJava、ARouter
这些。
-lib_coremodel
: 存放MVVM
中的Model
和ViewModel
两个模块,就是数据的处理和数据与UI页面的绑定。依赖lib_opensource
库。
-lib_common
: 公共库,主要有各种base,各种ui组件,自定义组件,公用的Activity
、公用的Fragment
,和公用的utils
等等。依赖lib_coremodel
库。
-
module_girls
: 子功能模块,可以在library
和application
之间切换,自己可以是一个app也可以成为别的app的一个组件模块。组件化编译时为app,反之为module
。 -
module_news
: 新闻功能模块,可以在library
和application
之间切换,自己可以是一个app也可以成为别的app的一个组件模块。组件化编译时为app
,反之为module
-
app_universal
: 定制版本的app,组件化编译时module_girls
和module_news
为app,所以不能把这两个作为module加进来编译,所以组件化编译时app_universal
要依赖lib_common
库,反之就可以把module_girls
和module_news
作为module加进来编译。 -
app_specific
: 定制版本的app
,组件化编译时module_girls
和module_news
为app,所以不能把这两个作为module加进来编译,所以组件化编译时app_specific
要依赖lib_common
库,反之就可以把module_girls
和module_news
作为module
加进来编译
五:android MVVM框架实现 Robobinding
-
Android平台上有一些比较好的MVVM框架,其中用的比较多的是RoboBinding,Robobinding。
-
为了精简框架,RoboBinding移除了大量不必要的代码,比如addXXListener(),findViewById()等。
-
可以将难以测试的Android代码转换为普通的JUnit测试。
-
提供对象类型Cursor来替换 - 关系类型Cursor,因为我们已经习惯于操作对象 。
-
可以很容易的为任何自定义组件,第三方组件或Android widget编写属性绑定实现,简化代码,使项目易于维护
-----------------------------------------Robobiding 简单使用示例:----------------------------------------------
- View层(对应android xml文件)
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:bind=“http://robobinding.org/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=“org.robobinding.androidmvvm.MainActivity”
tools:ignore=“MissingPrefix”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
bind:text=“{hello}”/> //单向绑定,修改Model属性时,会自动反映到视图中(需要实体中提供相应的getHello(),setHello()方法)。
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Name:”/>
<EditText
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
bind:text=“${name}”/> //双向绑定,修改model属性时,会自动反映到视图中;反过来,修改视图内容,也会自动更改Model的相关属性。
<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Say Hello”
bind:onClick=“sayHello”/>
- Model层:PresentationModel()
public class PresentationModel implements HasPresentationModelChangeSupport {
private PresentationModelChangeSupport changeSupport;
private String name;
public PresentationModel() {
changeSupport = new PresentationModelChangeSupport(this);
}
public String getHello() {
return name + “: hello Android MVVM(Presentation Model)!”;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name; Log.d(“model”, "setName(),name: " + name);
}
public void sayHello(){
changeSupport.firePropertyChange(“hello”);
}
@Override
public PresentationModelChangeSupport getPresentationModelChangeSupport() {
return changeSupport;
}
}
**Controller层: **
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PresentationModel presentationModel = new PresentationModel();
ViewBinder viewBinder = createViewBinder();
View rootView = viewBinder.inflateAndBind(R.layout.activity_main, presentationModel);//将model和view进行绑定
setContentView(rootView);
}
private ViewBinder createViewBinder() {
BinderFactory reusableBinderFactory = new BinderFactoryBuilder().build();
return reusableBinderFactory.createViewBinder(this);
}
}
----------------------------------------------Robobiding 简单使用结束:------------------------------------------------
android官方支持的databinding框架使用
使用条件:
-
android studio 1.3及以上版本
-
gradle 2.2及以上版本
-
android plugin for gradle 1.3.0及以上版本
使用步骤:
- 在项目顶层build.gradle中添加以下依赖:
dependencies { classpath “com.android.databinding:dataBinder:1.0-rc1”}
- 在需要使用databinding的moudle中添加
apply plugin: ‘com.android.databinding’
注意
在项目编译过程中,会出现诸如以下错误:
错误一:
Error:Application and test application id cannot be the same: both are ‘com.fengjr.mobile’ for qihuDebugAndroidTest
此处要求test的application id和项目id不能一致,需将test 的id(即testApplicationId “com.fengjr.mobile"修改为"com.fengjr.mobile.test”)
示例
view 文件 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><variable
name=“stu”
type=“com.fengjr.mobile.databind.Student” /> //指明该view文件要绑定的数据模型
<LinearLayout
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@{stu.name}”/>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@{stu.addr}”/>
** Model文件:**
package com.fengjr.mobile.databind;
/** * Created by gaoge on 15/9/22. */
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
总结:
各行各样都会淘汰一些能力差的,不仅仅是IT这个行业,所以,不要被程序猿是吃青春饭等等这类话题所吓倒,也不要觉得,找到一份工作,就享受安逸的生活,你在安逸的同时,别人正在奋力的向前跑,这样与别人的差距也就会越来越遥远,加油,希望,我们每一个人,成为更好的自己。
-
BAT大厂面试题、独家面试工具包,
-
资料包括 数据结构、Kotlin、计算机网络、Framework源码、数据结构与算法、小程序、NDK、Flutter,
持续更新**
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-lcgIeHtL-1711909434867)]
总结:
各行各样都会淘汰一些能力差的,不仅仅是IT这个行业,所以,不要被程序猿是吃青春饭等等这类话题所吓倒,也不要觉得,找到一份工作,就享受安逸的生活,你在安逸的同时,别人正在奋力的向前跑,这样与别人的差距也就会越来越遥远,加油,希望,我们每一个人,成为更好的自己。
-
BAT大厂面试题、独家面试工具包,
-
资料包括 数据结构、Kotlin、计算机网络、Framework源码、数据结构与算法、小程序、NDK、Flutter,
[外链图片转存中…(img-KGCmGt78-1711909434867)]
[外链图片转存中…(img-bxteU6j3-1711909434868)]