├── .gitignore ├── LICENSE ├── README.md ├── anyview.gif ├── loadingandretrymanager ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zhy │ │ └── base │ │ └── loadandretry │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zhy │ │ └── base │ │ └── loadandretry │ │ ├── LoadingAndRetryLayout.java │ │ ├── LoadingAndRetryManager.java │ │ ├── OnLoadingAndRetryListener.java │ │ └── test │ │ ├── AnyViewTestActivity.java │ │ ├── CategoryActivity.java │ │ ├── FragmentTestActivity.java │ │ ├── MainActivity.java │ │ ├── MyApplication.java │ │ └── NormalFragment.java │ └── res │ ├── layout │ ├── activity_anyview_test.xml │ ├── activity_category.xml │ ├── activity_fragment_test.xml │ ├── activity_main.xml │ ├── base_empty.xml │ ├── base_loading.xml │ ├── base_retry.xml │ ├── fragment_layout.xml │ └── item_category.xml │ ├── menu │ ├── menu_category.xml │ ├── menu_fragment_test.xml │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── loadingandtry.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 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 | -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LoadingAndRetryManager 2 | 3 | 无缝为Activity、Fragment、任何View设置等待(loading)、重试(retry)、无数据(empty)页面。 4 | 5 | 6 | ## How to Use 7 | 8 | 如果多个页面共享加载和重试页面,建议全局设置个基本的。比如在Application中: 9 | 10 | ```java 11 | public class MyApplication extends Application 12 | { 13 | @Override 14 | public void onCreate() 15 | { 16 | super.onCreate(); 17 | LoadingAndRetryManager.BASE_RETRY_LAYOUT_ID = R.layout.base_retry; 18 | LoadingAndRetryManager.BASE_LOADING_LAYOUT_ID = R.layout.base_loading; 19 | LoadingAndRetryManager.BASE_EMPTY_LAYOUT_ID = R.layout.base_empty; 20 | } 21 | } 22 | ``` 23 | 24 | 在Activity中: 25 | 26 | ```java 27 | public class MainActivity extends AppCompatActivity 28 | { 29 | LoadingAndRetryManager mLoadingAndRetryManager; 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) 33 | { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | 37 | mLoadingAndRetryManager = LoadingAndRetryManager.generate(this, listener); 38 | 39 | loadData(); 40 | 41 | } 42 | ``` 43 | 44 | 只需要在onCreate中调用`LoadingAndRetryManager.generate(this,callback)`即可。 45 | 46 | * 在Fragment中与Activity中用法一致。 47 | 48 | * 为任何View添加,只需要将第一个参数改成对应的View即可。 49 | 50 | 51 | 如果需要针对单个Activity、Fragment、View定制页面,重写接口的回调方法: 52 | 53 | ```java 54 | public View generateLoadingLayout() 55 | { 56 | return null; 57 | } 58 | 59 | public View generateRetryLayout() 60 | { 61 | return null; 62 | } 63 | 64 | public View generateEmptyLayout() 65 | { 66 | return null; 67 | } 68 | ``` 69 | 即可,针对每个页面都有对应的设置事件的回调,如果有需求直接复写。 70 | 71 | ### API 72 | 73 | * mLoadingAndRetryManager.showContent(); 74 | * mLoadingAndRetryManager.showRetry(); 75 | * mLoadingAndRetryManager.showLoading(); 76 | * mLoadingAndRetryManager.showEmpty(); 77 | 78 | ## 效果图 79 | 80 | * In Activity or Fragment 81 | 82 | 83 | 84 | * In Any View 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /anyview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/LoadingAndRetryManager/b739a4cef02a4e336c3d64922ce761ee16422868/anyview.gif -------------------------------------------------------------------------------- /loadingandretrymanager/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /loadingandretrymanager/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "23.0.0 rc3" 6 | 7 | defaultConfig { 8 | applicationId "com.zhy.base.loadandretry" 9 | minSdkVersion 10 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 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 | compile 'com.android.support:appcompat-v7:22.2.1' 25 | } 26 | -------------------------------------------------------------------------------- /loadingandretrymanager/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 /Users/zhy/android/sdk/android-sdk-macosx/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 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/androidTest/java/com/zhy/base/loadandretry/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase 10 | { 11 | public ApplicationTest() 12 | { 13 | super(Application.class); 14 | } 15 | } -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/LoadingAndRetryLayout.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry; 2 | 3 | import android.content.Context; 4 | import android.os.Looper; 5 | import android.util.AttributeSet; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.widget.FrameLayout; 10 | 11 | /** 12 | * Created by zhy on 15/8/26. 13 | */ 14 | public class LoadingAndRetryLayout extends FrameLayout 15 | { 16 | private View mLoadingView; 17 | private View mRetryView; 18 | private View mContentView; 19 | private View mEmptyView; 20 | private LayoutInflater mInflater; 21 | 22 | private static final String TAG = LoadingAndRetryLayout.class.getSimpleName(); 23 | 24 | 25 | public LoadingAndRetryLayout(Context context, AttributeSet attrs, int defStyleAttr) 26 | { 27 | super(context, attrs, defStyleAttr); 28 | mInflater = LayoutInflater.from(context); 29 | } 30 | 31 | 32 | public LoadingAndRetryLayout(Context context, AttributeSet attrs) 33 | { 34 | this(context, attrs, -1); 35 | } 36 | 37 | public LoadingAndRetryLayout(Context context) 38 | { 39 | this(context, null); 40 | } 41 | 42 | private boolean isMainThread() 43 | { 44 | return Looper.myLooper() == Looper.getMainLooper(); 45 | } 46 | 47 | public void showLoading() 48 | { 49 | if (isMainThread()) 50 | { 51 | showView(mLoadingView); 52 | } else 53 | { 54 | post(new Runnable() 55 | { 56 | @Override 57 | public void run() 58 | { 59 | showView(mLoadingView); 60 | } 61 | }); 62 | } 63 | } 64 | 65 | public void showRetry() 66 | { 67 | if (isMainThread()) 68 | { 69 | showView(mRetryView); 70 | } else 71 | { 72 | post(new Runnable() 73 | { 74 | @Override 75 | public void run() 76 | { 77 | showView(mRetryView); 78 | } 79 | }); 80 | } 81 | 82 | } 83 | 84 | public void showContent() 85 | { 86 | if (isMainThread()) 87 | { 88 | showView(mContentView); 89 | } else 90 | { 91 | post(new Runnable() 92 | { 93 | @Override 94 | public void run() 95 | { 96 | showView(mContentView); 97 | } 98 | }); 99 | } 100 | } 101 | 102 | public void showEmpty() 103 | { 104 | if (isMainThread()) 105 | { 106 | showView(mEmptyView); 107 | } else 108 | { 109 | post(new Runnable() 110 | { 111 | @Override 112 | public void run() 113 | { 114 | showView(mEmptyView); 115 | } 116 | }); 117 | } 118 | } 119 | 120 | 121 | private void showView(View view) 122 | { 123 | if (view == null) return; 124 | 125 | if (view == mLoadingView) 126 | { 127 | mLoadingView.setVisibility(View.VISIBLE); 128 | if (mRetryView != null) 129 | mRetryView.setVisibility(View.GONE); 130 | if (mContentView != null) 131 | mContentView.setVisibility(View.GONE); 132 | if (mEmptyView != null) 133 | mEmptyView.setVisibility(View.GONE); 134 | } else if (view == mRetryView) 135 | { 136 | mRetryView.setVisibility(View.VISIBLE); 137 | if (mLoadingView != null) 138 | mLoadingView.setVisibility(View.GONE); 139 | if (mContentView != null) 140 | mContentView.setVisibility(View.GONE); 141 | if (mEmptyView != null) 142 | mEmptyView.setVisibility(View.GONE); 143 | } else if (view == mContentView) 144 | { 145 | mContentView.setVisibility(View.VISIBLE); 146 | if (mLoadingView != null) 147 | mLoadingView.setVisibility(View.GONE); 148 | if (mRetryView != null) 149 | mRetryView.setVisibility(View.GONE); 150 | if (mEmptyView != null) 151 | mEmptyView.setVisibility(View.GONE); 152 | } else if (view == mEmptyView) 153 | { 154 | mEmptyView.setVisibility(View.VISIBLE); 155 | if (mLoadingView != null) 156 | mLoadingView.setVisibility(View.GONE); 157 | if (mRetryView != null) 158 | mRetryView.setVisibility(View.GONE); 159 | if (mContentView != null) 160 | mContentView.setVisibility(View.GONE); 161 | } 162 | 163 | 164 | } 165 | 166 | public View setContentView(int layoutId) 167 | { 168 | return setContentView(mInflater.inflate(layoutId, this, false)); 169 | } 170 | 171 | public View setLoadingView(int layoutId) 172 | { 173 | return setLoadingView(mInflater.inflate(layoutId, this, false)); 174 | } 175 | 176 | public View setEmptyView(int layoutId) 177 | { 178 | return setEmptyView(mInflater.inflate(layoutId, this, false)); 179 | } 180 | 181 | public View setRetryView(int layoutId) 182 | { 183 | return setRetryView(mInflater.inflate(layoutId, this, false)); 184 | } 185 | public View setLoadingView(View view) 186 | { 187 | View loadingView = mLoadingView; 188 | if (loadingView != null) 189 | { 190 | Log.w(TAG, "you have already set a loading view and would be instead of this new one."); 191 | } 192 | removeView(loadingView); 193 | addView(view); 194 | mLoadingView = view; 195 | return mLoadingView; 196 | } 197 | 198 | public View setEmptyView(View view) 199 | { 200 | View emptyView = mEmptyView; 201 | if (emptyView != null) 202 | { 203 | Log.w(TAG, "you have already set a empty view and would be instead of this new one."); 204 | } 205 | removeView(emptyView); 206 | addView(view); 207 | mEmptyView = view; 208 | return mEmptyView; 209 | } 210 | 211 | public View setRetryView(View view) 212 | { 213 | View retryView = mRetryView; 214 | if (retryView != null) 215 | { 216 | Log.w(TAG, "you have already set a retry view and would be instead of this new one."); 217 | } 218 | removeView(retryView); 219 | addView(view); 220 | mRetryView = view; 221 | return mRetryView; 222 | 223 | } 224 | 225 | public View setContentView(View view) 226 | { 227 | View contentView = mContentView; 228 | if (contentView != null) 229 | { 230 | Log.w(TAG, "you have already set a retry view and would be instead of this new one."); 231 | } 232 | removeView(contentView); 233 | addView(view); 234 | mContentView = view; 235 | return mContentView; 236 | } 237 | 238 | public View getRetryView() 239 | { 240 | return mRetryView; 241 | } 242 | 243 | public View getLoadingView() 244 | { 245 | return mLoadingView; 246 | } 247 | 248 | public View getContentView() 249 | { 250 | return mContentView; 251 | } 252 | 253 | public View getEmptyView() 254 | { 255 | return mEmptyView; 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/LoadingAndRetryManager.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.support.v4.app.Fragment; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | /** 10 | * Created by zhy on 15/8/27. 11 | */ 12 | public class LoadingAndRetryManager 13 | { 14 | public static final int NO_LAYOUT_ID = 0; 15 | public static int BASE_LOADING_LAYOUT_ID = NO_LAYOUT_ID; 16 | public static int BASE_RETRY_LAYOUT_ID = NO_LAYOUT_ID; 17 | public static int BASE_EMPTY_LAYOUT_ID = NO_LAYOUT_ID; 18 | 19 | public LoadingAndRetryLayout mLoadingAndRetryLayout; 20 | 21 | 22 | public OnLoadingAndRetryListener DEFAULT_LISTENER = new OnLoadingAndRetryListener() 23 | { 24 | @Override 25 | public void setRetryEvent(View retryView) 26 | { 27 | 28 | } 29 | }; 30 | 31 | 32 | public LoadingAndRetryManager(Object activityOrFragmentOrView, OnLoadingAndRetryListener listener) 33 | { 34 | if (listener == null) listener = DEFAULT_LISTENER; 35 | 36 | ViewGroup contentParent = null; 37 | Context context; 38 | if (activityOrFragmentOrView instanceof Activity) 39 | { 40 | Activity activity = (Activity) activityOrFragmentOrView; 41 | context = activity; 42 | contentParent = (ViewGroup) activity.findViewById(android.R.id.content); 43 | } else if (activityOrFragmentOrView instanceof Fragment) 44 | { 45 | Fragment fragment = (Fragment) activityOrFragmentOrView; 46 | context = fragment.getActivity(); 47 | contentParent = (ViewGroup) (fragment.getView().getParent()); 48 | } else if (activityOrFragmentOrView instanceof View) 49 | { 50 | View view = (View) activityOrFragmentOrView; 51 | contentParent = (ViewGroup) (view.getParent()); 52 | context = view.getContext(); 53 | } else 54 | { 55 | throw new IllegalArgumentException("the argument's type must be Fragment or Activity: init(context)"); 56 | } 57 | int childCount = contentParent.getChildCount(); 58 | //get contentParent 59 | int index = 0; 60 | View oldContent; 61 | if (activityOrFragmentOrView instanceof View) 62 | { 63 | oldContent = (View) activityOrFragmentOrView; 64 | for (int i = 0; i < childCount; i++) 65 | { 66 | if (contentParent.getChildAt(i) == oldContent) 67 | { 68 | index = i; 69 | break; 70 | } 71 | } 72 | } else 73 | { 74 | oldContent = contentParent.getChildAt(0); 75 | } 76 | contentParent.removeView(oldContent); 77 | //setup content layout 78 | LoadingAndRetryLayout loadingAndRetryLayout = new LoadingAndRetryLayout(context); 79 | 80 | ViewGroup.LayoutParams lp = oldContent.getLayoutParams(); 81 | contentParent.addView(loadingAndRetryLayout, index, lp); 82 | loadingAndRetryLayout.setContentView(oldContent); 83 | // setup loading,retry,empty layout 84 | setupLoadingLayout(listener, loadingAndRetryLayout); 85 | setupRetryLayout(listener, loadingAndRetryLayout); 86 | setupEmptyLayout(listener, loadingAndRetryLayout); 87 | //callback 88 | listener.setRetryEvent(loadingAndRetryLayout.getRetryView()); 89 | listener.setLoadingEvent(loadingAndRetryLayout.getLoadingView()); 90 | listener.setEmptyEvent(loadingAndRetryLayout.getEmptyView()); 91 | mLoadingAndRetryLayout = loadingAndRetryLayout; 92 | } 93 | 94 | private void setupEmptyLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) 95 | { 96 | if (listener.isSetEmptyLayout()) 97 | { 98 | int layoutId = listener.generateEmptyLayoutId(); 99 | if (layoutId != NO_LAYOUT_ID) 100 | { 101 | loadingAndRetryLayout.setEmptyView(layoutId); 102 | } else 103 | { 104 | loadingAndRetryLayout.setEmptyView(listener.generateEmptyLayout()); 105 | } 106 | } else 107 | { 108 | if (BASE_EMPTY_LAYOUT_ID != NO_LAYOUT_ID) 109 | loadingAndRetryLayout.setEmptyView(BASE_EMPTY_LAYOUT_ID); 110 | } 111 | } 112 | 113 | private void setupLoadingLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) 114 | { 115 | if (listener.isSetLoadingLayout()) 116 | { 117 | int layoutId = listener.generateLoadingLayoutId(); 118 | if (layoutId != NO_LAYOUT_ID) 119 | { 120 | loadingAndRetryLayout.setLoadingView(layoutId); 121 | } else 122 | { 123 | loadingAndRetryLayout.setLoadingView(listener.generateLoadingLayout()); 124 | } 125 | } else 126 | { 127 | if (BASE_LOADING_LAYOUT_ID != NO_LAYOUT_ID) 128 | loadingAndRetryLayout.setLoadingView(BASE_LOADING_LAYOUT_ID); 129 | } 130 | } 131 | 132 | private void setupRetryLayout(OnLoadingAndRetryListener listener, LoadingAndRetryLayout loadingAndRetryLayout) 133 | { 134 | if (listener.isSetRetryLayout()) 135 | { 136 | int layoutId = listener.generateRetryLayoutId(); 137 | if (layoutId != NO_LAYOUT_ID) 138 | { 139 | loadingAndRetryLayout.setLoadingView(layoutId); 140 | } else 141 | { 142 | loadingAndRetryLayout.setLoadingView(listener.generateRetryLayout()); 143 | } 144 | } else 145 | { 146 | if (BASE_RETRY_LAYOUT_ID != NO_LAYOUT_ID) 147 | loadingAndRetryLayout.setRetryView(BASE_RETRY_LAYOUT_ID); 148 | } 149 | } 150 | 151 | public static LoadingAndRetryManager generate(Object activityOrFragment, OnLoadingAndRetryListener listener) 152 | { 153 | return new LoadingAndRetryManager(activityOrFragment, listener); 154 | } 155 | 156 | public void showLoading() 157 | { 158 | mLoadingAndRetryLayout.showLoading(); 159 | } 160 | 161 | public void showRetry() 162 | { 163 | mLoadingAndRetryLayout.showRetry(); 164 | } 165 | 166 | public void showContent() 167 | { 168 | mLoadingAndRetryLayout.showContent(); 169 | } 170 | 171 | public void showEmpty() 172 | { 173 | mLoadingAndRetryLayout.showEmpty(); 174 | } 175 | 176 | 177 | } 178 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/OnLoadingAndRetryListener.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry; 2 | 3 | import android.view.View; 4 | 5 | public abstract class OnLoadingAndRetryListener 6 | { 7 | public abstract void setRetryEvent(View retryView); 8 | 9 | public void setLoadingEvent(View loadingView) 10 | { 11 | } 12 | 13 | public void setEmptyEvent(View emptyView) 14 | { 15 | } 16 | 17 | public int generateLoadingLayoutId() 18 | { 19 | return LoadingAndRetryManager.NO_LAYOUT_ID; 20 | } 21 | 22 | public int generateRetryLayoutId() 23 | { 24 | return LoadingAndRetryManager.NO_LAYOUT_ID; 25 | } 26 | 27 | public int generateEmptyLayoutId() 28 | { 29 | return LoadingAndRetryManager.NO_LAYOUT_ID; 30 | } 31 | 32 | public View generateLoadingLayout() 33 | { 34 | return null; 35 | } 36 | 37 | public View generateRetryLayout() 38 | { 39 | return null; 40 | } 41 | 42 | public View generateEmptyLayout() 43 | { 44 | return null; 45 | } 46 | 47 | public boolean isSetLoadingLayout() 48 | { 49 | if (generateLoadingLayoutId() != LoadingAndRetryManager.NO_LAYOUT_ID || generateLoadingLayout() != null) 50 | return true; 51 | return false; 52 | } 53 | 54 | public boolean isSetRetryLayout() 55 | { 56 | if (generateRetryLayoutId() != LoadingAndRetryManager.NO_LAYOUT_ID || generateRetryLayout() != null) 57 | return true; 58 | return false; 59 | } 60 | 61 | public boolean isSetEmptyLayout() 62 | { 63 | if (generateEmptyLayoutId() != LoadingAndRetryManager.NO_LAYOUT_ID || generateEmptyLayout() != null) 64 | return true; 65 | return false; 66 | } 67 | 68 | 69 | } -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/test/AnyViewTestActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry.test; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | import android.widget.Toast; 8 | 9 | import com.zhy.base.loadandretry.LoadingAndRetryManager; 10 | import com.zhy.base.loadandretry.OnLoadingAndRetryListener; 11 | import com.zhy.base.loadandretry.R; 12 | 13 | public class AnyViewTestActivity extends AppCompatActivity 14 | { 15 | 16 | private TextView mTextView; 17 | 18 | LoadingAndRetryManager mLoadingAndRetryManager; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) 22 | { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_anyview_test); 25 | 26 | mTextView = (TextView) findViewById(R.id.id_textview); 27 | 28 | mLoadingAndRetryManager = LoadingAndRetryManager.generate(mTextView, new OnLoadingAndRetryListener() 29 | { 30 | @Override 31 | public void setRetryEvent(View retryView) 32 | { 33 | retryRefreashTextView(retryView); 34 | } 35 | }); 36 | 37 | refreashTextView(); 38 | 39 | 40 | } 41 | 42 | private void refreashTextView() 43 | { 44 | mLoadingAndRetryManager.showLoading(); 45 | 46 | new Thread() 47 | { 48 | @Override 49 | public void run() 50 | { 51 | try 52 | { 53 | Thread.sleep(2000); 54 | } catch (InterruptedException e) 55 | { 56 | e.printStackTrace(); 57 | } 58 | if (Math.random() > 0.6) 59 | { 60 | mLoadingAndRetryManager.showContent(); 61 | } else 62 | { 63 | mLoadingAndRetryManager.showRetry(); 64 | } 65 | } 66 | }.start(); 67 | 68 | 69 | } 70 | 71 | public void retryRefreashTextView(View retryView) 72 | { 73 | View view = retryView.findViewById(R.id.id_btn_retry); 74 | view.setOnClickListener(new View.OnClickListener() 75 | { 76 | @Override 77 | public void onClick(View v) 78 | { 79 | Toast.makeText(AnyViewTestActivity.this, "retry event invoked", Toast.LENGTH_SHORT).show(); 80 | AnyViewTestActivity.this.refreashTextView(); 81 | } 82 | }); 83 | } 84 | 85 | 86 | } 87 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/test/CategoryActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry.test; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.ListView; 10 | 11 | import com.zhy.base.loadandretry.R; 12 | 13 | import java.util.Arrays; 14 | import java.util.List; 15 | 16 | public class CategoryActivity extends AppCompatActivity 17 | { 18 | private ListView mListView; 19 | private List mDatas = Arrays.asList("LoadingAndRetry in Activity", "LoadingAndRetry in Fragment", "LoadingAndRetry in Any View"); 20 | 21 | private Class[] mClazz = new Class[]{MainActivity.class, FragmentTestActivity.class,AnyViewTestActivity.class}; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) 25 | { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_category); 28 | 29 | mListView = (ListView) findViewById(R.id.id_listview_category); 30 | 31 | mListView.setAdapter(new ArrayAdapter(this, R.layout.item_category, R.id.id_title, mDatas)); 32 | 33 | mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 34 | { 35 | @Override 36 | public void onItemClick(AdapterView parent, View view, int position, long id) 37 | { 38 | if (position + 1 > mClazz.length) return; 39 | Intent intent = new Intent(CategoryActivity.this, mClazz[position]); 40 | startActivity(intent); 41 | } 42 | }); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/test/FragmentTestActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry.test; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v7.app.AppCompatActivity; 6 | 7 | import com.zhy.base.loadandretry.R; 8 | 9 | public class FragmentTestActivity extends AppCompatActivity 10 | { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) 14 | { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_fragment_test); 17 | 18 | Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.id_rl_fragment_container); 19 | 20 | if (fragment == null) 21 | { 22 | getSupportFragmentManager().beginTransaction().add(R.id.id_rl_fragment_container, new NormalFragment()).commit(); 23 | } 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/test/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry.test; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.Toast; 7 | 8 | import com.zhy.base.loadandretry.LoadingAndRetryManager; 9 | import com.zhy.base.loadandretry.OnLoadingAndRetryListener; 10 | import com.zhy.base.loadandretry.R; 11 | 12 | public class MainActivity extends AppCompatActivity 13 | { 14 | LoadingAndRetryManager mLoadingAndRetryManager; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) 18 | { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | 22 | 23 | mLoadingAndRetryManager = LoadingAndRetryManager.generate(this, new OnLoadingAndRetryListener() 24 | { 25 | @Override 26 | public void setRetryEvent(View retryView) 27 | { 28 | MainActivity.this.setRetryEvent(retryView); 29 | } 30 | }); 31 | 32 | loadData(); 33 | 34 | } 35 | 36 | private void loadData() 37 | { 38 | mLoadingAndRetryManager.showLoading(); 39 | 40 | new Thread() 41 | { 42 | @Override 43 | public void run() 44 | { 45 | try 46 | { 47 | Thread.sleep(2000); 48 | } catch (InterruptedException e) 49 | { 50 | e.printStackTrace(); 51 | } 52 | double v = Math.random(); 53 | if (v > 0.8) 54 | { 55 | mLoadingAndRetryManager.showContent(); 56 | } else if (v > 0.4) 57 | { 58 | mLoadingAndRetryManager.showRetry(); 59 | } else 60 | { 61 | mLoadingAndRetryManager.showEmpty(); 62 | } 63 | } 64 | }.start(); 65 | 66 | 67 | } 68 | 69 | 70 | public void setRetryEvent(View retryView) 71 | { 72 | View view = retryView.findViewById(R.id.id_btn_retry); 73 | view.setOnClickListener(new View.OnClickListener() 74 | { 75 | @Override 76 | public void onClick(View v) 77 | { 78 | Toast.makeText(MainActivity.this, "retry event invoked", Toast.LENGTH_SHORT).show(); 79 | loadData(); 80 | } 81 | }); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/test/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry.test; 2 | 3 | import android.app.Application; 4 | 5 | import com.zhy.base.loadandretry.LoadingAndRetryManager; 6 | import com.zhy.base.loadandretry.R; 7 | 8 | /** 9 | * Created by zhy on 15/8/27. 10 | */ 11 | public class MyApplication extends Application 12 | { 13 | @Override 14 | public void onCreate() 15 | { 16 | super.onCreate(); 17 | LoadingAndRetryManager.BASE_RETRY_LAYOUT_ID = R.layout.base_retry; 18 | LoadingAndRetryManager.BASE_LOADING_LAYOUT_ID = R.layout.base_loading; 19 | LoadingAndRetryManager.BASE_EMPTY_LAYOUT_ID = R.layout.base_empty; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/java/com/zhy/base/loadandretry/test/NormalFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhy.base.loadandretry.test; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.Toast; 9 | 10 | import com.zhy.base.loadandretry.LoadingAndRetryManager; 11 | import com.zhy.base.loadandretry.OnLoadingAndRetryListener; 12 | import com.zhy.base.loadandretry.R; 13 | 14 | /** 15 | * Created by zhy on 15/8/27. 16 | */ 17 | public class NormalFragment extends android.support.v4.app.Fragment 18 | { 19 | LoadingAndRetryManager mLoadingAndRetryManager; 20 | 21 | @Override 22 | public void onCreate(@Nullable Bundle savedInstanceState) 23 | { 24 | super.onCreate(savedInstanceState); 25 | loadData(); 26 | } 27 | 28 | @Nullable 29 | @Override 30 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 31 | { 32 | return inflater.inflate(R.layout.fragment_layout, container, false); 33 | } 34 | 35 | @Override 36 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) 37 | { 38 | super.onViewCreated(view, savedInstanceState); 39 | 40 | 41 | mLoadingAndRetryManager = LoadingAndRetryManager.generate(this, new OnLoadingAndRetryListener() 42 | { 43 | @Override 44 | public void setRetryEvent(View retryView) 45 | { 46 | NormalFragment.this.setRetryEvent(retryView); 47 | } 48 | }); 49 | 50 | mLoadingAndRetryManager.showLoading(); 51 | } 52 | 53 | 54 | public void setRetryEvent(View retryView) 55 | { 56 | View view = retryView.findViewById(R.id.id_btn_retry); 57 | view.setOnClickListener(new View.OnClickListener() 58 | { 59 | @Override 60 | public void onClick(View v) 61 | { 62 | Toast.makeText(getActivity(), "retry event invoked", Toast.LENGTH_SHORT).show(); 63 | mLoadingAndRetryManager.showLoading(); 64 | loadData(); 65 | } 66 | }); 67 | } 68 | 69 | private void loadData() 70 | { 71 | 72 | 73 | new Thread() 74 | { 75 | @Override 76 | public void run() 77 | { 78 | try 79 | { 80 | Thread.sleep(2000); 81 | } catch (InterruptedException e) 82 | { 83 | e.printStackTrace(); 84 | } 85 | if (Math.random() > 0.6) 86 | { 87 | mLoadingAndRetryManager.showContent(); 88 | } else 89 | { 90 | mLoadingAndRetryManager.showRetry(); 91 | } 92 | } 93 | }.start(); 94 | } 95 | } 96 | 97 | 98 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/activity_anyview_test.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 16 | 17 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/activity_category.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/activity_fragment_test.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/base_empty.xml: -------------------------------------------------------------------------------- 1 | 7 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/base_loading.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /loadingandretrymanager/src/main/res/layout/base_retry.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 16 | 28 | 29 | 41 | 42 |