├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── libraries │ ├── animated_vector_drawable_25_3_1.xml │ ├── appcompat_v7_25_3_1.xml │ ├── constraint_layout_1_0_2.xml │ ├── constraint_layout_solver_1_0_2.xml │ ├── espresso_core_2_2_2.xml │ ├── espresso_idling_resource_2_2_2.xml │ ├── exposed_instrumentation_api_publish_0_5.xml │ ├── hamcrest_core_1_3.xml │ ├── hamcrest_integration_1_3.xml │ ├── hamcrest_library_1_3.xml │ ├── javawriter_2_1_1.xml │ ├── javax_annotation_api_1_2.xml │ ├── javax_inject_1.xml │ ├── jsr305_2_0_1.xml │ ├── junit_4_12.xml │ ├── rules_0_5.xml │ ├── runner_0_5.xml │ ├── support_annotations_25_3_1.xml │ ├── support_compat_25_3_1.xml │ ├── support_core_ui_25_3_1.xml │ ├── support_core_utils_25_3_1.xml │ ├── support_fragment_25_3_1.xml │ ├── support_media_compat_25_3_1.xml │ ├── support_v4_25_3_1.xml │ └── support_vector_drawable_25_3_1.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zht │ │ └── networkstateview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zht │ │ │ └── networkstateview │ │ │ ├── App.java │ │ │ ├── ui │ │ │ ├── activity │ │ │ │ └── MainActivity.java │ │ │ └── widget │ │ │ │ └── NetworkStateView.java │ │ │ └── utils │ │ │ └── UIUtils.java │ └── res │ │ ├── drawable-hdpi │ │ ├── ic_empty.png │ │ ├── ic_net_error.png │ │ ├── ic_no_network.png │ │ └── ic_refresh.png │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── view_empty.xml │ │ ├── view_loading.xml │ │ ├── view_network_error.xml │ │ ├── view_network_state.xml │ │ └── view_no_network.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── network_state_view.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zht │ └── networkstateview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots ├── screenshot1.png ├── screenshot2.png ├── screenshot3.png └── screenshot4.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/libraries/animated_vector_drawable_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/appcompat_v7_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/constraint_layout_1_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/libraries/constraint_layout_solver_1_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/espresso_core_2_2_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/espresso_idling_resource_2_2_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/exposed_instrumentation_api_publish_0_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/hamcrest_integration_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/hamcrest_library_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/javawriter_2_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/javax_annotation_api_1_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/javax_inject_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/jsr305_2_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/junit_4_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/rules_0_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/runner_0_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_annotations_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_compat_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_core_ui_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_core_utils_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_fragment_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_media_compat_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/support_v4_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/libraries/support_vector_drawable_25_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NetworkStateView 2 | 一个在进行数据加载时显示多种状态的自定义View,包括加载中丶加载成功丶加载失败丶网络异常丶没有网络丶无数据 3 | 4 | ![](https://github.com/AlarmZeng/NetworkStateView/blob/master/screenshots/screenshot1.png) 5 | ![](https://github.com/AlarmZeng/NetworkStateView/blob/master/screenshots/screenshot2.png) 6 | ![](https://github.com/AlarmZeng/NetworkStateView/blob/master/screenshots/screenshot3.png) 7 | ![](https://github.com/AlarmZeng/NetworkStateView/blob/master/screenshots/screenshot4.png) 8 | 9 | ## 使用 10 | 11 | ### 设置状态布局文件和默认样式 12 | 13 | * 直接在布局文件中声明 14 | 15 | 29 | 30 | 31 | 32 | * 在styles文件进行设置 33 | 34 | 39 | 40 | 45 | 46 | ### 属性 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | ### 示例 84 | 85 | NetworkStateView networkStateView = (NetworkStateView) findViewById(R.id.nsv_state_view); 86 | 87 | //加载中 88 | networkStateView.showLoading(); 89 | //加载失败(网络错误) 90 | networkStateView.showError(); 91 | //没有网络 92 | networkStateView.showNoNetwork(); 93 | //没有数据 94 | networkStateView.showEmpty(); 95 | 96 | ## 注意 97 | 98 | 在对状态View进行控件id设置时,需要根据下面规则进行设置: 99 | 100 | * 加载失败显示的图片id:error_image 101 | * 加载失败显示的文字id:error_text 102 | * 没有数据显示的图片id:empty_image 103 | * 没有数据显示的文字id:empty_text 104 | * 没有网络显示的图片id:no_network_image 105 | * 没有网络显示的文字id:no_network_text 106 | * 进行刷新显示的图片id:refresh_view 107 | 108 | 详细可见代码 109 | 110 | ## 文章 111 | 112 | 对**NetworkStateView**的详细介绍可以查看文章[NetworkStateView](http://www.jianshu.com/p/858d41972d15) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | # Gradle files 2 | build/ 3 | .gradle/ 4 | 5 | # Android Studio 6 | *.iml 7 | .idea 8 | 9 | # Intellij project files 10 | *.ipr 11 | *.iws 12 | .idea/ 13 | 14 | # Local configuration file (sdk path, etc) 15 | local.properties 16 | 17 | /.idea/workspace.xml 18 | /.idea/libraries 19 | .DS_Store 20 | /captures 21 | 22 | #built application files 23 | #*.apk 24 | *.ap_ 25 | 26 | # files for the dex VM 27 | *.dex 28 | 29 | # generated files 30 | bin/ 31 | gen/ 32 | 33 | # Windows thumbnail db 34 | Thumbs.db 35 | 36 | # Java class files 37 | *.class 38 | # Eclipse project files 39 | .classpath 40 | .project 41 | # Eclipse Metadata 42 | .metadata/ 43 | # Proguard folder generated by Eclipse 44 | proguard/ 45 | 46 | # Log Files 47 | *.log 48 | 49 | #NDK 50 | obj/ -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.zht.networkstateview" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /app/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 E:\AndroidSDK\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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zht/networkstateview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zht.networkstateview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zht.networkstateview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/zht/networkstateview/App.java: -------------------------------------------------------------------------------- 1 | package com.zht.networkstateview; 2 | 3 | import android.app.Application; 4 | import android.os.Handler; 5 | 6 | /** 7 | * Created by ZHT on 2017/4/15. 8 | */ 9 | 10 | public class App extends Application { 11 | 12 | private static App mContext; 13 | 14 | private static Handler mMainThreadHandler; 15 | 16 | @Override 17 | public void onCreate() { 18 | super.onCreate(); 19 | 20 | mContext = this; 21 | mMainThreadHandler = new Handler(); 22 | } 23 | 24 | public static App getApplication() { 25 | return mContext; 26 | } 27 | 28 | public static Handler getMainThreadHandler() { 29 | return mMainThreadHandler; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/zht/networkstateview/ui/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zht.networkstateview.ui.activity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | 8 | import com.zht.networkstateview.App; 9 | import com.zht.networkstateview.R; 10 | import com.zht.networkstateview.ui.widget.NetworkStateView; 11 | 12 | public class MainActivity extends AppCompatActivity implements View.OnClickListener, NetworkStateView.OnRefreshListener { 13 | 14 | private NetworkStateView networkStateView; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | initView(); 22 | } 23 | 24 | private void initView() { 25 | 26 | networkStateView = (NetworkStateView) findViewById(R.id.nsv_state_view); 27 | 28 | Button bt_loading = (Button) findViewById(R.id.bt_loading); 29 | Button bt_error = (Button) findViewById(R.id.bt_error); 30 | Button bt_noNetwork = (Button) findViewById(R.id.bt_no_network); 31 | Button bt_empty = (Button) findViewById(R.id.bt_empty); 32 | 33 | bt_loading.setOnClickListener(this); 34 | bt_error.setOnClickListener(this); 35 | bt_noNetwork.setOnClickListener(this); 36 | bt_empty.setOnClickListener(this); 37 | 38 | networkStateView.setOnRefreshListener(this); 39 | networkStateView.showLoading(); 40 | showSuccess(); 41 | } 42 | 43 | @Override 44 | public void onClick(View view) { 45 | switch (view.getId()) { 46 | case R.id.bt_loading: 47 | networkStateView.showLoading(); 48 | break; 49 | 50 | case R.id.bt_error: 51 | networkStateView.showError(); 52 | break; 53 | 54 | case R.id.bt_no_network: 55 | networkStateView.showNoNetwork(); 56 | break; 57 | 58 | case R.id.bt_empty: 59 | networkStateView.showEmpty(); 60 | break; 61 | 62 | default: 63 | break; 64 | } 65 | 66 | showSuccess(); 67 | } 68 | 69 | @Override 70 | public void onRefresh() { 71 | networkStateView.showLoading(); 72 | showSuccess(); 73 | } 74 | 75 | private void showSuccess() { 76 | App.getMainThreadHandler().postDelayed(new Runnable() { 77 | @Override 78 | public void run() { 79 | networkStateView.showSuccess(); 80 | } 81 | }, 2000); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/zht/networkstateview/ui/widget/NetworkStateView.java: -------------------------------------------------------------------------------- 1 | package com.zht.networkstateview.ui.widget; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.support.annotation.AttrRes; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.text.TextUtils; 9 | import android.util.AttributeSet; 10 | import android.util.TypedValue; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.ImageView; 15 | import android.widget.LinearLayout; 16 | import android.widget.TextView; 17 | 18 | import com.zht.networkstateview.R; 19 | import com.zht.networkstateview.utils.UIUtils; 20 | 21 | /** 22 | * Created by ZHT on 2017/4/15. 23 | * 加载状态的自定义View 24 | */ 25 | 26 | public class NetworkStateView extends LinearLayout { 27 | 28 | //当前的加载状态 29 | private int mCurrentState; 30 | private static final int STATE_SUCCESS = 0; 31 | private static final int STATE_LOADING = 1; 32 | private static final int STATE_NETWORK_ERROR = 2; 33 | private static final int STATE_NO_NETWORK = 3; 34 | private static final int STATE_EMPTY = 4; 35 | 36 | //布局添加位置 37 | private static final int VIEW_POSITION = 0; 38 | 39 | private int mLoadingViewId; 40 | 41 | private int mErrorViewId; 42 | private int mErrorImageId; 43 | private String mErrorText; 44 | 45 | private int mNoNetworkViewId; 46 | private int mNoNetworkImageId; 47 | private String mNoNetworkText; 48 | 49 | private int mEmptyViewId; 50 | private int mEmptyImageId; 51 | private String mEmptyText; 52 | 53 | private int mRefreshViewId; 54 | 55 | private int mTextColor; 56 | private int mTextSize; 57 | 58 | private View mLoadingView; 59 | private View mErrorView; 60 | private View mNoNetworkView; 61 | private View mEmptyView; 62 | 63 | private LayoutInflater mInflater; 64 | private ViewGroup.LayoutParams params; 65 | 66 | private OnRefreshListener mRefreshListener; 67 | 68 | public NetworkStateView(@NonNull Context context) { 69 | this(context, null); 70 | } 71 | 72 | public NetworkStateView(@NonNull Context context, @Nullable AttributeSet attrs) { 73 | this(context, attrs, R.attr.styleNetworkStateView); 74 | } 75 | 76 | public NetworkStateView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { 77 | super(context, attrs, defStyleAttr); 78 | 79 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NetworkStateView, defStyleAttr, R.style.NetworkStateView_Style); 80 | 81 | mLoadingViewId = typedArray.getResourceId(R.styleable.NetworkStateView_loadingView, R.layout.view_loading); 82 | 83 | mErrorViewId = typedArray.getResourceId(R.styleable.NetworkStateView_errorView, R.layout.view_network_error); 84 | mErrorImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvErrorImage, NO_ID); 85 | mErrorText = typedArray.getString(R.styleable.NetworkStateView_nsvErrorText); 86 | 87 | mNoNetworkViewId = typedArray.getResourceId(R.styleable.NetworkStateView_noNetworkView, R.layout.view_no_network); 88 | mNoNetworkImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvNoNetworkImage, NO_ID); 89 | mNoNetworkText = typedArray.getString(R.styleable.NetworkStateView_nsvNoNetworkText); 90 | 91 | mEmptyViewId = typedArray.getResourceId(R.styleable.NetworkStateView_emptyView, R.layout.view_empty); 92 | mEmptyImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvEmptyImage, NO_ID); 93 | mEmptyText = typedArray.getString(R.styleable.NetworkStateView_nsvEmptyText); 94 | 95 | mRefreshViewId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvRefreshImage, NO_ID); 96 | 97 | mTextColor = typedArray.getColor(R.styleable.NetworkStateView_nsvTextColor, 0x8a000000); 98 | mTextSize = typedArray.getDimensionPixelSize(R.styleable.NetworkStateView_nsvTextSize, UIUtils.dp2px(14)); 99 | 100 | typedArray.recycle(); 101 | 102 | mInflater = LayoutInflater.from(context); 103 | params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 104 | setBackgroundColor(UIUtils.getColor(R.color.white)); 105 | } 106 | 107 | @Override 108 | protected void onFinishInflate() { 109 | super.onFinishInflate(); 110 | showSuccess(); 111 | } 112 | 113 | /** 114 | * 加载成功,隐藏加载状态的View 115 | */ 116 | public void showSuccess() { 117 | mCurrentState = STATE_SUCCESS; 118 | showViewByState(mCurrentState); 119 | } 120 | 121 | /** 122 | * 显示加载中的状态 123 | */ 124 | public void showLoading() { 125 | mCurrentState = STATE_LOADING; 126 | if (null == mLoadingView) { 127 | mLoadingView = mInflater.inflate(mLoadingViewId, null); 128 | addView(mLoadingView, VIEW_POSITION, params); 129 | } 130 | showViewByState(mCurrentState); 131 | } 132 | 133 | /** 134 | * 显示加载失败(网络错误)状态 135 | */ 136 | public void showError() { 137 | mCurrentState = STATE_NETWORK_ERROR; 138 | if (null == mErrorView) { 139 | mErrorView = mInflater.inflate(mErrorViewId, null); 140 | ImageView errorImage = (ImageView) mErrorView.findViewById(R.id.error_image); 141 | TextView errorText = (TextView) mErrorView.findViewById(R.id.error_text); 142 | ImageView errorRefreshView = (ImageView) mErrorView.findViewById(R.id.refresh_view); 143 | 144 | image(errorImage, mErrorImageId); 145 | 146 | text(errorText, mErrorText); 147 | 148 | image(errorRefreshView, mRefreshViewId); 149 | 150 | if (null != errorRefreshView) { 151 | errorRefreshView.setOnClickListener(new OnClickListener() { 152 | @Override 153 | public void onClick(View view) { 154 | if (null != mRefreshListener) { 155 | mRefreshListener.onRefresh(); 156 | } 157 | } 158 | }); 159 | } 160 | addView(mErrorView, VIEW_POSITION, params); 161 | } 162 | showViewByState(mCurrentState); 163 | } 164 | 165 | /** 166 | * 显示没有网络状态 167 | */ 168 | public void showNoNetwork() { 169 | mCurrentState = STATE_NO_NETWORK; 170 | if (null == mNoNetworkView) { 171 | mNoNetworkView = mInflater.inflate(mNoNetworkViewId, null); 172 | ImageView noNetworkImage = (ImageView) mNoNetworkView.findViewById(R.id.no_network_image); 173 | TextView noNetworkText = (TextView) mNoNetworkView.findViewById(R.id.no_network_text); 174 | ImageView networkRefreshView = (ImageView) mNoNetworkView.findViewById(R.id.refresh_view); 175 | 176 | image(noNetworkImage, mNoNetworkImageId); 177 | 178 | text(noNetworkText, mNoNetworkText); 179 | 180 | image(networkRefreshView, mRefreshViewId); 181 | 182 | if (null != networkRefreshView) { 183 | networkRefreshView.setOnClickListener(new OnClickListener() { 184 | @Override 185 | public void onClick(View view) { 186 | if (null != mRefreshListener) { 187 | mRefreshListener.onRefresh(); 188 | } 189 | } 190 | }); 191 | } 192 | addView(mNoNetworkView, VIEW_POSITION, params); 193 | } 194 | showViewByState(mCurrentState); 195 | } 196 | 197 | /** 198 | * 显示无数据状态 199 | */ 200 | public void showEmpty() { 201 | mCurrentState = STATE_EMPTY; 202 | if (null == mEmptyView) { 203 | mEmptyView = mInflater.inflate(mEmptyViewId, null); 204 | ImageView emptyImage = (ImageView) mEmptyView.findViewById(R.id.empty_image); 205 | TextView emptyText = (TextView) mEmptyView.findViewById(R.id.empty_text); 206 | ImageView emptyRefreshView = (ImageView) mEmptyView.findViewById(R.id.refresh_view); 207 | 208 | image(emptyImage, mEmptyImageId); 209 | 210 | text(emptyText, mEmptyText); 211 | 212 | image(emptyRefreshView, mRefreshViewId); 213 | 214 | if (null != emptyRefreshView) { 215 | emptyRefreshView.setOnClickListener(new OnClickListener() { 216 | @Override 217 | public void onClick(View view) { 218 | if (null != mRefreshListener) { 219 | mRefreshListener.onRefresh(); 220 | } 221 | } 222 | }); 223 | } 224 | addView(mEmptyView, VIEW_POSITION, params); 225 | } 226 | showViewByState(mCurrentState); 227 | } 228 | 229 | private void showViewByState(int state) { 230 | 231 | //如果当前状态为加载成功,隐藏此View,反之显示 232 | this.setVisibility(state == STATE_SUCCESS ? View.GONE : View.VISIBLE); 233 | 234 | if (null != mLoadingView) { 235 | mLoadingView.setVisibility(state == STATE_LOADING ? View.VISIBLE : View.GONE); 236 | } 237 | 238 | if (null != mErrorView) { 239 | mErrorView.setVisibility(state == STATE_NETWORK_ERROR ? View.VISIBLE : View.GONE); 240 | } 241 | 242 | if (null != mNoNetworkView) { 243 | mNoNetworkView.setVisibility(state == STATE_NO_NETWORK ? View.VISIBLE : View.GONE); 244 | } 245 | 246 | if (null != mEmptyView) { 247 | mEmptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.GONE); 248 | } 249 | } 250 | 251 | private void image(ImageView view, int imageId) { 252 | if (null != view && imageId != NO_ID) { 253 | view.setImageResource(imageId); 254 | } 255 | } 256 | 257 | private void text(TextView view, String str) { 258 | if (null != view && !TextUtils.isEmpty(str)) { 259 | view.setText(str); 260 | view.setTextColor(mTextColor); 261 | view.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); 262 | } 263 | } 264 | 265 | public void setOnRefreshListener(OnRefreshListener listener) { 266 | mRefreshListener = listener; 267 | } 268 | 269 | public interface OnRefreshListener { 270 | void onRefresh(); 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /app/src/main/java/com/zht/networkstateview/utils/UIUtils.java: -------------------------------------------------------------------------------- 1 | package com.zht.networkstateview.utils; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.graphics.Color; 6 | import android.graphics.drawable.Drawable; 7 | import android.os.Build; 8 | import android.support.annotation.RequiresApi; 9 | import android.util.DisplayMetrics; 10 | import android.view.WindowManager; 11 | 12 | import com.zht.networkstateview.App; 13 | 14 | /** 15 | * Created by ZHT on 2017/4/17. 16 | * 有关UI的工具类,如获取资源(颜色,字符串,drawable等), 17 | * 屏幕宽高,dp与px转换 18 | */ 19 | 20 | public class UIUtils { 21 | 22 | private static Context getContext() { 23 | return App.getApplication(); 24 | } 25 | 26 | private static Resources getResources() { 27 | return getContext().getResources(); 28 | } 29 | 30 | /** 31 | * 获取颜色值 32 | * @param resId 颜色资源id 33 | * @return 颜色值 34 | */ 35 | public static int getColor(int resId) { 36 | return getResources().getColor(resId); 37 | } 38 | 39 | @RequiresApi(api = Build.VERSION_CODES.M) 40 | public static int getColor(int resId, Resources.Theme theme) { 41 | return getResources().getColor(resId, theme); 42 | } 43 | 44 | public static int getColor(String color) { 45 | return Color.parseColor(color); 46 | } 47 | 48 | /** 49 | * 获取Drawable 50 | * @param resTd Drawable资源id 51 | * @return Drawable 52 | */ 53 | public static Drawable getDrawable(int resTd) { 54 | return getResources().getDrawable(resTd); 55 | } 56 | 57 | /** 58 | * 获取字符串 59 | * @param resId 字符串资源id 60 | * @return 字符串 61 | */ 62 | public static String getString(int resId) { 63 | return getResources().getString(resId); 64 | } 65 | 66 | /** 67 | * 获取字符串数组 68 | * @param resId 数组资源id 69 | * @return 字符串数组 70 | */ 71 | public static String[] getStringArray(int resId) { 72 | return getResources().getStringArray(resId); 73 | } 74 | 75 | /** 76 | * 将dp值转换为px值 77 | * @param dp 需要转换的dp值 78 | * @return px值 79 | */ 80 | public static int dp2px(float dp) { 81 | return (int) (getResources().getDisplayMetrics().density * dp + 0.5f); 82 | } 83 | 84 | /** 85 | * 将px值转换为dp值 86 | * @param px 需要转换的px值 87 | * @return dp值 88 | */ 89 | public static int px2dp(float px) { 90 | return (int) (px / getResources().getDisplayMetrics().density + 0.5f); 91 | } 92 | 93 | public static DisplayMetrics getDisplayMetrics() { 94 | 95 | DisplayMetrics displayMetrics = new DisplayMetrics(); 96 | WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 97 | windowManager.getDefaultDisplay().getMetrics(displayMetrics); 98 | 99 | return displayMetrics; 100 | } 101 | 102 | /** 103 | * 获取屏幕宽度 像素值 104 | * @return 屏幕宽度 105 | */ 106 | public static int getScreenWidth() { 107 | return getDisplayMetrics().widthPixels; 108 | } 109 | 110 | /** 111 | * 获取屏幕高度 像素值 112 | * @return 屏幕高度 113 | */ 114 | public static int getScreenHegith() { 115 | return getDisplayMetrics().heightPixels; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_empty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlarmZeng/NetworkStateView/36ce0e06d97599ab305129b87f01a2fa81ec00c8/app/src/main/res/drawable-hdpi/ic_empty.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_net_error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlarmZeng/NetworkStateView/36ce0e06d97599ab305129b87f01a2fa81ec00c8/app/src/main/res/drawable-hdpi/ic_net_error.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_no_network.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlarmZeng/NetworkStateView/36ce0e06d97599ab305129b87f01a2fa81ec00c8/app/src/main/res/drawable-hdpi/ic_no_network.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_refresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlarmZeng/NetworkStateView/36ce0e06d97599ab305129b87f01a2fa81ec00c8/app/src/main/res/drawable-hdpi/ic_refresh.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 |