Android MVVM 架构项目教程
android-mvvm-architectureThis repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView项目地址:https://gitcode.com/gh_mirrors/an/android-mvvm-architecture
项目介绍
Android MVVM 架构项目是一个基于 Model-View-ViewModel 设计模式的开源项目,旨在帮助开发者构建可维护、可扩展和高性能的 Android 应用。该项目提供了一套完整的架构模板,包括数据层、视图层和视图模型层的实现示例。
项目快速启动
克隆项目
首先,克隆项目到本地:
git clone https://github.com/janishar/android-mvvm-architecture.git
导入项目
使用 Android Studio 打开克隆下来的项目文件夹。
配置依赖
在 build.gradle
文件中添加必要的依赖项:
dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.room:room-runtime:2.2.6'
annotationProcessor 'androidx.room:room-compiler:2.2.6'
}
运行项目
点击 Android Studio 中的运行按钮,选择一个模拟器或连接的设备,运行项目。
应用案例和最佳实践
应用案例
假设我们正在构建一个简单的待办事项列表应用,用户可以添加和查看任务。以下是关键部分的实现:
数据层
定义数据源、仓库和业务逻辑类:
data class Task(val id: Int, val title: String, val description: String, val isCompleted: Boolean)
class TaskRepository {
private val tasks = mutableListOf<Task>()
fun addTask(task: Task) {
tasks.add(task)
}
fun getAllTasks(): List<Task> {
return tasks.toList()
}
}
视图模型层
定义视图模型类:
class TaskViewModel : ViewModel() {
private val repository = TaskRepository()
val tasks: LiveData<List<Task>> = MutableLiveData()
fun addTask(task: Task) {
repository.addTask(task)
(tasks as MutableLiveData).value = repository.getAllTasks()
}
}
视图层
定义视图布局和活动:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/taskTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Task Title" />
<EditText
android:id="@+id/taskDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Task Description" />
<Button
android:id="@+id/addTaskButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Task" />
<ListView
android:id="@+id/taskList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
class TodoListActivity : AppCompatActivity() {
private lateinit var viewModel: TaskViewModel
private lateinit var binding: ActivityTodoListBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityTodoListBinding.inflate(layoutInflater)
setContentView(binding.root)
viewModel = ViewModelProvider(this).get(TaskViewModel::class.java)
binding.viewModel = viewModel
binding.lifecycleOwner = this
binding.addTaskButton.setOnClickListener {
val title = binding.taskTitle.text.toString()
val description = binding.taskDescription.text.toString()
viewModel.addTask(Task(0, title, description, false))
}
viewModel.tasks.observe(this, Observer { tasks ->
android-mvvm-architectureThis repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView项目地址:https://gitcode.com/gh_mirrors/an/android-mvvm-architecture