Repository: goldze/MVVMHabit
Branch: master
Commit: ab2bf079815e
Files: 190
Total size: 566.8 KB
Directory structure:
gitextract_gppaw7p9/
├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── UpdateLog.md
├── app/
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src/
│ ├── androidTest/
│ │ └── java/
│ │ └── com/
│ │ └── goldze/
│ │ └── mvvmhabit/
│ │ └── ExampleInstrumentedTest.java
│ ├── main/
│ │ ├── AndroidManifest.xml
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── goldze/
│ │ │ └── mvvmhabit/
│ │ │ ├── app/
│ │ │ │ ├── AppApplication.java
│ │ │ │ ├── AppViewModelFactory.java
│ │ │ │ └── Injection.java
│ │ │ ├── binding/
│ │ │ │ └── twinklingrefreshlayout/
│ │ │ │ └── ViewAdapter.java
│ │ │ ├── data/
│ │ │ │ ├── DemoRepository.java
│ │ │ │ └── source/
│ │ │ │ ├── HttpDataSource.java
│ │ │ │ ├── LocalDataSource.java
│ │ │ │ ├── http/
│ │ │ │ │ ├── HttpDataSourceImpl.java
│ │ │ │ │ └── service/
│ │ │ │ │ └── DemoApiService.java
│ │ │ │ └── local/
│ │ │ │ └── LocalDataSourceImpl.java
│ │ │ ├── entity/
│ │ │ │ ├── DemoEntity.java
│ │ │ │ ├── FormEntity.java
│ │ │ │ └── SpinnerItemData.java
│ │ │ ├── ui/
│ │ │ │ ├── base/
│ │ │ │ │ ├── adapter/
│ │ │ │ │ │ └── BaseFragmentPagerAdapter.java
│ │ │ │ │ ├── fragment/
│ │ │ │ │ │ └── BasePagerFragment.java
│ │ │ │ │ └── viewmodel/
│ │ │ │ │ └── ToolbarViewModel.java
│ │ │ │ ├── form/
│ │ │ │ │ ├── FormFragment.java
│ │ │ │ │ └── FormViewModel.java
│ │ │ │ ├── login/
│ │ │ │ │ ├── LoginActivity.java
│ │ │ │ │ └── LoginViewModel.java
│ │ │ │ ├── main/
│ │ │ │ │ ├── DemoActivity.java
│ │ │ │ │ └── DemoViewModel.java
│ │ │ │ ├── network/
│ │ │ │ │ ├── NetWorkFragment.java
│ │ │ │ │ ├── NetWorkItemViewModel.java
│ │ │ │ │ ├── NetWorkViewModel.java
│ │ │ │ │ └── detail/
│ │ │ │ │ ├── DetailFragment.java
│ │ │ │ │ └── DetailViewModel.java
│ │ │ │ ├── rv_multi/
│ │ │ │ │ ├── MultiRecycleHeadViewModel.java
│ │ │ │ │ ├── MultiRecycleLeftItemViewModel.java
│ │ │ │ │ ├── MultiRecycleRightItemViewModel.java
│ │ │ │ │ ├── MultiRecycleViewFragment.java
│ │ │ │ │ └── MultiRecycleViewModel.java
│ │ │ │ ├── tab_bar/
│ │ │ │ │ ├── activity/
│ │ │ │ │ │ └── TabBarActivity.java
│ │ │ │ │ └── fragment/
│ │ │ │ │ ├── TabBar1Fragment.java
│ │ │ │ │ ├── TabBar2Fragment.java
│ │ │ │ │ ├── TabBar3Fragment.java
│ │ │ │ │ └── TabBar4Fragment.java
│ │ │ │ ├── viewpager/
│ │ │ │ │ ├── activity/
│ │ │ │ │ │ └── ViewPagerActivity.java
│ │ │ │ │ ├── adapter/
│ │ │ │ │ │ └── ViewPagerBindingAdapter.java
│ │ │ │ │ └── vm/
│ │ │ │ │ ├── ViewPagerItemViewModel.java
│ │ │ │ │ └── ViewPagerViewModel.java
│ │ │ │ └── vp_frg/
│ │ │ │ └── ViewPagerGroupFragment.java
│ │ │ └── utils/
│ │ │ ├── HttpsUtils.java
│ │ │ └── RetrofitClient.java
│ │ └── res/
│ │ ├── drawable/
│ │ │ └── login_clear_input.xml
│ │ ├── layout/
│ │ │ ├── activity_demo.xml
│ │ │ ├── activity_login.xml
│ │ │ ├── activity_tab_bar.xml
│ │ │ ├── fragment_base_pager.xml
│ │ │ ├── fragment_detail.xml
│ │ │ ├── fragment_form.xml
│ │ │ ├── fragment_multi_rv.xml
│ │ │ ├── fragment_network.xml
│ │ │ ├── fragment_tab_bar_1.xml
│ │ │ ├── fragment_tab_bar_2.xml
│ │ │ ├── fragment_tab_bar_3.xml
│ │ │ ├── fragment_tab_bar_4.xml
│ │ │ ├── fragment_viewpager.xml
│ │ │ ├── item_multi_head.xml
│ │ │ ├── item_multi_rv_left.xml
│ │ │ ├── item_multi_rv_right.xml
│ │ │ ├── item_network.xml
│ │ │ ├── item_viewpager.xml
│ │ │ └── layout_toolbar.xml
│ │ ├── values/
│ │ │ ├── attrs.xml
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── xml/
│ │ └── network_security_config.xml
│ └── test/
│ └── java/
│ └── com/
│ └── goldze/
│ └── mvvmhabit/
│ └── ExampleUnitTest.java
├── build.gradle
├── config.gradle
├── gradle/
│ └── wrapper/
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── mvvmhabit/
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src/
│ ├── androidTest/
│ │ └── java/
│ │ └── me/
│ │ └── goldze/
│ │ └── mvvmhabit/
│ │ └── ExampleInstrumentedTest.java
│ ├── main/
│ │ ├── AndroidManifest.xml
│ │ ├── java/
│ │ │ └── me/
│ │ │ └── goldze/
│ │ │ └── mvvmhabit/
│ │ │ ├── base/
│ │ │ │ ├── AppManager.java
│ │ │ │ ├── BaseActivity.java
│ │ │ │ ├── BaseApplication.java
│ │ │ │ ├── BaseFragment.java
│ │ │ │ ├── BaseModel.java
│ │ │ │ ├── BaseViewModel.java
│ │ │ │ ├── ContainerActivity.java
│ │ │ │ ├── IBaseView.java
│ │ │ │ ├── IBaseViewModel.java
│ │ │ │ ├── IModel.java
│ │ │ │ ├── ItemViewModel.java
│ │ │ │ ├── MultiItemViewModel.java
│ │ │ │ └── ViewModelFactory.java
│ │ │ ├── binding/
│ │ │ │ ├── command/
│ │ │ │ │ ├── BindingAction.java
│ │ │ │ │ ├── BindingCommand.java
│ │ │ │ │ ├── BindingConsumer.java
│ │ │ │ │ ├── BindingFunction.java
│ │ │ │ │ └── ResponseCommand.java
│ │ │ │ └── viewadapter/
│ │ │ │ ├── checkbox/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── edittext/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── image/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── listview/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── mswitch/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── radiogroup/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── recyclerview/
│ │ │ │ │ ├── DividerLine.java
│ │ │ │ │ ├── LayoutManagers.java
│ │ │ │ │ ├── LineManagers.java
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── scrollview/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── spinner/
│ │ │ │ │ ├── IKeyAndValue.java
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── swiperefresh/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── view/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── viewgroup/
│ │ │ │ │ ├── IBindingItemViewModel.java
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ ├── viewpager/
│ │ │ │ │ └── ViewAdapter.java
│ │ │ │ └── webview/
│ │ │ │ └── ViewAdapter.java
│ │ │ ├── bus/
│ │ │ │ ├── Messenger.java
│ │ │ │ ├── RxBus.java
│ │ │ │ ├── RxBusSubscriber.java
│ │ │ │ ├── RxSubscriptions.java
│ │ │ │ ├── WeakAction.java
│ │ │ │ └── event/
│ │ │ │ ├── SingleLiveEvent.java
│ │ │ │ └── SnackbarMessage.java
│ │ │ ├── crash/
│ │ │ │ ├── CaocConfig.java
│ │ │ │ ├── CaocInitProvider.java
│ │ │ │ ├── CustomActivityOnCrash.java
│ │ │ │ └── DefaultErrorActivity.java
│ │ │ ├── http/
│ │ │ │ ├── ApiDisposableObserver.java
│ │ │ │ ├── BaseResponse.java
│ │ │ │ ├── DownLoadManager.java
│ │ │ │ ├── ExceptionHandle.java
│ │ │ │ ├── NetworkUtil.java
│ │ │ │ ├── ResponseThrowable.java
│ │ │ │ ├── cookie/
│ │ │ │ │ ├── CookieJarImpl.java
│ │ │ │ │ └── store/
│ │ │ │ │ ├── CookieStore.java
│ │ │ │ │ ├── MemoryCookieStore.java
│ │ │ │ │ ├── PersistentCookieStore.java
│ │ │ │ │ └── SerializableHttpCookie.java
│ │ │ │ ├── download/
│ │ │ │ │ ├── DownLoadStateBean.java
│ │ │ │ │ ├── DownLoadSubscriber.java
│ │ │ │ │ ├── ProgressCallBack.java
│ │ │ │ │ └── ProgressResponseBody.java
│ │ │ │ └── interceptor/
│ │ │ │ ├── BaseInterceptor.java
│ │ │ │ ├── CacheInterceptor.java
│ │ │ │ ├── ProgressInterceptor.java
│ │ │ │ └── logging/
│ │ │ │ ├── I.java
│ │ │ │ ├── Level.java
│ │ │ │ ├── Logger.java
│ │ │ │ ├── LoggingInterceptor.java
│ │ │ │ └── Printer.java
│ │ │ ├── utils/
│ │ │ │ ├── CloseUtils.java
│ │ │ │ ├── ConvertUtils.java
│ │ │ │ ├── ImageUtils.java
│ │ │ │ ├── KLog.java
│ │ │ │ ├── MaterialDialogUtils.java
│ │ │ │ ├── RegexUtils.java
│ │ │ │ ├── RxUtils.java
│ │ │ │ ├── SDCardUtils.java
│ │ │ │ ├── SPUtils.java
│ │ │ │ ├── StringUtils.java
│ │ │ │ ├── ToastUtils.java
│ │ │ │ ├── Utils.java
│ │ │ │ ├── compression/
│ │ │ │ │ ├── Luban.java
│ │ │ │ │ ├── OnCompressListener.java
│ │ │ │ │ └── Preconditions.java
│ │ │ │ └── constant/
│ │ │ │ ├── MemoryConstants.java
│ │ │ │ ├── RegexConstants.java
│ │ │ │ └── TimeConstants.java
│ │ │ └── widget/
│ │ │ └── ControlDistributeLinearLayout.java
│ │ └── res/
│ │ ├── layout/
│ │ │ ├── activity_container.xml
│ │ │ └── customactivityoncrash_default_error_activity.xml
│ │ └── values/
│ │ ├── attrs.xml
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ └── strings.xml
│ └── test/
│ └── java/
│ └── me/
│ └── goldze/
│ └── mvvmhabit/
│ └── ExampleUnitTest.java
└── settings.gradle
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
# Auto detect text files and perform LF normalization
* text=auto
================================================
FILE: .gitignore
================================================
*.iml
.gradle
key.jks
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
/app/release
/mvvmhabit/bintray.gradle
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 goldze(曾宪泽)
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.
================================================
FILE: README.md
================================================
## 最新日志
**v4.0.0:2021年07月16日**
- 迁移AndroidX分支作为主线分支;
- 升级第三方框架依赖版本;
- 升级gradle插件版本支持;
- 优化框架代码,解决已知Bug;
- 修改文档说明。
#### [更多日志](./UpdateLog.md)
***
**注:**
[3.x:Support版(最后版本:3.1.6)](https://github.com/goldze/MVVMHabit/tree/20210716_v3.1.6_android)
[4.x:AndroidX版(最后版本:4.0.0)](https://github.com/goldze/MVVMHabit) 建议使用当前版本
> **原文地址:** [https://github.com/goldze/MVVMHabit](https://github.com/goldze/MVVMHabit)
②群
①群 (已满)
# MVVMHabit
##
目前,android流行的MVC、MVP模式的开发框架很多,然而一款基于MVVM模式开发框架却很少。**MVVMHabit是以谷歌DataBinding+LiveData+ViewModel框架为基础,整合Okhttp+RxJava+Retrofit+Glide等流行模块,加上各种原生控件自定义的BindingAdapter,让事件与数据源完美绑定的一款容易上瘾的实用性MVVM快速开发框架**。从此告别findViewById(),告别setText(),告别setOnClickListener()...
## 框架流程

## 框架特点
- **快速开发**
只需要写项目的业务逻辑,不用再去关心网络请求、权限申请、View的生命周期等问题,撸起袖子就是干。
- **维护方便**
MVVM开发模式,低耦合,逻辑分明。Model层负责将请求的数据交给ViewModel;ViewModel层负责将请求到的数据做业务逻辑处理,最后交给View层去展示,与View一一对应;View层只负责界面绘制刷新,不处理业务逻辑,非常适合分配独立模块开发。
- **流行框架**
[retrofit](https://github.com/square/retrofit)+[okhttp](https://github.com/square/okhttp)+[rxJava](https://github.com/ReactiveX/RxJava)负责网络请求;[gson](https://github.com/google/gson)负责解析json数据;[glide](https://github.com/bumptech/glide)负责加载图片;[rxlifecycle](https://github.com/trello/RxLifecycle)负责管理view的生命周期;与网络请求共存亡;[rxbinding](https://github.com/JakeWharton/RxBinding)结合databinding扩展UI事件;[rxpermissions](https://github.com/tbruyelle/RxPermissions)负责Android 6.0权限申请;[material-dialogs](https://github.com/afollestad/material-dialogs)一个漂亮的、流畅的、可定制的material design风格的对话框。
- **数据绑定**
满足google目前控件支持的databinding双向绑定,并扩展原控件一些不支持的数据绑定。例如将图片的url路径绑定到ImageView控件中,在BindingAdapter方法里面则使用Glide加载图片;View的OnClick事件在BindingAdapter中方法使用RxView防重复点击,再把事件回调到ViewModel层,实现xml与ViewModel之间数据和事件的绑定(框架里面部分扩展控件和回调命令使用的是@kelin原创的)。
- **基类封装**
专门针对MVVM模式打造的BaseActivity、BaseFragment、BaseViewModel,在View层中不再需要定义ViewDataBinding和ViewModel,直接在BaseActivity、BaseFragment上限定泛型即可使用。普通界面只需要编写Fragment,然后使用ContainerActivity盛装(代理),这样就不需要每个界面都在AndroidManifest中注册一遍。
- **全局操作**
1. 全局的Activity堆栈式管理,在程序任何地方可以打开、结束指定的Activity,一键退出应用程序。
2. LoggingInterceptor全局拦截网络请求日志,打印Request和Response,格式化json、xml数据显示,方便与后台调试接口。
3. 全局Cookie,支持SharedPreferences和内存两种管理模式。
4. 通用的网络请求异常监听,根据不同的状态码或异常设置相应的message。
5. 全局的异常捕获,程序发生异常时不会崩溃,可跳入异常界面重启应用。
6. 全局事件回调,提供RxBus、Messenger两种回调方式。
7. 全局任意位置一行代码实现文件下载进度监听(暂不支持多文件进度监听)。
8. 全局点击事件防抖动处理,防止点击过快。
## 1、准备工作
> 网上的很多有关MVVM的资料,在此就不再阐述什么是MVVM了,不清楚的朋友可以先去了解一下。[todo-mvvm-live](https://github.com/googlesamples/android-architecture/tree/todo-mvvm-live)
### 1.1、启用databinding
在主工程app的build.gradle的android {}中加入:
```gradle
dataBinding {
enabled true
}
```
### 1.2、依赖Library
从远程依赖:
在根目录的build.gradle中加入
```gradle
allprojects {
repositories {
...
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
```
在主项目app的build.gradle中依赖
```gradle
dependencies {
...
implementation 'com.github.goldze:MVVMHabit:4.0.0'
}
```
或
下载例子程序,在主项目app的build.gradle中依赖例子程序中的**mvvmhabit**:
```gradle
dependencies {
...
implementation project(':mvvmhabit')
}
```
### 1.3、配置config.gradle
如果不是远程依赖,而是下载的例子程序,那么还需要将例子程序中的config.gradle放入你的主项目根目录中,然后在根目录build.gradle的第一行加入:
```gradle
apply from: "config.gradle"
```
**注意:** config.gradle中的
android = [] 是你的开发相关版本配置,可自行修改
support = [] 是你的support相关配置,可自行修改
dependencies = [] 是依赖第三方库的配置,可以加新库,但不要去修改原有第三方库的版本号,不然可能会编译不过
### 1.4、配置AndroidManifest
添加权限:
```xml
```
配置Application:
继承**mvvmhabit**中的BaseApplication,或者调用
```java
BaseApplication.setApplication(this);
```
来初始化你的Application
可以在你的自己AppApplication中配置
```java
//是否开启日志打印
KLog.init(true);
//配置全局异常崩溃操作
CaocConfig.Builder.create()
.backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式
.enabled(true) //是否启动全局异常捕获
.showErrorDetails(true) //是否显示错误详细信息
.showRestartButton(true) //是否显示重启按钮
.trackActivities(true) //是否跟踪Activity
.minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
.errorDrawable(R.mipmap.ic_launcher) //错误图标
.restartActivity(LoginActivity.class) //重新启动后的activity
//.errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
//.eventListener(new YourCustomEventListener()) //崩溃后的错误监听
.apply();
```
## 2、快速上手
### 2.1、第一个Activity
> 以大家都熟悉的登录操作为例:三个文件**LoginActivty.java**、**LoginViewModel.java**、**activity_login.xml**
##### 2.1.1、关联ViewModel
在activity_login.xml中关联LoginViewModel。
```xml
.....
```
> variable - type:类的全路径 variable - name:变量名
##### 2.1.2、继承BaseActivity
LoginActivity继承BaseActivity
```java
public class LoginActivity extends BaseActivity {
//ActivityLoginBinding类是databinding框架自定生成的,对activity_login.xml
@Override
public int initContentView(Bundle savedInstanceState) {
return R.layout.activity_login;
}
@Override
public int initVariableId() {
return BR.viewModel;
}
@Override
public LoginViewModel initViewModel() {
//View持有ViewModel的引用,如果没有特殊业务处理,这个方法可以不重写
return ViewModelProviders.of(this).get(LoginViewModel.class);
}
}
```
> 保存activity_login.xml后databinding会生成一个ActivityLoginBinding类。(如果没有生成,试着点击Build->Clean Project)
BaseActivity是一个抽象类,有两个泛型参数,一个是ViewDataBinding,另一个是BaseViewModel,上面的ActivityLoginBinding则是继承的ViewDataBinding作为第一个泛型约束,LoginViewModel继承BaseViewModel作为第二个泛型约束。
重写BaseActivity的二个抽象方法
initContentView() 返回界面layout的id
initVariableId() 返回变量的id,对应activity_login中name="viewModel",就像一个控件的id,可以使用R.id.xxx,这里的BR跟R文件一样,由系统生成,使用BR.xxx找到这个ViewModel的id。
选择性重写initViewModel()方法,返回ViewModel对象
```java
@Override
public LoginViewModel initViewModel() {
//View持有ViewModel的引用,如果没有特殊业务处理,这个方法可以不重写
return ViewModelProviders.of(this).get(LoginViewModel.class);
}
```
**注意:** 不重写initViewModel(),默认会创建LoginActivity中第二个泛型约束的LoginViewModel,如果没有指定第二个泛型,则会创建BaseViewModel
##### 2.1.3、继承BaseViewModel
LoginViewModel继承BaseViewModel
```java
public class LoginViewModel extends BaseViewModel {
public LoginViewModel(@NonNull Application application) {
super(application);
}
....
}
```
BaseViewModel与BaseActivity通过LiveData来处理常用UI逻辑,即可在ViewModel中使用父类的showDialog()、startActivity()等方法。在这个LoginViewModel中就可以尽情的写你的逻辑了!
> BaseFragment的使用和BaseActivity一样,详情参考Demo。
### 2.2、数据绑定
> 拥有databinding框架自带的双向绑定,也有扩展
##### 2.2.1、传统绑定
绑定用户名:
在LoginViewModel中定义
```java
//用户名的绑定
public ObservableField userName = new ObservableField<>("");
```
在用户名EditText标签中绑定
```xml
android:text="@={viewModel.userName}"
```
这样一来,输入框中输入了什么,userName.get()的内容就是什么,userName.set("")设置什么,输入框中就显示什么。
**注意:** @符号后面需要加=号才能达到双向绑定效果;userName需要是public的,不然viewModel无法找到它。
点击事件绑定:
在LoginViewModel中定义
```java
//登录按钮的点击事件
public View.OnClickListener loginOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
```
在登录按钮标签中绑定
```xml
android:onClick="@{viewModel.loginOnClick}"
```
这样一来,用户的点击事件直接被回调到ViewModel层了,更好的维护了业务逻辑
这就是强大的databinding框架双向绑定的特性,不用再给控件定义id,setText(),setOnClickListener()。
**但是,光有这些,完全满足不了我们复杂业务的需求啊!MVVMHabit闪亮登场:它有一套自定义的绑定规则,可以满足大部分的场景需求,请继续往下看。**
##### 2.2.2、自定义绑定
还拿点击事件说吧,不用传统的绑定方式,使用自定义的点击事件绑定。
在LoginViewModel中定义
```java
//登录按钮的点击事件
public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
@Override
public void call() {
}
});
```
在activity_login中定义命名空间
```xml
xmlns:binding="http://schemas.android.com/apk/res-auto"
```
在登录按钮标签中绑定
```xml
binding:onClickCommand="@{viewModel.loginOnClickCommand}"
```
这和原本传统的绑定不是一样吗?不,这其实是有差别的。使用这种形式的绑定,在原本事件绑定的基础之上,带有防重复点击的功能,1秒内多次点击也只会执行一次操作。如果不需要防重复点击,可以加入这条属性
```xml
binding:isThrottleFirst="@{Boolean.TRUE}"
```
那这功能是在哪里做的呢?答案在下面的代码中。
```java
//防重复点击间隔(秒)
public static final int CLICK_INTERVAL = 1;
/**
* requireAll 是意思是是否需要绑定全部参数, false为否
* View的onClick事件绑定
* onClickCommand 绑定的命令,
* isThrottleFirst 是否开启防止过快点击
*/
@BindingAdapter(value = {"onClickCommand", "isThrottleFirst"}, requireAll = false)
public static void onClickCommand(View view, final BindingCommand clickCommand, final boolean isThrottleFirst) {
if (isThrottleFirst) {
RxView.clicks(view)
.subscribe(new Consumer