理解android-architecture mvvm

https://github.com/googlesamples/android-architecture
todo-mvvm-databinding
在这里插入图片描述

The ViewModel in the MVVM architecture plays a similar role to the Presenter in the MVP architecture. The two architectures differ in the way that the View communicates with the ViewModel or Presenter respectively:

When the app modifies the ViewModel in the MVVM architecture, the View is automatically updated by a library or framework. You can’t update the View directly from the ViewModel, as the ViewModel doesn’t have access to the necessary reference.
You can however update the View from the Presenter in an MVP architecture as it has the necessary reference to the View. When a change is necessary, you can explicitly call the View from the Presenter to update it. In this project, you use layout files to bind observable fields in the ViewModel to specific UI elements such as a TextView, or ImageView. The Data Binding Library ensures that the View and ViewModel remain in sync bi-directionally as illustrated by the following diagram.

在这里插入图片描述
todo-mvvm-live

Live events
A new SingleLiveEvent class is created, which extends MutableLiveData so it’s lifecycle-aware. It’s used for communication between ViewModels and UI views (activities and fragments).

Instead of holding data, it dispatches data once. This is important to prevent events being fired after a rotation, for example.

A convenient use for this is navigation. There is no reference to the View from a ViewModel so the communication between them must happen via a subscription. ViewModels expose events like openTaskEvent and views subscribe to them. For example:

private void subscribeToNavigationChanges(TaskDetailViewModel viewModel) {
    // The activity observes the navigation commands in the ViewModel
    viewModel.getEditTaskCommand().observe(this, new Observer<Void>() {
        @Override
        public void onChanged(@Nullable Void _) {
            TaskDetailActivity.this.onStartEditTask();
        }
    });
    viewModel.getDeleteTaskCommand().observe(this, new Observer<Void>() {
        @Override
        public void onChanged(@Nullable Void _) {
            TaskDetailActivity.this.onTaskDeleted();
        }
    });
}

Snackbar
To show a Snackbar, you must use a static call to pass a view object:

Snackbar.make(View coordinatorLayout, String text, int length).show();

A ViewModel, however, doesn’t have the necessary reference to the view hierarchy. Instead, you can manually subscribe the snackbar to a Snackbar event. In this case the subscription is made to a SnackbarMessage which extends a SingleLiveEvent and takes a string resource ID (an integer). There’s only one snackbar and there should only be one active observer at a time. Messages are only shown once.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android MVVM Fragment Demo是一个演示应用程序,用于展示如何在Android应用中使用MVVM架构和Fragment进行开发。 MVVM(Model-View-ViewModel)是一种软件架构模式,它的目的是将应用程序的界面逻辑与业务逻辑分离。在MVVM中,视图模型(ViewModel)是连接视图(View)和数据模型(Model)之间的桥梁。它负责管理视图的状态和显示逻辑,并将用户的操作转化为对数据模型的请求。 在这个Demo中,主要使用了Android Architecture Components中的ViewModel、LiveData和Data Binding库来实现MVVM架构。它的主要结构是由一个Activity和多个Fragment组成。 Activity扮演着整个应用程序的容器,负责管理Fragment的加载和切换。每个Fragment都对应一个视图和一个视图模型。视图模型通过LiveData和Data Binding与视图进行双向数据绑定,实现了数据的自动更新。 这个Demo还演示了如何使用ViewModel来进行数据的获取和处理,以及如何使用LiveData来观察数据的变化。当数据发生变化时,LiveData会自动通知视图进行更新。同时,它还展示了如何使用Data Binding来简化视图与数据模型之间的绑定操作,减少了手动操作视图的代码。 总体来说,Android MVVM Fragment Demo是一个能够帮助开发者理解和实践MVVM架构的示例应用程序。通过学习这个Demo,开发者可以更好地掌握MVVM的开发思想和相关技术,提高开发效率和代码质量。 ### 回答2: Android MVVM(Model-View-ViewModel)是一种用于构建Android应用程序的架构模式。MVVM模式的核心思想是将应用程序的逻辑与UI分离,使得开发者能够更好地管理和维护代码。 一个MVVM结构的Android应用程序通常由包含视图、视图模型和数据模型的三个主要组件组成。 Fragment是Android中一个常用的UI组件,可用于构建可重用且独立的UI模块。MVVM模式在Fragment中的应用与其他类型的视图组件相似,包括Activity、Dialog等。 在一个MVVM Fragment Demo中,通常会有一个Fragment承载UI视图,一个ViewModel处理业务逻辑和数据绑定,以及一个Model提供数据。 MVVM Fragment Demo的开发步骤通常如下: 1. 创建一个包含Fragment的布局文件,用于展示UI视图。 2. 创建一个Fragment类,继承自Android的Fragment基类。在Fragment中,你可以实现界面的初始化和相关的生命周期方法。 3. 创建一个ViewModel类,用于处理业务逻辑和数据绑定。在ViewModel中,你可以定义数据模型和相关的方法,以处理与UI交互的逻辑。 4. 在Fragment中,通过ViewModel对象,将数据和UI视图进行绑定。你可以使用Android的Data Binding库来简化数据绑定的过程。 5. 在Model中,你可以定义相关的数据源和获取数据的方法。这些数据可以通过网络请求、数据库查询等方式获取。 6. 将Model中的数据传递给ViewModel,并在ViewModel中进行处理和转换。 7. 最后,将处理后的数据传递给Fragment中的UI视图,更新UI。 通过使用MVVM架构模式,你可以更好地管理Android应用程序的代码,并实现UI和业务逻辑之间的分离。同时,MVVM的数据绑定机制可以简化UI更新的过程,提高开发效率。 总之,一个Android MVVM Fragment Demo将帮助你理解和实践MVVM架构模式在Android应用开发中的应用,提高代码的可维护性和可测试性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值