├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── build.gradle ├── display ├── banner.png ├── dis.gif └── mvp.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties ├── mvp.puml ├── sample ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── xujiaji │ │ └── sample │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── data.zip │ ├── java │ └── io │ │ └── xujiaji │ │ └── sample │ │ ├── App.java │ │ ├── adapter │ │ └── HomeAdapter.java │ │ ├── contract │ │ ├── CodeContract.java │ │ └── HomeContract.java │ │ ├── general │ │ ├── RequestListener.java │ │ └── RequestListenerProxy.java │ │ ├── model │ │ ├── CodeModel.java │ │ ├── HomeModel.java │ │ └── entity │ │ │ └── FileEntity.java │ │ ├── presenter │ │ ├── CodePresenter.java │ │ └── HomePresenter.java │ │ ├── util │ │ └── FileHelper.java │ │ └── view │ │ ├── CodeFragment.java │ │ └── HomeActivity.java │ └── res │ ├── drawable │ ├── ic_navigate_before_black_24dp.xml │ ├── item_bg_0.xml │ ├── item_bg_1.xml │ ├── item_bg_2.xml │ ├── item_bg_3.xml │ └── item_bg_4.xml │ ├── layout │ ├── activity_home.xml │ ├── fragment_code.xml │ ├── item_layer.xml │ └── toolbar.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── settings.gradle └── xmvp ├── .gitignore ├── bintray.gradle ├── build.gradle ├── install.gradle ├── proguard-rules.pro └── src ├── main ├── AndroidManifest.xml ├── java │ └── io │ │ └── xujiaji │ │ └── xmvp │ │ ├── XViewCycle.java │ │ ├── contracts │ │ └── XContract.java │ │ ├── presenters │ │ └── XBasePresenter.java │ │ ├── utils │ │ ├── ActivityUtils.java │ │ └── GenericHelper.java │ │ └── view │ │ ├── base │ │ ├── XBaseActivity.java │ │ ├── XBaseFragment.java │ │ └── v4 │ │ │ └── XBaseFragment.java │ │ └── interfaces │ │ ├── XActivityCycle.java │ │ ├── XFragViewCycle.java │ │ └── XViewCycle.java └── res │ └── values │ └── strings.xml └── test └── java └── io └── xujiaji └── xmvp └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | /.idea/workspace.xml 3 | /.idea/libraries/ 4 | /build 5 | /gradle.properties 6 | 7 | /XMVPLibrary/build/ 8 | /sample/build/ 9 | /.gradle/ 10 | /local.properties 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 37 | 38 | 39 | 40 | 41 | 42 | 44 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![banner](display/banner.png) 2 | [![GitHub release](https://img.shields.io/badge/size-12%20kb-green.svg)](https://github.com/xujiaji/XMVP/releases) [![GitHub release](https://img.shields.io/badge/bintray-1.2.3-brightgreen.svg)](https://bintray.com/xujiaji/maven/xmvp/1.2.3) 3 | 4 | # XMVP 5 | This is a mvp framework to help you easily achieve mvp structure. 6 | 7 | 中文文档: [XMVP(简洁的MVP框架)](https://juejin.im/post/5a31ecfaf265da4325294fa9) 8 | 9 | ## Update 10 | ``` 11 | > v1.2.3-androidx change to androidx library 12 | 13 | > v1.2.3 add onPresenterCircle method 14 | 15 | > v1.2.2 Fragment lazy load data, and add cycle function in View. Fix some bug. 16 | 17 | > v1.1.5 Can not create Presenter after fixing obfuscation 18 | 19 | > v1.1.4 fixes the subclass Activity of the current Activity crashes because it can not create a Presenter. 20 | 21 | > v1.1.3 Called after the start() method in Presenter has been put into View's onInit() method. 22 | 23 | > v1.1.2 XBasePresenter add judgment to determine whether the view still exists. 24 | 25 | > v1.1.1 Add a fragment of the extended V4 package in the 'io.xujiaji.xmvp.view.base.v4' package. 26 | ``` 27 | 28 | ## Introduction 29 | - You can call the method in step 2 directly through the presenter in the Activity or Fragment 30 | - You can call the definition of the View interface in Presenter method, and can be called by model in step 2 Model implementation of the class method 31 | - and the start and end methods in Presenter for the start and end of the Activity and Fragmentt lifecycle. 32 | - through XMVP you do not care about View, Presenter, Model is how to connect, you can easily decouple the project. 33 | - Finally you can easily build the code using the '[MVPManager](https://github.com/xujiaji/MVPManager)' plugin 34 | 35 | 36 | ## How to use? 37 | 38 | ### First, Add xmvp dependency 39 | ``` 40 | dependencies { 41 | implementation 'com.github.xujiaji:xmvp:1.2.3' 42 | } 43 | ``` 44 | or 45 | 46 | [![Download aar](https://img.shields.io/badge/download-.aar-red.svg)](https://github.com/xujiaji/XMVP/releases) 47 | 48 | > use androidx 49 | 50 | ``` 51 | dependencies { 52 | implementation 'com.github.xujiaji:xmvp:1.2.3-androidx' 53 | } 54 | ``` 55 | ### Step1:define a contract 56 | You need to define a contract in contracts package, it contains a extend 'XContract.Presenter' interface and a extend 'XContract.View' interface. 57 | > Example: [HomeContract.java](./sample/src/main/java/io/xujiaji/sample/contract/HomeContract.java) 58 | 59 | ``` java 60 | public interface HomeContract { 61 | interface Presenter extends XContract.Presenter{ 62 | void loadData(Activity activity); 63 | } 64 | 65 | interface View extends XContract.View{ 66 | void loadStart(); 67 | void loadEnd(List fileEntities); 68 | } 69 | 70 | interface Model extends XContract.Model { 71 | void scanFile(final Activity activity, final FileHelper.Listener> listener); 72 | } 73 | } 74 | ``` 75 | 76 | ### Step2:An implementation class for the Model interface. 77 | > Example: [HomeModel.java](./sample/src/main/java/io/xujiaji/sample/model/HomeModel.java) 78 | 79 | ``` java 80 | public class HomeModel implements HomeContract.Model { 81 | @Override 82 | public void scanFile(final Activity activity, final FileHelper.Listener> listener) { 83 | ... 84 | } 85 | } 86 | ``` 87 | 88 | ### Step3:An implementation class for the Presenter interface. 89 | > Example: [HomePresenter.java](./sample/src/main/java/io/xujiaji/sample/presenter/HomePresenter.java) 90 | 91 | ``` java 92 | public class HomePresenter extends XBasePresenter implements HomeContract.Presenter { 93 | 94 | @Override 95 | public void loadData(Activity activity) { 96 | view.loadStart(); 97 | model.scanFile(activity, new FileHelper.Listener>() { 98 | @Override 99 | public void success(List fileEntities) { 100 | view.loadEnd(fileEntities); 101 | } 102 | }); 103 | } 104 | } 105 | ``` 106 | 107 | ### Step4:An implementation class for the View interface. 108 | > Example: [HomeActivity.java](./sample/src/main/java/io/xujiaji/sample/view/HomeActivity.java) 109 | 110 | ``` java 111 | public class HomeActivity extends XBaseActivity implements HomeContract.View { 112 | ... 113 | 114 | @Override 115 | public void onInitCircle() { 116 | ... 117 | presenter.loadData(this); 118 | } 119 | 120 | @Override 121 | public void onListenerCircle() { 122 | ... 123 | } 124 | 125 | ... 126 | 127 | @Override 128 | public void loadStart() { 129 | ... 130 | } 131 | 132 | @Override 133 | public void loadEnd(List fileEntities) { 134 | ... 135 | } 136 | 137 | @Override 138 | public int layoutId() { 139 | return R.layout.activity_home; 140 | } 141 | } 142 | ``` 143 | 144 | #### You think this MVP too much trouble? 145 | MVPManager helps you manage MVP code quickly. 146 | [link MVPManager](https://github.com/xujiaji/MVPManager) 147 | 148 | #### Home UML 149 | ![mvp uml](display/mvp.png) 150 | 151 | # License 152 | ``` 153 | Copyright 2016 XuJiaji 154 | 155 | Licensed under the Apache License, Version 2.0 (the "License"); 156 | you may not use this file except in compliance with the License. 157 | You may obtain a copy of the License at 158 | 159 | http://www.apache.org/licenses/LICENSE-2.0 160 | 161 | Unless required by applicable law or agreed to in writing, software 162 | distributed under the License is distributed on an "AS IS" BASIS, 163 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 164 | See the License for the specific language governing permissions and 165 | limitations under the License. 166 | ``` 167 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | google() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.4.0-alpha02' 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 11 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | mavenCentral() 21 | maven { 22 | url "https://jitpack.io" 23 | } 24 | google() 25 | } 26 | } 27 | 28 | task clean(type: Delete) { 29 | delete rootProject.buildDir 30 | } 31 | 32 | 33 | ext { 34 | setup = [compileSdk: 28, 35 | buildTools: "28.0.3", 36 | minSdk : 14, 37 | targetSdk : 28] 38 | 39 | versions = [ 40 | supportLib: '1+', 41 | glide : "4.8.0", 42 | junit : "4.12", 43 | butterknife : "9.0.0-rc1", 44 | codeview : "0.3.1", 45 | rvHolder : "2.9.41" 46 | ] 47 | 48 | bintrayRepo = 'maven' 49 | bintrayName = 'xmvp' 50 | 51 | publishedGroupId = 'com.github.xujiaji' 52 | libraryName = 'xmvp' 53 | artifact = 'xmvp' 54 | 55 | libraryDescription = 'This is a mvp framework to help you easily achieve mvp structure.' 56 | 57 | siteUrl = 'https://github.com/xujiaji/XMVP' 58 | gitUrl = 'https://github.com/xujiaji/XMVP.git' 59 | 60 | libraryVersion = '1.2.3-androidx' 61 | 62 | developerId = 'xujiaji' 63 | developerName = 'xujiaji' 64 | developerEmail = 'xuoocc@gmail.com' 65 | 66 | licenseName = 'The Apache Software License, Version 2.0' 67 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 68 | allLicenses = ["Apache-2.0"] 69 | } 70 | -------------------------------------------------------------------------------- /display/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/display/banner.png -------------------------------------------------------------------------------- /display/dis.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/display/dis.gif -------------------------------------------------------------------------------- /display/mvp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/display/mvp.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Nov 11 03:14:53 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | #Thu Jun 15 14:29:13 CST 2017 11 | bintray.apikey=290eb732a5496554c7732fc0f592eba7597e1ced 12 | sdk.dir=D\:\\Android\\android-sdk 13 | bintray.user=xujiaji 14 | bintray.gpg.password=1357810danmu 15 | -------------------------------------------------------------------------------- /mvp.puml: -------------------------------------------------------------------------------- 1 | @startuml 2 | class HomeActivity 3 | interface View 4 | interface Model 5 | interface Presenter 6 | 7 | HomeActivity .up.|> View 8 | HomeActivity -right-> HomePresenter 9 | HomePresenter --> View 10 | HomePresenter .right.> Presenter 11 | HomePresenter --> HomeModel 12 | HomeModel ..|> Model 13 | hide member 14 | @enduml -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion setup.compileSdk 5 | 6 | defaultConfig { 7 | applicationId "io.xujiaji.sample" 8 | minSdkVersion setup.minSdk 9 | targetSdkVersion setup.targetSdk 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | } 14 | 15 | dependencies { 16 | implementation fileTree(include: ['*.jar'], dir: 'libs') 17 | implementation project(':xmvp') 18 | testImplementation "junit:junit:${versions.junit}" 19 | implementation "androidx.legacy:legacy-support-v4:${versions.supportLib}" 20 | implementation "androidx.appcompat:appcompat:${versions.supportLib}" 21 | implementation "androidx.recyclerview:recyclerview:${versions.supportLib}" 22 | implementation "com.google.android.material:material:${versions.supportLib}" 23 | //绑定UI 24 | implementation "com.jakewharton:butterknife:${versions.butterknife}" 25 | annotationProcessor "com.jakewharton:butterknife-compiler:${versions.butterknife}" 26 | implementation "thereisnospon.codeview:codeview:${versions.codeview}" 27 | implementation "com.github.CymChad:BaseRecyclerViewAdapterHelper:${versions.rvHolder}" 28 | } -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/jiana/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | 16 | 17 | -optimizationpasses 7 18 | -dontusemixedcaseclassnames # 是否使用大小写混合 19 | -dontpreverify # 混淆时是否做预校验 20 | -verbose # 混淆时是否记录日志 21 | #忽略警告 22 | -ignorewarning 23 | #不去忽略非公共的库类 24 | -dontskipnonpubliclibraryclasses 25 | #优化 不优化输入的类文件 26 | -dontoptimize 27 | # 混淆时所采用的算法 28 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 29 | -keepclassmembers class fqcn.of.javascript.interface.for.webview { 30 | public *; 31 | } 32 | 33 | #如果项目中用到了webview的复杂操作 ,最好加入 34 | -keepclassmembers class * extends android.webkit.WebViewClient { 35 | public void *(android.webkit.WebView,java.lang.String,android.graphics.Bitmap); 36 | public boolean *(android.webkit.WebView,java.lang.String); 37 | } 38 | -keepclassmembers class * extends android.webkit.WebChromeClient { 39 | public void *(android.webkit.WebView,java.lang.String); 40 | } 41 | 42 | #本地方法 43 | -keepclasseswithmembernames class * { 44 | native ; 45 | } 46 | 47 | #枚举不混淆 48 | -keepclassmembers enum * { 49 | public static **[] values(); 50 | public static ** valueOf(java.lang.String); 51 | } 52 | 53 | -keep class assets.** {*; } 54 | 55 | #View的构造函数 56 | -keepclasseswithmembers class * { 57 | public (android.content.Context, android.util.AttributeSet); 58 | } 59 | 60 | #注解框架butterknife 61 | # https://github.com/JakeWharton/butterknife 62 | # http://jakewharton.github.io/butterknife/ 63 | -keep class butterknife.** { *; } 64 | -dontwarn butterknife.internal.** 65 | -keep class **$$ViewBinder { *; } 66 | -keepclasseswithmembernames class * { 67 | @butterknife.* ; 68 | } 69 | -keepclasseswithmembernames class * { 70 | @butterknife.* ; 71 | } 72 | 73 | 74 | 75 | #序列化值 76 | -keep class * implements android.os.Parcelable { 77 | public static final android.os.Parcelable$Creator *; 78 | } 79 | 80 | #序列化值 81 | -keep class * implements java.ioSerializable { 82 | public static final java.ioSerializable *; 83 | } 84 | 85 | # 保护注解 86 | -keepattributes *Annotation* 87 | -keep class * extends java.lang.annotation.Annotation {*;} 88 | 89 | # 泛型与反射 90 | -keepattributes Signature 91 | -keepattributes EnclosingMethod 92 | 93 | -dontwarn io.xujiaji.hnbc.model.entity.** 94 | -keep class io.xujiaji.hnbc.model.entity.** { *; } 95 | 96 | #BottomSheet 97 | -dontwarn com.flipboard.bottomsheet.commons.** 98 | -keep class com.flipboard.bottomsheet.commons.** { *; } 99 | -dontwarn com.flipboard.bottomsheet.** 100 | -keep class com.flipboard.bottomsheet.** { *; } 101 | 102 | #Glide 图片框架 https://github.com/bumptech/glide 103 | -keep public class * implements com.bumptech.glide.module.GlideModule 104 | -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** { 105 | **[] $VALUES; 106 | public *; 107 | } 108 | -keep class com.loopj.** { *; } 109 | #LeakCanary内存检测 110 | -keep class org.eclipse.mat.** { *; } 111 | -keep class com.squareup.leakcanary.** { *; } 112 | 113 | # banner 的混淆代码 114 | -keep class com.youth.banner.** { 115 | *; 116 | } 117 | 118 | 119 | #-keep public class * extends android.app.Fragment 120 | #-keep public class * extends android.app.Activity 121 | #-keep public class * extends android.app.Application 122 | #-keep public class * extends android.app.Service 123 | #-keep public class * extends android.content.BroadcastReceiver 124 | #-keep public class * extends android.content.ContentProvider 125 | #-keep public class * extends android.app.backup.BackupAgentHelper 126 | #-keep public class * extends android.preference.Preference 127 | #如果有引用v4包可以添加下面这行 128 | -keep public class * extends android.support.** 129 | #如果引用了v4或者v7包 130 | -dontwarn android.support.** 131 | 132 | 133 | -keep class com.jpardogo.android.googleprogressbar.** { *; } 134 | -keep class com.tencent.open.TDialog$* 135 | -keep class com.tencent.open.TDialog$* {*;} 136 | -keep class com.tencent.open.PKDialog 137 | -keep class com.tencent.open.PKDialog {*;} 138 | -keep class com.tencent.open.PKDialog$* 139 | -keep class com.tencent.open.PKDialog$* {*;} 140 | 141 | -ignorewarnings 142 | 143 | -keepattributes Signature,*Annotation* 144 | 145 | # keep BmobSDK 146 | -dontwarn cn.bmob.v3.** 147 | -keep class cn.bmob.v3.** {*;} 148 | 149 | # 确保JavaBean不被混淆-否则gson将无法将数据解析成具体对象 150 | -keep class * extends cn.bmob.v3.BmobObject { 151 | *; 152 | } 153 | -keep class com.example.bmobexample.bean.BankCard{*;} 154 | -keep class com.example.bmobexample.bean.GameScore{*;} 155 | -keep class com.example.bmobexample.bean.MyUser{*;} 156 | -keep class com.example.bmobexample.bean.Person{*;} 157 | -keep class com.example.bmobexample.file.Movie{*;} 158 | -keep class com.example.bmobexample.file.Song{*;} 159 | -keep class com.example.bmobexample.relation.Post{*;} 160 | -keep class com.example.bmobexample.relation.Comment{*;} 161 | 162 | # gson 163 | -dontwarn com.google.** 164 | -keep class com.google.gson.** {*;} 165 | 166 | # keep BmobPush 167 | -dontwarn cn.bmob.push.** 168 | -keep class cn.bmob.push.** {*;} 169 | 170 | # keep okhttp3、okio 171 | -dontwarn okhttp3.** 172 | -keep class okhttp3.** { *;} 173 | -keep interface okhttp3.** { *; } 174 | -dontwarn okio.** 175 | 176 | # keep rx 177 | -dontwarn sun.misc.** 178 | -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* { 179 | long producerIndex; 180 | long consumerIndex; 181 | } 182 | -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef { 183 | rx.internal.util.atomic.LinkedQueueNode producerNode; 184 | } 185 | -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef { 186 | rx.internal.util.atomic.LinkedQueueNode consumerNode; 187 | } 188 | 189 | # 如果你需要兼容6.0系统,请不要混淆org.apache.http.legacy.jar 190 | -dontwarn android.net.compatibility.** 191 | -dontwarn android.net.http.** 192 | -dontwarn com.android.internal.http.multipart.** 193 | -dontwarn org.apache.commons.** 194 | -dontwarn org.apache.http.** 195 | -keep class android.net.compatibility.**{*;} 196 | -keep class android.net.http.**{*;} 197 | -keep class com.android.internal.http.multipart.**{*;} 198 | -keep class org.apache.commons.**{*;} 199 | -keep class org.apache.http.**{*;} 200 | 201 | -keep class * implements java.io.Serializable {*;} 202 | -keepclassmembers class * implements java.io.Serializable 203 | 204 | #apk 包内所有 class 的内部结构 205 | -dump class_files.txt 206 | #未混淆的类和成员 207 | -printseeds seeds.txt 208 | #列出从 apk 中删除的代码 209 | -printusage unused.txt 210 | #混淆前后的映射 211 | -verbose 212 | -printmapping mapping.txt 213 | 214 | #不混淆资源类 215 | -keep class **.R$* { 216 | *; 217 | } -------------------------------------------------------------------------------- /sample/src/androidTest/java/io/xujiaji/sample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/assets/data.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/sample/src/main/assets/data.zip -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/App.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | /** 7 | * Created by jiana on 16-11-21. 8 | */ 9 | 10 | public class App extends Application{ 11 | private static Context mContext; 12 | @Override 13 | public void onCreate() { 14 | super.onCreate(); 15 | mContext = this.getApplicationContext(); 16 | } 17 | 18 | public static Context getInstanse() { 19 | return mContext; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/adapter/HomeAdapter.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.adapter; 2 | 3 | import androidx.recyclerview.widget.RecyclerView; 4 | import android.view.View; 5 | import android.widget.TextView; 6 | 7 | import com.chad.library.adapter.base.BaseMultiItemQuickAdapter; 8 | import com.chad.library.adapter.base.BaseViewHolder; 9 | 10 | import java.io.File; 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.List; 14 | 15 | import io.xujiaji.sample.App; 16 | import io.xujiaji.sample.R; 17 | import io.xujiaji.sample.model.entity.FileEntity; 18 | 19 | /** 20 | * Created by jiana on 16-11-21. 21 | */ 22 | 23 | public class HomeAdapter extends BaseMultiItemQuickAdapter { 24 | private List bgRes; 25 | 26 | public HomeAdapter(List data) { 27 | super(data); 28 | initBgRes(); 29 | for (int i = 0; i < 6; i++) { 30 | addItemType(i, R.layout.item_layer); 31 | } 32 | } 33 | 34 | private void initBgRes() { 35 | bgRes = new ArrayList<>(); 36 | bgRes.add(R.drawable.item_bg_0); 37 | bgRes.add(R.drawable.item_bg_1); 38 | bgRes.add(R.drawable.item_bg_2); 39 | bgRes.add(R.drawable.item_bg_3); 40 | bgRes.add(R.drawable.item_bg_4); 41 | bgRes.add(R.drawable.item_bg_1); 42 | Collections.shuffle(bgRes); 43 | } 44 | 45 | @Override 46 | protected void convert(final BaseViewHolder baseViewHolder, final FileEntity multiItemEntity) { 47 | TextView name = baseViewHolder.getView(R.id.name); 48 | baseViewHolder.addOnClickListener(R.id.name); 49 | name.setText(multiItemEntity.getFile().getName()); 50 | final int level = multiItemEntity.getLevel(); 51 | if (level < bgRes.size()) { 52 | name.setBackgroundResource(bgRes.get(level)); 53 | } 54 | RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) name.getLayoutParams(); 55 | params.leftMargin = (level + 1) * App.getInstanse().getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin); 56 | name.setLayoutParams(params); 57 | // Log.e("convert", multiItemEntity.getLevel() + "; file = " + multiItemEntity.getFile()); 58 | // expandAll(baseViewHolder.getAdapterPosition(), true); 59 | name.setOnClickListener(new View.OnClickListener() { 60 | @Override 61 | public void onClick(View v) { 62 | int pos = baseViewHolder.getAdapterPosition(); 63 | if (multiItemEntity.isExpanded()) { 64 | collapse(pos, false); 65 | } else { 66 | expand(pos, false); 67 | } 68 | File file = multiItemEntity.getFile(); 69 | if (listener != null && file.isFile()) { 70 | listener.open(file); 71 | } 72 | } 73 | }); 74 | } 75 | 76 | 77 | private OpenListener listener; 78 | public void setOpenLister(OpenListener listener) { 79 | this.listener = listener; 80 | } 81 | public interface OpenListener { 82 | void open(File file); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/contract/CodeContract.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.contract; 2 | 3 | import android.app.Activity; 4 | 5 | import java.io.File; 6 | 7 | import io.xujiaji.sample.general.RequestListener; 8 | import io.xujiaji.sample.util.FileHelper; 9 | import io.xujiaji.xmvp.contracts.XContract; 10 | 11 | /** 12 | * Created by jiana on 16-11-21. 13 | */ 14 | 15 | public interface CodeContract { 16 | interface Presenter extends XContract.Presenter{ 17 | void readCodeByFile(Activity activity, File file); 18 | } 19 | 20 | interface View extends XContract.View{ 21 | void showLoadCode(); 22 | void showCode(String code); 23 | } 24 | 25 | interface Model extends XContract.Model { 26 | 27 | void readCode(final Activity activity, final File file, final RequestListener listener); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/contract/HomeContract.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.contract; 2 | 3 | import android.app.Activity; 4 | 5 | import java.util.List; 6 | 7 | import io.xujiaji.sample.general.RequestListener; 8 | import io.xujiaji.sample.model.entity.FileEntity; 9 | import io.xujiaji.sample.util.FileHelper; 10 | import io.xujiaji.xmvp.contracts.XContract; 11 | 12 | /** 13 | * Created by jiana on 16-11-19. 14 | */ 15 | 16 | public interface HomeContract { 17 | interface Presenter extends XContract.Presenter{ 18 | void loadData(Activity activity); 19 | } 20 | 21 | interface View extends XContract.View{ 22 | void loadStart(); 23 | void loadEnd(List fileEntities); 24 | } 25 | 26 | interface Model extends XContract.Model { 27 | void scanFile(final Activity activity, final RequestListener> listener); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/general/RequestListener.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.general; 2 | 3 | /** 4 | * 通用请求监听
5 | * Generic request listener 6 | * Created by jiaji on 2018/1/30. 7 | */ 8 | 9 | public interface RequestListener 10 | { 11 | void requestSuccess(T t); 12 | 13 | void requestErr(String errStr); 14 | } 15 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/general/RequestListenerProxy.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.general; 2 | 3 | import io.xujiaji.xmvp.presenters.XBasePresenter; 4 | 5 | /** 6 | * Created by jiaji on 2018/1/30. 7 | */ 8 | 9 | public abstract class RequestListenerProxy implements RequestListener 10 | { 11 | private XBasePresenter p; 12 | 13 | public RequestListenerProxy(XBasePresenter p) 14 | { 15 | this.p = p; 16 | } 17 | 18 | @Override 19 | public void requestSuccess(T t) 20 | { 21 | if (p.viewIsExist()) 22 | { 23 | success(t); 24 | } 25 | } 26 | 27 | @Override 28 | public void requestErr(String errStr) 29 | { 30 | if (p.viewIsExist()) 31 | { 32 | err(errStr); 33 | } 34 | } 35 | 36 | public abstract void success(T t); 37 | 38 | public void err(String err) 39 | { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/model/CodeModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package io.xujiaji.sample.model; 17 | 18 | import android.app.Activity; 19 | 20 | import java.io.File; 21 | 22 | import io.xujiaji.sample.contract.CodeContract; 23 | import io.xujiaji.sample.general.RequestListener; 24 | import io.xujiaji.sample.util.FileHelper; 25 | 26 | /** 27 | * Created by jiana on 13/12/16. 28 | */ 29 | public class CodeModel implements CodeContract.Model { 30 | 31 | @Override 32 | public void readCode(final Activity activity, final File file, final RequestListener listener) { 33 | new Thread() { 34 | @Override 35 | public void run() { 36 | FileHelper fileHelper = new FileHelper(); 37 | fileHelper.readText(activity, file, listener); 38 | } 39 | }.start(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/model/HomeModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package io.xujiaji.sample.model; 17 | 18 | import android.app.Activity; 19 | 20 | import java.util.List; 21 | 22 | import io.xujiaji.sample.contract.HomeContract; 23 | import io.xujiaji.sample.general.RequestListener; 24 | import io.xujiaji.sample.model.entity.FileEntity; 25 | import io.xujiaji.sample.util.FileHelper; 26 | 27 | /** 28 | * Created by jiana on 13/12/16. 29 | */ 30 | public class HomeModel implements HomeContract.Model { 31 | @Override 32 | public void scanFile(final Activity activity, final RequestListener> listener) { 33 | new Thread() { 34 | @Override 35 | public void run() { 36 | final FileHelper fileHelper = new FileHelper(); 37 | fileHelper.unzip(activity, listener); 38 | } 39 | }.start(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/model/entity/FileEntity.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.model.entity; 2 | 3 | import com.chad.library.adapter.base.entity.AbstractExpandableItem; 4 | import com.chad.library.adapter.base.entity.MultiItemEntity; 5 | 6 | import java.io.File; 7 | 8 | /** 9 | * Created by jiana on 16-11-21. 10 | * 11 | */ 12 | 13 | public class FileEntity extends AbstractExpandableItem implements MultiItemEntity { 14 | private File file; 15 | private int level; 16 | 17 | public File getFile() { 18 | return file; 19 | } 20 | 21 | public void setFile(File file) { 22 | this.file = file; 23 | } 24 | 25 | public void setLevel(int level) { 26 | this.level = level; 27 | } 28 | 29 | @Override 30 | public int getLevel() { 31 | return level; 32 | } 33 | 34 | @Override 35 | public int getItemType() { 36 | return level; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/presenter/CodePresenter.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.presenter; 2 | 3 | import android.app.Activity; 4 | 5 | import java.io.File; 6 | 7 | import io.xujiaji.sample.contract.CodeContract; 8 | import io.xujiaji.sample.general.RequestListenerProxy; 9 | import io.xujiaji.sample.model.CodeModel; 10 | import io.xujiaji.sample.util.FileHelper; 11 | import io.xujiaji.xmvp.presenters.XBasePresenter; 12 | 13 | /** 14 | * Created by jiana on 16-11-21. 15 | */ 16 | 17 | public class CodePresenter extends XBasePresenter implements CodeContract.Presenter { 18 | 19 | @Override 20 | public void readCodeByFile(Activity activity, File file) { 21 | view.showLoadCode(); 22 | model.readCode(activity, file, new RequestListenerProxy(this) 23 | { 24 | @Override 25 | public void success(String s) 26 | { 27 | view.showCode(s); 28 | } 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/presenter/HomePresenter.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.presenter; 2 | 3 | import android.app.Activity; 4 | 5 | import java.util.List; 6 | 7 | import io.xujiaji.sample.contract.HomeContract; 8 | import io.xujiaji.sample.general.RequestListenerProxy; 9 | import io.xujiaji.sample.model.HomeModel; 10 | import io.xujiaji.sample.model.entity.FileEntity; 11 | import io.xujiaji.sample.util.FileHelper; 12 | import io.xujiaji.xmvp.presenters.XBasePresenter; 13 | 14 | /** 15 | * Created by jiana on 16-11-19. 16 | */ 17 | 18 | public class HomePresenter extends XBasePresenter implements HomeContract.Presenter { 19 | 20 | @Override 21 | public void loadData(Activity activity) { 22 | view.loadStart(); 23 | model.scanFile(activity, new RequestListenerProxy>(this) 24 | { 25 | @Override 26 | public void success(List fileEntities) 27 | { 28 | view.loadEnd(fileEntities); 29 | } 30 | 31 | }); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/util/FileHelper.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.util; 2 | 3 | import android.app.Activity; 4 | import android.os.Environment; 5 | 6 | import java.io.BufferedInputStream; 7 | import java.io.BufferedOutputStream; 8 | import java.io.File; 9 | import java.io.FileOutputStream; 10 | import java.io.FileReader; 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | import java.io.OutputStream; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.zip.ZipEntry; 17 | import java.util.zip.ZipInputStream; 18 | 19 | import io.xujiaji.sample.App; 20 | import io.xujiaji.sample.general.RequestListener; 21 | import io.xujiaji.sample.model.entity.FileEntity; 22 | 23 | /** 24 | * Created by jiana on 16-11-21. 25 | * 文件帮助类
26 | * file helper class 27 | */ 28 | 29 | public class FileHelper { 30 | 31 | /** 32 | * assets压缩包文件名
33 | * zip file name in assets folder 34 | */ 35 | public static final String FILE_NAME = "data.zip"; 36 | public static final int BUFFER_SIZE = 1024; 37 | /** 38 | * 文件层数 39 | */ 40 | public static int fileLayer = 0; 41 | 42 | /** 43 | * 解压 44 | */ 45 | public void unzip(Activity activity, final RequestListener> listener) { 46 | try { 47 | unzipModuleData(new File(getAppCacheDir()), 48 | App.getInstanse().getAssets().open(FILE_NAME)); 49 | 50 | File file = new File(getAppCacheDir()); 51 | final List fileEntities = new ArrayList<>(); 52 | for (File f : file.listFiles()) { 53 | // FileEntity fileEntity = new FileEntity(); 54 | // fileEntity.setFile(f); 55 | // fileEntity.setLevel(0); 56 | // fill(fileEntity, f); 57 | fileEntities.add(fill(new FileEntity(), f)); 58 | } 59 | activity.runOnUiThread(new Runnable() { 60 | @Override 61 | public void run() { 62 | listener.requestSuccess(fileEntities); 63 | } 64 | }); 65 | } catch (Exception e) { 66 | e.printStackTrace(); 67 | } 68 | } 69 | 70 | private FileEntity fill(FileEntity fileEntityResult, File file) { 71 | // Log.e("file", fileLayer + ";folder = " + file.isDirectory() + ";file = " + file.toString()); 72 | // FileEntity fileEntity = new FileEntity(); 73 | fileEntityResult.setFile(file); 74 | fileEntityResult.setLevel(fileLayer); 75 | if (file.isDirectory()) { 76 | fileLayer++; 77 | for (File f : file.listFiles()) { 78 | fileEntityResult.addSubItem(fill(new FileEntity(), f)); 79 | } 80 | fileLayer--; 81 | } 82 | return fileEntityResult; 83 | // fileEntityResult.addSubItem(fileEntity); 84 | } 85 | 86 | /** 87 | * 解压 88 | * 89 | * @param directory 90 | * @param ism 91 | * @throws Exception 92 | */ 93 | public void unzipModuleData(final File directory, InputStream ism) throws Exception { 94 | InputStream is = new BufferedInputStream(ism); 95 | ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); 96 | try { 97 | ZipEntry entry; 98 | while ((entry = zis.getNextEntry()) != null) { 99 | String entryName = entry.getName(); 100 | if (!entryName.isEmpty()) { 101 | if (entry.isDirectory()) { 102 | File file = new File(directory, entryName); 103 | if (file.exists()) continue; 104 | if (!file.mkdir()) { 105 | throw new RuntimeException("Failed to create directory"); 106 | } 107 | 108 | } else { 109 | int count; 110 | byte[] buff = new byte[BUFFER_SIZE]; 111 | BufferedOutputStream dest = null; 112 | try { 113 | OutputStream fos = new FileOutputStream(new File(directory, entryName)); 114 | dest = new BufferedOutputStream(fos, BUFFER_SIZE); 115 | while ((count = zis.read(buff, 0, BUFFER_SIZE)) != -1) { 116 | dest.write(buff, 0, count); 117 | } 118 | dest.flush(); 119 | } finally { 120 | if (dest != null) { 121 | dest.close(); 122 | } 123 | } 124 | } 125 | } 126 | } 127 | } catch (Exception e) { 128 | e.printStackTrace(); 129 | } finally { 130 | is.close(); 131 | zis.close(); 132 | } 133 | } 134 | 135 | public void readText(Activity activity, File file, final RequestListener listener) { 136 | try { 137 | FileReader fr = new FileReader(file); 138 | final StringBuilder sb = new StringBuilder(); 139 | int len; 140 | char[] buff = new char[1024]; 141 | while ((len = fr.read(buff)) != -1) { 142 | sb.append(buff, 0, len); 143 | } 144 | fr.close(); 145 | 146 | activity.runOnUiThread(new Runnable() { 147 | @Override 148 | public void run() { 149 | listener.requestSuccess(sb.toString()); 150 | } 151 | }); 152 | 153 | } catch (IOException e) { 154 | e.printStackTrace(); 155 | } 156 | } 157 | 158 | /** 159 | * 获取缓存目录
160 | * Get cache folder 161 | * @return 162 | */ 163 | public String getAppCacheDir() { 164 | String cachePath = null; 165 | // if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) 166 | // || !Environment.isExternalStorageRemovable()) { 167 | // File f = App.getInstanse().getExternalCacheDir(); 168 | // cachePath = App.getInstanse().getExternalCacheDir().getPath(); 169 | // } else { 170 | cachePath = App.getInstanse().getCacheDir().getPath(); 171 | // } 172 | return cachePath; 173 | } 174 | 175 | } 176 | 177 | 178 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/view/CodeFragment.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.view; 2 | 3 | import android.app.ProgressDialog; 4 | import android.os.Bundle; 5 | import androidx.annotation.NonNull; 6 | 7 | import java.io.File; 8 | 9 | import butterknife.BindView; 10 | import butterknife.ButterKnife; 11 | import io.xujiaji.sample.R; 12 | import io.xujiaji.sample.contract.CodeContract; 13 | import io.xujiaji.sample.presenter.CodePresenter; 14 | import io.xujiaji.xmvp.view.base.XBaseFragment; 15 | import thereisnospon.codeview.CodeView; 16 | import thereisnospon.codeview.CodeViewTheme; 17 | 18 | /** 19 | * Created by jiana on 16-11-21. 20 | */ 21 | 22 | public class CodeFragment extends XBaseFragment implements CodeContract.View { 23 | public static final String KEY = "file"; 24 | private ProgressDialog dialog; 25 | private File mFile; 26 | 27 | public static CodeFragment newInstance(File file) { 28 | CodeFragment cf = new CodeFragment(); 29 | Bundle bundle = new Bundle(); 30 | bundle.putSerializable(KEY, file); 31 | cf.setArguments(bundle); 32 | return cf; 33 | } 34 | 35 | @BindView(R.id.codeView) 36 | CodeView codeView; 37 | 38 | @Override 39 | public int layoutId() { 40 | return R.layout.fragment_code; 41 | } 42 | 43 | @Override 44 | public void onArgumentsHandle(@NonNull Bundle bundle) { 45 | mFile = (File) bundle.getSerializable(KEY); 46 | } 47 | 48 | @Override 49 | public void onInitCircle() { 50 | ButterKnife.bind(this, getRootView()); 51 | codeView.setTheme(CodeViewTheme.ANDROIDSTUDIO).fillColor(); 52 | } 53 | 54 | @Override 55 | public void onLazyLoad() { 56 | presenter.readCodeByFile(getActivity(), mFile); 57 | } 58 | 59 | @Override 60 | public void showLoadCode() { 61 | dialog = new ProgressDialog(getActivity()); 62 | dialog.setTitle("loading..."); 63 | dialog.setCancelable(false); 64 | dialog.show(); 65 | } 66 | 67 | @Override 68 | public void showCode(String code) { 69 | codeView.showCode(code); 70 | codeView.postDelayed(new Runnable() { 71 | @Override 72 | public void run() { 73 | if (dialog != null && dialog.isShowing()) { 74 | dialog.dismiss(); 75 | } 76 | } 77 | }, 1000); 78 | } 79 | 80 | @Override 81 | public boolean isInViewPager() { 82 | return false; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /sample/src/main/java/io/xujiaji/sample/view/HomeActivity.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample.view; 2 | 3 | import android.app.FragmentTransaction; 4 | import android.app.ProgressDialog; 5 | import com.google.android.material.appbar.AppBarLayout; 6 | import androidx.recyclerview.widget.LinearLayoutManager; 7 | import androidx.recyclerview.widget.RecyclerView; 8 | import androidx.appcompat.widget.Toolbar; 9 | import android.view.View; 10 | import android.widget.FrameLayout; 11 | 12 | import java.io.File; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | import butterknife.BindView; 17 | import butterknife.ButterKnife; 18 | import io.xujiaji.sample.R; 19 | import io.xujiaji.sample.adapter.HomeAdapter; 20 | import io.xujiaji.sample.contract.HomeContract; 21 | import io.xujiaji.sample.model.entity.FileEntity; 22 | import io.xujiaji.sample.presenter.HomePresenter; 23 | import io.xujiaji.xmvp.view.base.XBaseActivity; 24 | 25 | public class HomeActivity extends XBaseActivity implements HomeContract.View { 26 | @BindView(R.id.toolbar) 27 | Toolbar toolbar; 28 | @BindView(R.id.rvFolder) 29 | RecyclerView rvFolder; 30 | @BindView(R.id.fragParentLayout) 31 | FrameLayout fragParentLayout; 32 | private ProgressDialog dialog; 33 | private HomeAdapter adapter; 34 | @BindView(R.id.appBarLayout) 35 | AppBarLayout appBarLayout; 36 | 37 | @Override 38 | public void onInitCircle() { 39 | ButterKnife.bind(this); 40 | toolbar.setTitle(R.string.xmvp_folder); 41 | setSupportActionBar(toolbar); 42 | rvFolder.setLayoutManager(new LinearLayoutManager(this)); 43 | adapter = new HomeAdapter(new ArrayList()); 44 | rvFolder.setAdapter(adapter); 45 | presenter.loadData(this); 46 | } 47 | 48 | @Override 49 | public void onListenerCircle() { 50 | adapter.setOpenLister(new HomeAdapter.OpenListener() { 51 | @Override 52 | public void open(File file) { 53 | showCode(file); 54 | } 55 | }); 56 | 57 | toolbar.setNavigationOnClickListener(new View.OnClickListener() { 58 | @Override 59 | public void onClick(View v) { 60 | onBackPressed(); 61 | } 62 | }); 63 | } 64 | 65 | /** 66 | * 显示代码
show code detail 67 | * 68 | * @param file 69 | */ 70 | private void showCode(File file) { 71 | FragmentTransaction ft = getFragmentManager().beginTransaction(); 72 | CodeFragment cf = CodeFragment.newInstance(file); 73 | ft.replace(R.id.fragParentLayout, cf); 74 | ft.addToBackStack(null); 75 | ft.commit(); 76 | showBlack(file.getName()); 77 | // rvFolder.setVisibility(View.GONE); 78 | fragParentLayout.setVisibility(View.VISIBLE); 79 | } 80 | 81 | /** 82 | * 显示返回按钮和标题,传入null则隐藏返回按钮
83 | * show back button and title, hide back when passing in null 84 | * @param title 85 | */ 86 | private void showBlack(String title) { 87 | if (title != null) { 88 | toolbar.setNavigationIcon(R.drawable.ic_navigate_before_black_24dp); 89 | toolbar.setTitle(title); 90 | appBarLayout.setExpanded(true, true); 91 | } else { 92 | toolbar.setNavigationIcon(null); 93 | toolbar.setTitle(R.string.xmvp_folder); 94 | fragParentLayout.setVisibility(View.GONE); 95 | } 96 | 97 | } 98 | 99 | 100 | @Override 101 | public void loadStart() { 102 | dialog = new ProgressDialog(this); 103 | dialog.setTitle("loading data..."); 104 | dialog.setCancelable(false); 105 | dialog.show(); 106 | } 107 | 108 | @Override 109 | public void loadEnd(List fileEntities) { 110 | if (dialog != null && dialog.isShowing()) { 111 | dialog.dismiss(); 112 | } 113 | adapter.getData().addAll(fileEntities); 114 | adapter.notifyDataSetChanged(); 115 | } 116 | 117 | @Override 118 | public int layoutId() { 119 | return R.layout.activity_home; 120 | } 121 | 122 | @Override 123 | public void onBackPressed() { 124 | if (fragParentLayout.getVisibility() == View.VISIBLE) { 125 | showBlack(null); 126 | } 127 | super.onBackPressed(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_navigate_before_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/item_bg_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/item_bg_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/item_bg_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/item_bg_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/item_bg_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_home.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 17 | 18 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/fragment_code.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/item_layer.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/toolbar.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 18 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xujiaji/XMVP/32dcb40e3de49737d097e89a3290550e52e2d2ed/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #F44336 4 | #D32F2F 5 | #FF5252 6 | #edf2f4 7 | 8 | -------------------------------------------------------------------------------- /sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 48dp 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | XMP example 3 | XMVP代码目录 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':xmvp' 2 | -------------------------------------------------------------------------------- /xmvp/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /xmvp/bintray.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | task sourcesJar(type: Jar) { 6 | from android.sourceSets.main.java.srcDirs 7 | classifier = 'sources' 8 | } 9 | 10 | task javadoc(type: Javadoc) { 11 | source = android.sourceSets.main.java.srcDirs 12 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 13 | options.encoding "UTF-8" 14 | options.charSet 'UTF-8' 15 | options.author true 16 | options.version true 17 | failOnError false 18 | } 19 | 20 | task javadocJar(type: Jar, dependsOn: javadoc) { 21 | classifier = 'javadoc' 22 | from javadoc.destinationDir 23 | } 24 | artifacts { 25 | archives javadocJar 26 | archives sourcesJar 27 | } 28 | 29 | // Bintray 30 | Properties properties = new Properties() 31 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 32 | 33 | bintray { 34 | user = properties.getProperty("bintray.user") 35 | key = properties.getProperty("bintray.apikey") 36 | 37 | configurations = ['archives'] 38 | pkg { 39 | repo = bintrayRepo 40 | name = bintrayName 41 | desc = libraryDescription 42 | websiteUrl = siteUrl 43 | vcsUrl = gitUrl 44 | licenses = allLicenses 45 | publish = true 46 | publicDownloadNumbers = true 47 | version { 48 | desc = libraryDescription 49 | gpg { 50 | sign = true //Determines whether to GPG sign the files. The default is false 51 | passphrase = properties.getProperty("bintray.gpg.password") 52 | //Optional. The passphrase for GPG signing' 53 | } 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /xmvp/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion setup.compileSdk 5 | 6 | defaultConfig { 7 | minSdkVersion setup.minSdk 8 | targetSdkVersion setup.targetSdk 9 | versionCode 5 10 | versionName "1.2.2" 11 | } 12 | buildTypes { 13 | release { 14 | minifyEnabled false 15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 16 | } 17 | } 18 | } 19 | 20 | dependencies { 21 | implementation fileTree(include: ['*.jar'], dir: 'libs') 22 | compileOnly "androidx.appcompat:appcompat:${versions.supportLib}" 23 | compileOnly "junit:junit:${versions.junit}" 24 | } 25 | apply from: 'install.gradle' 26 | apply from: 'bintray.gradle' 27 | javadoc { 28 | options{ 29 | encoding "UTF-8" 30 | charSet 'UTF-8' 31 | author true 32 | version true 33 | links "https://github.com/xujiaji/XMVP" 34 | title "XMVP Doc" 35 | } 36 | } -------------------------------------------------------------------------------- /xmvp/install.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | group = publishedGroupId // Maven Group ID for the artifact 4 | 5 | install { 6 | repositories.mavenInstaller { 7 | // This generates POM.xml with proper parameters 8 | pom { 9 | project { 10 | packaging 'aar' 11 | groupId publishedGroupId 12 | artifactId artifact 13 | 14 | // Add your description here 15 | name libraryName 16 | description libraryDescription 17 | url siteUrl 18 | 19 | // Set your license 20 | licenses { 21 | license { 22 | name licenseName 23 | url licenseUrl 24 | } 25 | } 26 | developers { 27 | developer { 28 | id developerId 29 | name developerName 30 | email developerEmail 31 | } 32 | } 33 | scm { 34 | connection gitUrl 35 | developerConnection gitUrl 36 | url siteUrl 37 | 38 | } 39 | } 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /xmvp/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/jiana/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /xmvp/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/XViewCycle.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.xmvp; 2 | 3 | /** 4 | * author: xujiaji 5 | * created on: 2018/9/4 10:57 6 | * description: 7 | */ 8 | public interface XViewCycle { 9 | } 10 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/contracts/XContract.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package io.xujiaji.xmvp.contracts; 17 | 18 | /** 19 | * MVP基础接口
20 | * MVP base interface 21 | */ 22 | public interface XContract { 23 | interface Presenter { 24 | 25 | } 26 | 27 | interface View { 28 | 29 | } 30 | 31 | interface Model { 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/presenters/XBasePresenter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.xujiaji.xmvp.presenters; 18 | 19 | import io.xujiaji.xmvp.contracts.XContract; 20 | 21 | /** 22 | * MVP:Presenter基类 23 | */ 24 | 25 | public class XBasePresenter { 26 | protected T view; 27 | protected E model; 28 | 29 | public void init(Object view, Object model) { 30 | this.view = (T) view; 31 | this.model = (E) model; 32 | } 33 | 34 | /** 35 | * 当Activity的onCreate或Fragment的onAttach方法执行时将会调用
36 | * call this method when Activity's onCreate or Fragment's onAttach method called 37 | */ 38 | public void start() {} 39 | 40 | /** 41 | * 当onDestroy或onDetach方法执行时将会调用
42 | * call this method when Activity's onDestroy or Fragment's onDetach method called 43 | */ 44 | public void end() { 45 | view = null; 46 | model = null; 47 | } 48 | 49 | /** 50 | * view是否还存在
51 | * whether view exist 52 | */ 53 | public boolean viewIsExist() 54 | { 55 | return view != null; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/utils/ActivityUtils.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.xmvp.utils; 2 | 3 | /** 4 | * Activity工具类
5 | * Activity Tool class 6 | */ 7 | public class ActivityUtils { 8 | private static long exitTime = 0; 9 | 10 | /** 11 | * 作用:2s内点击两次返回则退出程序
Function: click twice within 2s to exit the program 12 | * 13 | * @return 是否退出程序
whether to quit the program 14 | */ 15 | public static boolean exitBy2Click() { 16 | return exitBy2Click(2000); 17 | } 18 | 19 | /** 20 | * @param space 两次点击时间间隔
custom interval time 21 | */ 22 | public static boolean exitBy2Click(int space) { 23 | if ((System.currentTimeMillis() - exitTime) > space) { 24 | exitTime = System.currentTimeMillis(); 25 | return false; 26 | } else { 27 | return true; 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/utils/GenericHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.xujiaji.xmvp.utils; 18 | 19 | import java.lang.reflect.ParameterizedType; 20 | import java.lang.reflect.Type; 21 | 22 | import io.xujiaji.xmvp.contracts.XContract; 23 | import io.xujiaji.xmvp.presenters.XBasePresenter; 24 | 25 | public class GenericHelper { 26 | 27 | public static Class getGenericClass(Class klass, Class filterClass) { 28 | Type type = klass.getGenericSuperclass(); 29 | if (type == null || !(type instanceof ParameterizedType)) return null; 30 | ParameterizedType parameterizedType = (ParameterizedType) type; 31 | Type[] types = parameterizedType.getActualTypeArguments(); 32 | for (Type t : types) { 33 | Class tClass = (Class) t; 34 | if (filterClass.isAssignableFrom(tClass)) { 35 | return tClass; 36 | } 37 | } 38 | 39 | return null; 40 | // if(types == null || types.length == 0) return null; 41 | // return (Class) types[0]; 42 | } 43 | 44 | private static Class getViewClass(Class klass, Class filterClass) { 45 | for (Class c : klass.getInterfaces()) { 46 | if (filterClass.isAssignableFrom(c)) return klass; 47 | } 48 | return getViewClass(klass.getSuperclass(), filterClass); 49 | } 50 | 51 | 52 | public static T newPresenter(Object obj) { 53 | if (!XContract.View.class.isInstance(obj)) { 54 | throw new RuntimeException("no implement XContract.BaseView"); 55 | } 56 | try { 57 | Class currentClass = obj.getClass(); 58 | Class viewClass = getViewClass(currentClass, XContract.View.class); 59 | Class presenterClass = getGenericClass(viewClass, XContract.Presenter.class); 60 | Class modelClass = getGenericClass(presenterClass, XContract.Model.class); 61 | // Constructor construct = presenterClass.getConstructor(viewClass, modelClass); 62 | XBasePresenter xBasePresenter = (XBasePresenter) presenterClass.newInstance(); 63 | xBasePresenter.init(obj, modelClass.newInstance()); 64 | return (T) xBasePresenter; 65 | } catch (Exception e) { 66 | e.printStackTrace(); 67 | } 68 | throw new RuntimeException("instance of presenter fail\n" + 69 | " Remind presenter need to extends XBasePresenter"); 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/view/base/XBaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.xujiaji.xmvp.view.base; 18 | 19 | import android.content.Intent; 20 | import android.os.Bundle; 21 | import androidx.annotation.NonNull; 22 | import androidx.annotation.Nullable; 23 | import androidx.appcompat.app.AppCompatActivity; 24 | 25 | import io.xujiaji.xmvp.presenters.XBasePresenter; 26 | import io.xujiaji.xmvp.utils.GenericHelper; 27 | import io.xujiaji.xmvp.view.interfaces.XActivityCycle; 28 | 29 | /** 30 | * 项目中Activity的基类
31 | * base Activity class 32 | */ 33 | public abstract class XBaseActivity extends AppCompatActivity implements XActivityCycle { 34 | protected T presenter; 35 | 36 | @Override 37 | protected void onCreate(@Nullable Bundle savedInstanceState) { 38 | onBeforeCreateCircle(); 39 | try{ 40 | presenter = GenericHelper.newPresenter(this); 41 | if (presenter != null) { 42 | presenter.start(); 43 | } 44 | }catch (Exception e) { 45 | e.printStackTrace(); 46 | } 47 | 48 | onPresenterCircle(presenter); 49 | super.onCreate(savedInstanceState); 50 | 51 | beforeSetContentView(); 52 | if (savedInstanceState != null) { 53 | // 页面已被启用,但因内存不足页面被系统销毁过 54 | // page start when the page was destroyed by the system due to insufficient memory 55 | onBundleHandle(savedInstanceState); 56 | } else { 57 | // 第一次进入页面获取上个页面传递过来的数据 58 | // handle intent when first enter the page 59 | Intent intent = getIntent(); 60 | if (intent != null) { 61 | onIntentHandle(intent); 62 | } 63 | } 64 | 65 | if (getContentId() == 0) { 66 | setContentView(layoutId()); 67 | } else { 68 | setContentView(getContentId()); 69 | } 70 | 71 | onInitCircle(); 72 | onListenerCircle(); 73 | 74 | onInit(); 75 | onListener(); 76 | 77 | } 78 | 79 | // 非standard的启动模式,第二次之后不会进入onCreate周期,转而是onNewIntent 80 | // Non-standard startup mode 81 | @Override 82 | protected void onNewIntent(Intent intent) { 83 | super.onNewIntent(intent); 84 | setIntent(intent); 85 | if (intent != null) { 86 | onIntentHandle(intent); 87 | } 88 | 89 | onInitCircle(); 90 | onListenerCircle(); 91 | 92 | onInit(); 93 | onListener(); 94 | } 95 | 96 | @Override 97 | public void onBeforeCreateCircle() { 98 | 99 | } 100 | 101 | @Override 102 | public void onPresenterCircle(T presenter) { 103 | 104 | } 105 | 106 | /** 107 | * 处理上个页面传递过来的值
108 | * Handle passed the previous page intent when first enter the page 109 | */ 110 | @Override 111 | public void onIntentHandle(@NonNull Intent intent) {} 112 | 113 | @Override 114 | public void onBundleHandle(@NonNull Bundle savedInstanceState) { 115 | 116 | } 117 | 118 | @Override 119 | public int layoutId() { return 0; } 120 | 121 | @Override 122 | public void onInitCircle() { } 123 | 124 | 125 | @Override 126 | public void onListenerCircle() { 127 | 128 | } 129 | 130 | /** 131 | * 需要在SetContentView之前做的操作 132 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onBeforeCreateCircle}
133 | * use {@link #onBeforeCreateCircle()}, now 134 | */ 135 | protected void beforeSetContentView() { 136 | } 137 | 138 | /** 139 | * 在这里面进行初始化 140 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onInitCircle()}
141 | * use {@link #onInitCircle()}, now 142 | */ 143 | protected void onInit() {} 144 | 145 | /** 146 | * 这里面写监听事件 147 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onListenerCircle()}
148 | * use {@link #onListenerCircle()}, now 149 | */ 150 | protected void onListener() {} 151 | 152 | /** 153 | * 获取布局的id 154 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #layoutId()}
155 | * use {@link #layoutId()}, now 156 | */ 157 | protected int getContentId() { return 0; } 158 | 159 | @Override 160 | protected void onDestroy() { 161 | super.onDestroy(); 162 | if (presenter != null) { 163 | presenter.end(); 164 | } 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/view/base/XBaseFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package io.xujiaji.xmvp.view.base; 17 | 18 | import android.app.Fragment; 19 | import android.content.Context; 20 | import android.os.Bundle; 21 | import androidx.annotation.NonNull; 22 | import androidx.annotation.Nullable; 23 | import android.view.LayoutInflater; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | 27 | import io.xujiaji.xmvp.presenters.XBasePresenter; 28 | import io.xujiaji.xmvp.utils.GenericHelper; 29 | import io.xujiaji.xmvp.view.interfaces.XFragViewCycle; 30 | 31 | /** 32 | * 33 | * 项目中Fragment的基类
base Fragment class
34 | * 部分代码参照(reference):https://github.com/xmagicj/LazyFragment/blob/master/app/src/main/java/com/xmagicj/android/lazyfragment/BaseFragment.java 35 | */ 36 | public abstract class XBaseFragment extends Fragment implements XFragViewCycle { 37 | 38 | protected T presenter; 39 | 40 | private View rootView; 41 | 42 | /** 43 | * Fragment是否可见状态
whether the Fragment is visible 44 | */ 45 | private boolean isFragmentVisible; 46 | 47 | /** 48 | * Layout已经初始化完成
Layout has been initialized 49 | */ 50 | private boolean isPrepared; 51 | 52 | /** 53 | * 是否第一次加载
whether first load 54 | */ 55 | private boolean isFirstLoad = true; 56 | 57 | /** 58 | *
 59 |      * 忽略isFirstLoad的值,强制刷新数据,但仍要Visible & Prepared
 60 |      * 一般用于PagerAdapter需要刷新各个子Fragment的场景
 61 |      * 不要new 新的 PagerAdapter 而采取reset数据的方式
 62 |      * 所以要求Fragment重新走initData方法
 63 |      * 故使用 {@link #setForceLoad(boolean)}来让Fragment下次执行initData
 64 |      * 
65 | * 66 | * force load 67 | *
 68 |      *     ignore isFirstLoad value, but still keep Visible & Prepared.
 69 |      *     use this when PagerAdapter need refresh multiple Fragment.
 70 |      *
 71 |      * 
72 | */ 73 | private boolean forceLoad = false; 74 | 75 | @Override 76 | public void onAttach(Context context) { 77 | super.onAttach(context); 78 | try{ 79 | presenter = GenericHelper.newPresenter(this); 80 | if (presenter != null) { 81 | presenter.start(); 82 | } 83 | }catch (Exception e) { 84 | e.printStackTrace(); 85 | } 86 | 87 | onPresenterCircle(presenter); 88 | } 89 | 90 | @Override 91 | public void onCreate(@Nullable Bundle savedInstanceState) { 92 | onBeforeCreateCircle(); 93 | super.onCreate(savedInstanceState); 94 | 95 | if (savedInstanceState != null) { 96 | onBundleHandle(savedInstanceState); 97 | } 98 | 99 | Bundle bundle = getArguments(); 100 | if (bundle != null && bundle.size() > 0) { 101 | onArgumentsHandle(bundle); 102 | } 103 | } 104 | 105 | @Nullable 106 | @Override 107 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 108 | if (getLayoutId() == 0) { 109 | rootView = inflater.inflate(layoutId(), container, false); 110 | } else { 111 | rootView = inflater.inflate(getLayoutId(), container, false); 112 | } 113 | isFirstLoad = true; 114 | isPrepared = true; 115 | if (!isInViewPager()) { 116 | isFragmentVisible = true; 117 | } 118 | onInitCircle(); 119 | onListenerCircle(); 120 | onInit(); 121 | onListener(); 122 | lazyLoad(); 123 | return rootView; 124 | } 125 | 126 | /*** 127 | * 如果是与ViewPager一起使用,调用的是setUserVisibleHint
If used with ViewPager, the call to setUserVisibleHint 128 | * @param isVisibleToUser 是否显示出来了 129 | */ 130 | @Override 131 | public void setUserVisibleHint(boolean isVisibleToUser) { 132 | super.setUserVisibleHint(isVisibleToUser); 133 | if (isVisibleToUser) { 134 | onVisible(); 135 | } else { 136 | onInvisible(); 137 | } 138 | } 139 | 140 | /** 141 | * 如果是通过FragmentTransaction的show和hide的方法来控制显示,调用的是onHiddenChanged. 此时您因该让 {@link #isInViewPager()} 返回false
142 | * If use by FragmentTransaction show or hide, the call to onHiddenChanged. now you need return false in {@link #isInViewPager()} 143 | * 144 | * @param hidden hidden True if the fragment is now hidden, false if it is not 145 | * visible. 146 | */ 147 | @Override 148 | public void onHiddenChanged(boolean hidden) { 149 | super.onHiddenChanged(hidden); 150 | if (hidden) { 151 | onInvisible(); 152 | } else { 153 | onVisible(); 154 | } 155 | } 156 | 157 | @Override 158 | public void onVisible() { 159 | isFragmentVisible = true; 160 | lazyLoad(); 161 | } 162 | 163 | @Override 164 | public void onInvisible() { 165 | isFragmentVisible = false; 166 | } 167 | 168 | @Override 169 | public void onBeforeCreateCircle() { } 170 | 171 | @Override 172 | public void onPresenterCircle(T presenter) { 173 | 174 | } 175 | 176 | @Override 177 | public void onBundleHandle(@NonNull Bundle savedInstanceState) { } 178 | 179 | @Override 180 | public void onArgumentsHandle(@NonNull Bundle bundle) { } 181 | 182 | @Override 183 | public void onListenerCircle() { } 184 | 185 | @Override 186 | public void onLazyLoad() { } 187 | 188 | @Override 189 | public void onInitCircle() { } 190 | 191 | @Override 192 | public int layoutId() { return 0; } 193 | 194 | /** 195 | * 在这里面进行初始化 196 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onInitCircle()}
197 | * use {@link #onInitCircle()}, now 198 | */ 199 | protected void onInit() {} 200 | 201 | /** 202 | * 这里面写监听事件 203 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onListenerCircle()}
204 | * use {@link #onListenerCircle()}, now 205 | */ 206 | protected void onListener() {} 207 | 208 | /** 209 | * 获取布局的id 210 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #layoutId()}
211 | * use {@link #layoutId()}, now 212 | */ 213 | protected int getLayoutId() { 214 | return 0; 215 | } 216 | 217 | public View getRootView() { 218 | return this.rootView; 219 | } 220 | 221 | private void lazyLoad() { 222 | if (isPrepared() && isFragmentVisible()) { 223 | if (isForceLoad() || isFirstLoad()) { 224 | forceLoad = false; 225 | isFirstLoad = false; 226 | onLazyLoad(); 227 | } 228 | } 229 | } 230 | 231 | @Override 232 | public void onDestroyView() { 233 | super.onDestroyView(); 234 | isPrepared = false; 235 | } 236 | 237 | @Override 238 | public void onDetach() { 239 | super.onDetach(); 240 | if (presenter != null) { 241 | presenter.end(); 242 | } 243 | } 244 | 245 | @Override 246 | public void setForceLoad(boolean forceLoad) { 247 | this.forceLoad = forceLoad; 248 | } 249 | 250 | @Override 251 | public boolean isForceLoad() { 252 | return forceLoad; 253 | } 254 | 255 | @Override 256 | public boolean isPrepared() { 257 | return isPrepared; 258 | } 259 | 260 | @Override 261 | public boolean isFirstLoad() { 262 | return isFirstLoad; 263 | } 264 | 265 | @Override 266 | public boolean isFragmentVisible() { 267 | return isFragmentVisible; 268 | } 269 | 270 | @Override 271 | public boolean isInViewPager() { 272 | return true; 273 | } 274 | } 275 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/view/base/v4/XBaseFragment.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.xmvp.view.base.v4; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import androidx.annotation.NonNull; 6 | import androidx.annotation.Nullable; 7 | import androidx.fragment.app.Fragment; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | import io.xujiaji.xmvp.presenters.XBasePresenter; 13 | import io.xujiaji.xmvp.utils.GenericHelper; 14 | import io.xujiaji.xmvp.view.interfaces.XFragViewCycle; 15 | 16 | 17 | /** 18 | * 19 | * 项目中Fragment的基类
base Fragment class
20 | * 部分代码参照(reference):https://github.com/xmagicj/LazyFragment/blob/master/app/src/main/java/com/xmagicj/android/lazyfragment/BaseFragment.java 21 | */ 22 | public abstract class XBaseFragment extends Fragment implements XFragViewCycle { 23 | 24 | protected T presenter; 25 | 26 | private View rootView; 27 | 28 | /** 29 | * Fragment是否可见状态
whether the Fragment is visible 30 | */ 31 | private boolean isFragmentVisible; 32 | 33 | /** 34 | * Layout已经初始化完成
Layout has been initialized 35 | */ 36 | private boolean isPrepared; 37 | 38 | /** 39 | * 是否第一次加载
whether first load 40 | */ 41 | private boolean isFirstLoad = true; 42 | 43 | /** 44 | *
 45 |      * 忽略isFirstLoad的值,强制刷新数据,但仍要Visible & Prepared
 46 |      * 一般用于PagerAdapter需要刷新各个子Fragment的场景
 47 |      * 不要new 新的 PagerAdapter 而采取reset数据的方式
 48 |      * 所以要求Fragment重新走initData方法
 49 |      * 故使用 {@link #setForceLoad(boolean)}来让Fragment下次执行initData
 50 |      * 
51 | * 52 | * force load 53 | *
 54 |      *     ignore isFirstLoad value, but still keep Visible & Prepared.
 55 |      *     use this when PagerAdapter need refresh multiple Fragment.
 56 |      *
 57 |      * 
58 | */ 59 | private boolean forceLoad = false; 60 | 61 | @Override 62 | public void onAttach(Context context) { 63 | super.onAttach(context); 64 | try{ 65 | presenter = GenericHelper.newPresenter(this); 66 | if (presenter != null) { 67 | presenter.start(); 68 | } 69 | }catch (Exception e) { 70 | e.printStackTrace(); 71 | } 72 | 73 | onPresenterCircle(presenter); 74 | } 75 | 76 | @Override 77 | public void onCreate(@Nullable Bundle savedInstanceState) { 78 | onBeforeCreateCircle(); 79 | super.onCreate(savedInstanceState); 80 | 81 | if (savedInstanceState != null) { 82 | onBundleHandle(savedInstanceState); 83 | } 84 | 85 | Bundle bundle = getArguments(); 86 | if (bundle != null && bundle.size() > 0) { 87 | onArgumentsHandle(bundle); 88 | } 89 | } 90 | 91 | @Nullable 92 | @Override 93 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 94 | if (getLayoutId() == 0) { 95 | rootView = inflater.inflate(layoutId(), container, false); 96 | } else { 97 | rootView = inflater.inflate(getLayoutId(), container, false); 98 | } 99 | isFirstLoad = true; 100 | isPrepared = true; 101 | if (!isInViewPager()) { 102 | isFragmentVisible = true; 103 | } 104 | onInitCircle(); 105 | onListenerCircle(); 106 | onInit(); 107 | onListener(); 108 | lazyLoad(); 109 | return rootView; 110 | } 111 | 112 | /*** 113 | * 如果是与ViewPager一起使用,调用的是setUserVisibleHint
If used with ViewPager, the call to setUserVisibleHint 114 | * @param isVisibleToUser 是否显示出来了 115 | */ 116 | @Override 117 | public void setUserVisibleHint(boolean isVisibleToUser) { 118 | super.setUserVisibleHint(isVisibleToUser); 119 | if (isVisibleToUser) { 120 | onVisible(); 121 | } else { 122 | onInvisible(); 123 | } 124 | } 125 | 126 | /** 127 | * 如果是通过FragmentTransaction的show和hide的方法来控制显示,调用的是onHiddenChanged. 此时您因该让 {@link #isInViewPager()} 返回false
128 | * If use by FragmentTransaction show or hide, the call to onHiddenChanged. now you need return false in {@link #isInViewPager()} 129 | * 130 | * @param hidden hidden True if the fragment is now hidden, false if it is not 131 | * visible. 132 | */ 133 | @Override 134 | public void onHiddenChanged(boolean hidden) { 135 | super.onHiddenChanged(hidden); 136 | if (hidden) { 137 | onInvisible(); 138 | } else { 139 | onVisible(); 140 | } 141 | } 142 | 143 | @Override 144 | public void onVisible() { 145 | isFragmentVisible = true; 146 | lazyLoad(); 147 | } 148 | 149 | @Override 150 | public void onInvisible() { 151 | isFragmentVisible = false; 152 | } 153 | 154 | @Override 155 | public void onBeforeCreateCircle() { } 156 | 157 | @Override 158 | public void onPresenterCircle(T presenter) { 159 | 160 | } 161 | 162 | @Override 163 | public void onBundleHandle(@NonNull Bundle savedInstanceState) { } 164 | 165 | @Override 166 | public void onArgumentsHandle(@NonNull Bundle bundle) { } 167 | 168 | @Override 169 | public void onListenerCircle() { } 170 | 171 | @Override 172 | public void onLazyLoad() { } 173 | 174 | @Override 175 | public void onInitCircle() { } 176 | 177 | @Override 178 | public int layoutId() { return 0; } 179 | 180 | /** 181 | * 在这里面进行初始化 182 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onInitCircle()}
183 | * use {@link #onInitCircle()}, now 184 | */ 185 | protected void onInit() {} 186 | 187 | /** 188 | * 这里面写监听事件 189 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #onListenerCircle()}
190 | * use {@link #onListenerCircle()}, now 191 | */ 192 | protected void onListener() {} 193 | 194 | /** 195 | * 获取布局的id 196 | * @deprecated 将弃用该方法,需尽快改为使用 {@link #layoutId()}
197 | * use {@link #layoutId()}, now 198 | */ 199 | protected int getLayoutId() { 200 | return 0; 201 | } 202 | 203 | public View getRootView() { 204 | return this.rootView; 205 | } 206 | 207 | private void lazyLoad() { 208 | if (isPrepared() && isFragmentVisible()) { 209 | if (isForceLoad() || isFirstLoad()) { 210 | forceLoad = false; 211 | isFirstLoad = false; 212 | onLazyLoad(); 213 | } 214 | } 215 | } 216 | 217 | @Override 218 | public void onDestroyView() { 219 | super.onDestroyView(); 220 | isPrepared = false; 221 | } 222 | 223 | @Override 224 | public void onDetach() { 225 | super.onDetach(); 226 | if (presenter != null) { 227 | presenter.end(); 228 | } 229 | } 230 | 231 | @Override 232 | public void setForceLoad(boolean forceLoad) { 233 | this.forceLoad = forceLoad; 234 | } 235 | 236 | @Override 237 | public boolean isForceLoad() { 238 | return forceLoad; 239 | } 240 | 241 | @Override 242 | public boolean isPrepared() { 243 | return isPrepared; 244 | } 245 | 246 | @Override 247 | public boolean isFirstLoad() { 248 | return isFirstLoad; 249 | } 250 | 251 | @Override 252 | public boolean isFragmentVisible() { 253 | return isFragmentVisible; 254 | } 255 | 256 | @Override 257 | public boolean isInViewPager() { 258 | return true; 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/view/interfaces/XActivityCycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.xujiaji.xmvp.view.interfaces; 18 | 19 | import android.content.Intent; 20 | import androidx.annotation.NonNull; 21 | 22 | import io.xujiaji.xmvp.presenters.XBasePresenter; 23 | 24 | /** 25 | * author: xujiaji 26 | * created on: 2018/9/11 15:05 27 | * description: 定义Activity View相关周期
Define Activity View related Cycle 28 | */ 29 | public interface XActivityCycle extends XViewCycle { 30 | /** 31 | * 处理上个页面传递过来的数据
Handle the data passed from the previous page 32 | */ 33 | void onIntentHandle(@NonNull Intent intent); 34 | } 35 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/view/interfaces/XFragViewCycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.xujiaji.xmvp.view.interfaces; 18 | 19 | import android.app.Fragment; 20 | import android.os.Bundle; 21 | import androidx.annotation.NonNull; 22 | 23 | import io.xujiaji.xmvp.presenters.XBasePresenter; 24 | 25 | /** 26 | * author: xujiaji 27 | * created on: 2018/9/4 10:57 28 | * description: 定义Fragment View相关周期
Define Fragment View related Cycle 29 | */ 30 | public interface XFragViewCycle extends XViewCycle { 31 | 32 | /** 33 | * 处理{@link Fragment#getArguments()} 的值,如果有才会调用
Handle the value of {@link Fragment#getArguments()} , if it is there, it will be called 34 | * @param bundle 35 | */ 36 | void onArgumentsHandle(@NonNull Bundle bundle); 37 | 38 | void onVisible(); 39 | 40 | void onInvisible(); 41 | 42 | void onLazyLoad(); 43 | 44 | /** 45 | * 忽略{@link #isFirstLoad() }的值,强制刷新数据,但仍要满足 {@link #isFragmentVisible()} && {@link #isPrepared()}
46 | * Ignore the value of {@link #isFirstLoad() } to force refresh data, but still satisfy {@link #isFragmentVisible()} && {@link #isPrepared()} 47 | */ 48 | void setForceLoad(boolean forceLoad); 49 | 50 | boolean isForceLoad(); 51 | 52 | boolean isPrepared(); 53 | 54 | boolean isFirstLoad(); 55 | 56 | boolean isFragmentVisible(); 57 | 58 | /** 59 | * 是否是在ViewPager中,默认为true 60 | * whether in ViewPager, default is true 61 | * @return 62 | */ 63 | boolean isInViewPager(); 64 | 65 | } 66 | -------------------------------------------------------------------------------- /xmvp/src/main/java/io/xujiaji/xmvp/view/interfaces/XViewCycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 XuJiaji 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.xujiaji.xmvp.view.interfaces; 18 | 19 | import android.os.Bundle; 20 | import androidx.annotation.NonNull; 21 | 22 | import io.xujiaji.xmvp.presenters.XBasePresenter; 23 | 24 | /** 25 | * author: xujiaji 26 | * created on: 2018/9/4 10:57 27 | * description: 定义View相关周期
Define View related Cycle 28 | */ 29 | public interface XViewCycle { 30 | 31 | /** 32 | * 在 super {@link android.app.Activity#onCreate(Bundle)}之前被调用
will be called before super class {@link android.app.Activity#onCreate(Bundle)} called 33 | */ 34 | void onBeforeCreateCircle(); 35 | 36 | void onPresenterCircle(T presenter); 37 | 38 | /** 39 | * 在 super {@link android.app.Activity#onCreate(Bundle)}之前被调用,并且有Bundle
will be called before super class {@link android.app.Activity#onCreate(Bundle)} called 40 | * @param savedInstanceState 该参数不可能为null
this parameter cannot be null 41 | */ 42 | void onBundleHandle(@NonNull Bundle savedInstanceState); 43 | 44 | /** 45 | * 获取布局的id
get layout id 46 | * 在 {@link #onBeforeCreateCircle }之后被调用
will be called after {@link #onBeforeCreateCircle } called 47 | * @return xml布局id
xml layout id 48 | */ 49 | int layoutId(); 50 | 51 | /** 52 | * 在这里面进行初始化
initialize here 53 | * 在 {@link #layoutId()} 之后被调用
will be called after {@link #layoutId()} called 54 | */ 55 | void onInitCircle(); 56 | 57 | /** 58 | * 这里面写监听事件
write listens event here 59 | * 在 {@link #onInitCircle()} 之后被调用
will be called after {@link #onInitCircle()} called 60 | */ 61 | void onListenerCircle(); 62 | 63 | } 64 | -------------------------------------------------------------------------------- /xmvp/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | xmvp 3 | 4 | -------------------------------------------------------------------------------- /xmvp/src/test/java/io/xujiaji/xmvp/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.xujiaji.sample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } --------------------------------------------------------------------------------