android mvvm实例,一个快速以MVVM开始做项目的框架 MVVMQuick

MVVMQuick

旨在快速以MVVM开始开发项目,其内集成了dagger2、okhttp3、retrofit、BaseRecyclerViewAdapterHelper、lifecycle等常用的框架和一些工具。

将大部分逻辑抽象,希望可以通过几步简单的配置,即可省去重复逻辑的代码。

Note

目前还在测试开发中,有兴趣的朋友可以体验体验给给建议哦!如要运用到项目中请自行下载项目导入mvvm-quik模块,也可直接以该项目开始开发。

该项目参考:

dependencies

implementation 'com.github.xujiaji:mvvm-quick:0.0.2'

Use

1.定义ViewModel,继承MQViewModel

@Singleton

public class ProjectListViewModel extends MQViewModel

{

...

}

2.布局文件fragment_project_list以DataBinding的写法

xmlns:app="http://schemas.android.com/apk/res-auto">

name="projectListViewModel"

type="com.xujiaji.learnmvvm.module.projectlist.ProjectListViewModel"/>

...

3.ui中的写法,将DataBinding和ViewModel作为泛型配置,MQFragment会自动为您自动实例化

@ActivityScoped

public class ProjectListFragment extends MQFragment

{

@Inject

Lazy mAdapter;

@Inject

public ProjectListFragment() {}

@Override

public void onBinding(FragmentProjectListBinding binding)

{

... 当Binding初始化后会调用

}

@Override

public void onObserveViewModel(ProjectListViewModel viewModel)

{

... 当ViewModel初始化后会调用(在Binding之后初始化)

}

}

4.在ViewModelSubComponent中添加配置,提供ViewModel实例(实例提供通过Dagger实现)

@Subcomponent

public interface ViewModelSubComponent

{

...

Lazy projectListViewModel(); //add

}

5.在AppModule的providesViewModel方法中添加配置

@Singleton

@Provides

static Map, Callable>> providesViewModel(ViewModelSubComponent.Builder viewModelSubComponent)

{

...

creators.put(ProjectListViewModel.class, vmsc::projectListViewModel);//add

return creators;

}

License

Copyright 2018 XuJiaji

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

你可能感兴趣的:(android,mvvm实例)