Jetpack
Jetpack 是一个由多个库组成的套件,可帮助开发者遵循最佳做法,减少样板代码并编写可在各种 Android 版本和设备中一致运行的代码,让开发者精力集中编写重要的代码。
Android Architecture Component (AAC)。
文末有福利!!!
官方推荐架构
请注意,每个组件仅依赖于其下一级的组件。例如,Activity 和 Fragment 仅依赖于视图模型。存储区是唯一依赖于其他多个类的类;在本例中,存储区依赖于持久性数据模型和远程后端数据源。
MVVM
MVVM即Model - View - ViewModel的缩写,它的出现是为了将图形界面与业务逻辑,数据模型进行解耦。
MVVM也是Google推崇的一种Android项目架构模型。
之前学习的Jetpack组建,大部分都是为了能够更好地架构MVVM应用程序而设计的。
API接口
接口:api.github.com/users/yaoxi…
工程结构
bean:实体类。
api:网络请求接口。
repository:仓储层。用于存放Room数据,网络数据,本地数据等。
viewmodel:从仓储层获取数据,不需要关心数据来源。
view:Activity,Fragment和布局文件,用会用到DataBinding组件
dao:Room数据库操作
application:实例化全局文件和获取全局上下文。
bindingAdapter:放一些
添加依赖
1
2
3implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'de.hdodenhof:circleimageview:3.0.1'
搭建项目
通过获取GitHub API获取个人信息进行展示。
1. 定义User实体类
1
2
3
4
5
6
7
8
9
10
11
12
13@Entity(tableName = "user") data class User( @PrimaryKey @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER) var id: Int, @ColumnInfo(name = "login", typeAffinity = ColumnInfo.TEXT) var login: String, @ColumnInfo(name = "name", typeAffinity = ColumnInfo.TEXT) var name: String?, @ColumnInfo(name = "avatar_url", typeAffinity = ColumnInfo.TEXT) @SerializedName("avatar_url")var avatar: String?, @ColumnInfo(name = "blog", typeAffinity = ColumnInfo.TEXT) var blog: String, @ColumnInfo(name = "company", typeAffinity = ColumnInfo.TEXT) var company: String?, @ColumnInfo(name = "bio", typeAffinity = ColumnInfo.TEXT) var bio: String?, @ColumnInfo(name = "location", typeAffinity = ColumnInfo.TEXT) var location: String?, @ColumnInfo(name = "htmlUrl", typeAffinity = ColumnInfo.TEXT) @SerializedName("html_url") var htmlUrl: String? )
2. 定义Dao类
1
2
3
4
5
6
7
8
9
10
11
12@Dao interface UserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertUser(user: User) @Delete fun deleteUser(user: User) @Query("select * from user where login =:name") fun getUserByName(name: String): LiveData<User> }
3. 定义DataBase类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23@Database(entities = [User::class], version =7) abstract class AppDatabase : RoomDatabase() { abstract fun userDao(): UserDao companion object { private var instance: AppDatabase? = null @Synchronized fun getDatabase(context: Context): AppDatabase { instance?.let { return it } return Room.databaseBuilder( context.applicationContext, AppDatabase::class.java, "user_db" ).fallbackToDestructiveMigration().build().apply { instance = this } } } }
4. 定义API接口
1
2
3
4
5interface Api { @GET("users/{userName}") fun getUser(@Path("userName") userName: String): Call<User> }
5. 定义Retrofit访问网络
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15object RetrofitClient { private const val BASE_URL = "https://api.github.com/" var retrofit: Retrofit init { retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()) .build() } fun getApi(): Api? { return retrofit.create(Api::class.java) } }
6. 定义Application类
1
2
3
4
5
6
7
8
9
10
11class MyApplication : Application() { companion object { lateinit var context: Context } override fun onCreate() { super.onCreate() context = applicationContext } }
7. 定义Repository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30object UserRepository { var userDao: UserDao = AppDatabase.getDatabase(MyApplication.context).userDao() fun getUser(name: String): LiveData<User> { refresh(name) return userDao.getUserByName(name) } fun refresh(name: String) { RetrofitClient.getApi()?.getUser(name)?.enqueue(object : Callback<User> { override fun onResponse(call: Call<User>, response: Response<User>) { if (response.body() != null) { insertUser(response.body()!!) } } override fun onFailure(call: Call<User>, t: Throwable) { Log.d("UserRepository", "onFailure$t") } }) } fun insertUser(user: User) { thread { userDao.insertUser(user) } } }
8. 定义ViewModel
1
2
3
4
5
6class MvvmViewModel : ViewModel() { val userName = "yaoxin521123" fun getUser() = UserRepository.getUser(userName) fun refresh() = UserRepository.refresh(userName) }
9. 绘制xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95<?xml version="1.0" encoding="utf-8"?> <layout xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="user" type="com.yx.androidseniorpreparetest.eighth.bean.User" /> </data> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/srl_SwipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".eighth.MvvmActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <de.hdodenhof.circleimageview.CircleImageView android:layout_width="95dp" android:layout_height="95dp" android:layout_gravity="center" android:layout_marginTop="20dp" app:image="@{user.avatar}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.name}" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.login}" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.blog}" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.company}" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.bio}" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.location}" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:text="@{user.htmlUrl}" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout </layout>
10. 在Activity触发事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class MvvmActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding = DataBindingUtil.setContentView<ActivityMvvmBinding>(this, R.layout.activity_mvvm) val viewModel = ViewModelProviders.of(this).get(MvvmViewModel::class.java) viewModel.getUser().observe(this, { if (it != null) { binding.user = it } }) binding.srlSwipeRefreshLayout.setOnRefreshListener { viewModel.refresh() binding.srlSwipeRefreshLayout.isRefreshing = false } } }
11. 定义BindingAapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class BindingAdapter { companion object { @JvmStatic @BindingAdapter(value = ["image", "defaultImageResource"], requireAll = false) fun setImage(imageView: ImageView, imageUrl: String?, imageResource: Int) { if (!TextUtils.isEmpty(imageUrl)) { Picasso.get() .load(imageUrl) .placeholder(R.drawable.ic_launcher_background) .error(R.drawable.ic_launcher_background) .into(imageView) } else { imageView.setImageResource(imageResource) } } } }
结语:后续会持续更新哦,喜欢的话点赞关注一下吧。
最后
按照国际惯例,给大家分享一套十分好用的Android进阶资料:《全网最全Android开发笔记》。
整个笔记一共8大模块、729个知识点,3382页,66万字,可以说覆盖了当下Android开发最前沿的技术点,和阿里、腾讯、字节等等大厂面试看重的技术。
好啦,这份资料就给大家介绍到这了,有需要详细文档的小伙伴,可以微信扫下方二维码回复JJ免费领取哈~
因为所包含的内容足够多,所以,这份笔记不仅仅可以用来当学习资料,还可以当工具书用。
如果你需要了解某个知识点,不管是Shift+F 搜索,还是按目录进行检索,都能用最快的速度找到你要的内容。
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照整个知识体系编排的。
(一)架构师必备Java基础
1、深入理解Java泛型
2、注解深入浅出
3、并发编程
4、数据传输与序列化
5、Java虚拟机原理
6、高效IO
……
(二)设计思想解读开源框架
1、热修复设计
2、插件化框架设计
3、组件化框架设计
4、图片加载框架
5、网络访问框架设计
6、RXJava响应式编程框架设计
……
(三)360°全方位性能优化
1、设计思想与代码质量优化
2、程序性能优化
- 启动速度与执行效率优化
- 布局检测与优化
- 内存优化
- 耗电优化
- 网络传输与数据储存优化
- APK大小优化
3、开发效率优化
- 分布式版本控制系统Git
- 自动化构建系统Gradle
……
(四)Android框架体系架构
1、高级UI晋升
2、Android内核组件
3、大型项目必备IPC
4、数据持久与序列化
5、Framework内核解析
……
(五)NDK模块开发
1、NDK开发之C/C++入门
2、JNI模块开发
3、Linux编程
4、底层图片处理
5、音视频开发
6、机器学习
……
(六)Flutter学习进阶
1、Flutter跨平台开发概述
2、Windows中Flutter开发环境搭建
3、编写你的第一个Flutter APP
4、Flutter Dart语言系统入门
……
(七)微信小程序开发
1、小程序概述及入门
2、小程序UI开发
3、API操作
4、购物商场项目实战
……
(八)kotlin从入门到精通
1、准备开始
2、基础
3、类和对象
4、函数和lambda表达式
5、其他
……
好啦,这份资料就给大家介绍到这了,有需要详细文档的小伙伴,可以微信扫下方二维码回复JJ免费领取哈~
最后
以上就是要减肥哑铃最近收集整理的关于Android安卓进阶技巧之Kotlin结合Jetpack构建MVVM文末有福利!!!最后的全部内容,更多相关Android安卓进阶技巧之Kotlin结合Jetpack构建MVVM文末有福利内容请搜索靠谱客的其他文章。
发表评论 取消回复