├── .gitignore ├── LICENSE.txt ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── resource │ ├── example.apk │ ├── loading.gif │ ├── refresh.gif │ ├── srecyclerview.gif │ ├── srv_key_123456 │ └── update_history.md └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── hzw │ │ └── srecyclerviewproject │ │ ├── ClockView.java │ │ ├── MainActivity.java │ │ ├── SRVApplication.java │ │ ├── SRVTestConfig.java │ │ ├── TestConfigDialog.java │ │ ├── TestEmptyView.java │ │ ├── TestErrorView.java │ │ ├── TestLoadFooter.java │ │ ├── TestLoadingView.java │ │ ├── TestRefreshHeader.java │ │ └── TestSRVModule.java │ └── res │ ├── drawable │ └── srecyclerview.gif │ ├── layout │ ├── activity_main.xml │ ├── dialog_config.xml │ ├── empty_test.xml │ ├── error_loading.xml │ ├── error_test.xml │ ├── footer_test.xml │ ├── header_test.xml │ ├── item_test.xml │ └── refresh_view.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 │ ├── attr.xml │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── srecyclerview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── com │ └── hzw │ └── srecyclerview │ ├── AbsLoadFooter.java │ ├── AbsRefreshHeader.java │ ├── AbsStateView.java │ ├── BaseSRVAdapter.java │ ├── SRVConfig.java │ ├── SRVHolder.java │ ├── SRVLoadFooter.java │ ├── SRVRefreshHeader.java │ ├── SRecyclerView.java │ └── SRecyclerViewModule.java └── res ├── drawable └── srv_arrow.png ├── layout ├── srv_load_footer.xml └── srv_refresh_header.xml └── values ├── attr.xml ├── ids.xml └── strings.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .idea 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | /release 11 | 12 | #built application files 13 | *.ap_ 14 | 15 | 16 | # files for the dex VM 17 | *.dex 18 | 19 | 20 | # Java class files 21 | *.class 22 | 23 | 24 | # generated files 25 | bin/ 26 | gen/ 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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 [2017] [hzwsunshine] 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SRecyclerView 2 | 有刷新和加载功能的RecyclerView
3 | 博客地址:http://blog.csdn.net/hzwailll/article/details/75285924 4 | 5 | [ ![Download](https://api.bintray.com/packages/hzwsunshine/maven/srecyclerview/images/download.svg) ](https://bintray.com/hzwsunshine/maven/srecyclerview/_latestVersion) 6 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 7 | 8 | 9 | 示例apk: [点击下载](https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/master/app/resource/example.apk) 10 | 11 | 12 | #### 主要功能有: 13 | 1. 下拉刷新,滑动到底部加载 14 | 2. 支持添加或删除多个头部和尾部 15 | 3. 支持自定义刷新头部和加载尾部 16 | 4. 支持全局配置刷新头部,加载尾部 17 | 5. 支持代码设置刷新头部,加载尾部(满足某个列表的特殊要求) 18 | 6. 支持全局及代码设置加载中布局,空布局和错误布局 19 | 6. 支持加载中布局,空布局和错误布局显示时的下拉刷新,及点击刷新重试 20 | 7. 支持加载尾部的无数据和加载错误的显示及加载错误时的点击加载重试 21 | 8. 支持设置LinearLayoutManager的分割线,以及纵向时分割线的左右距离 22 | 9. 支持数据不满一屏时的上滑加载 23 | 10. 支持列表Item的点击事件 24 | 11. 附带一个简易的适配器,大大减少适配器的代码 25 | 12. 附带的简易适配器,可用于SRecyclerView,也可用于普通RecyclerView 26 | 13. 默认已设置为纵向(VERTICAL)的LinearLayoutManager,请勿重复设置 27 | 28 | #### 不支持: 29 | 1. 不支持StaggeredGridLayoutManager流式布局的刷新和加载 30 | 2. 不支持横向布局的刷新和加载 31 | 3. 不支持滑动到底部时,上拉一段距离才触发加载功能的方式 32 | 4. 不支持类似于ListView式的空布局设置方式,空布局需要为一个单独布局 33 | 34 | 35 | 36 | 37 | ## Gradle 38 | **Use Gradle**:     39 | 40 | compile 'com.github.hzw:srecyclerview:1.2.9' 41 | 42 | 所需依赖:com.android.support:design:xxx 43 | 44 | 45 | ## 使用 46 | **xml** 47 | 48 | ```xml 49 | 56 | 57 | ``` 58 | 59 | **code** 60 | 61 | ```java 62 | //如果设置了加载监听,就是需要刷新加载功能,如果没有设置加载监听,那么就没有下拉与底部加载 63 | recyclerView.setLoadListener(new SRecyclerView.LoadListener() { 64 | @Override 65 | public void refresh() { 66 | } 67 | 68 | @Override 69 | public void loading() { 70 | } 71 | }); 72 | 73 | //item的点击事件 74 | recyclerView.setItemClickListener(new SRecyclerView.ItemClickListener() { 75 | @Override 76 | public void click(View v, int position) { 77 | } 78 | }); 79 | 80 | //可以在xml中配置分割线,也可以在代码中设置分割线 81 | recyclerView.setDivider(Color.LTGRAY, 3, 30, 0); 82 | 83 | //可以添加一个或多个尾部和头部 84 | recyclerView.addHeader(header); 85 | recyclerView.removeHeader(header); 86 | recyclerView.addFooter(footer); 87 | recyclerView.removeFooter(footer); 88 | 89 | //刷新完成 90 | recyclerView.refreshComplete(); 91 | //刷新错误,空数据时会显示错误布局 92 | recyclerView.refreshError(); 93 | 94 | //加载更多完成 95 | recyclerView.loadingComplete(); 96 | //加载更多没有数据 97 | recyclerView.loadNoMoreData(); 98 | //加载更多出现错误 99 | recyclerView.loadingError(); 100 | 101 | //设置刷新和加载更多是否可用 102 | recyclerView.setRefreshEnable(enable); 103 | recyclerView.setLoadingEnable(enable); 104 | 105 | //代码的刷新,应该在setAdapter方法之后调用,true表示有刷新动画,false无动画 106 | recyclerView.startRefresh(true); 107 | 108 | //更多使用方法请参考demo... 109 | ``` 110 | 111 | **全局配置** 112 | 113 | 如果需要自定义刷新头部或加载尾部的样式,以自定义刷新头部为例,有两种方式: 114 | 1. 新建类并实现AbsRefreshHeader,并在代码中调用**SrecyclerView.setRefreshHeader(new YourRefreshHeader(context))**,为当前的刷新列表设置你自定义的刷新头部 115 | 2. 使用全局配置方式,如下所示: 116 | 117 | ```java 118 | // 1. SRecyclerView的刷新头部和加载尾部的全局配置需要新建一个类,并实现SRecyclerViewModule接口 119 | public class TestSRVModule implements SRecyclerViewModule { 120 | @Override 121 | public AbsRefreshHeader getRefreshHeader(Context context) { 122 | return new TestRefreshHeader(context); 123 | } 124 | 125 | //对应的还可以配置全局的加载尾部,空布局和错误布局... 126 | } 127 | 128 | // 2. 并在AndroidManifest.xml中添加meta-data,name为实现类的路径,value必须为:"SRecyclerViewModule" 129 | 132 | ``` 133 | 以上是对全局配置的示例,当然你也可以不用做任何配置,如果默认的样式能满足你的要求的话!
134 | 为了满足某个列表有特殊的刷新头部或加载尾部或空布局或错误布局的情况,可以在代码中为这个列表单独设置,即方式1,方式1的优先级大于方式2 135 | ```java 136 | //一下两个方法需要在setAdapter方法之前设置才有效 137 | recyclerView.setRefreshHeader(new TestRefreshHeader(context)); 138 | recyclerView.setLoadingFooter(new TestLoadFooter(context)); 139 | 140 | //代码中设置一个空布局和错误布局 141 | recyclerView.setEmptyView(new TestEmptyView(context)); 142 | recyclerView.setErrorView(new TestErrorView(context)); 143 | ``` 144 | 145 |

146 | 147 | 148 | ## Update History 149 | 150 | [更新历史](https://github.com/HzwSunshine/SRecyclerView/blob/master/app/resource/update_history.md) 151 | 152 | 153 | License 154 | ------- 155 | 156 | Copyright 2017 hzwsunshine 157 | 158 | Licensed under the Apache License, Version 2.0 (the "License"); 159 | you may not use this file except in compliance with the License. 160 | You may obtain a copy of the License at 161 | 162 | http://www.apache.org/licenses/LICENSE-2.0 163 | 164 | Unless required by applicable law or agreed to in writing, software 165 | distributed under the License is distributed on an "AS IS" BASIS, 166 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 167 | See the License for the specific language governing permissions and 168 | limitations under the License. 169 | 170 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | buildToolsVersion '26.0.2' 6 | defaultConfig { 7 | applicationId "com.hzw.srecyclerviewproject" 8 | minSdkVersion 14 9 | targetSdkVersion 26 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(include: ['*.jar'], dir: 'libs') 23 | compile project(':srecyclerview') 24 | compile 'com.android.support:appcompat-v7:26.1.0' 25 | compile 'com.android.support:support-v4:26.1.0' 26 | compile 'com.android.support:design:26.1.0' 27 | debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' 28 | releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' 29 | compile 'com.android.support.constraint:constraint-layout:1.1.2' 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 C:\Users\DELL\AppData\Local\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 | 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/resource/example.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/5729c610807438ec1fa7d8f293a3c859289b7f08/app/resource/example.apk -------------------------------------------------------------------------------- /app/resource/loading.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/5729c610807438ec1fa7d8f293a3c859289b7f08/app/resource/loading.gif -------------------------------------------------------------------------------- /app/resource/refresh.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/5729c610807438ec1fa7d8f293a3c859289b7f08/app/resource/refresh.gif -------------------------------------------------------------------------------- /app/resource/srecyclerview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/5729c610807438ec1fa7d8f293a3c859289b7f08/app/resource/srecyclerview.gif -------------------------------------------------------------------------------- /app/resource/srv_key_123456: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/5729c610807438ec1fa7d8f293a3c859289b7f08/app/resource/srv_key_123456 -------------------------------------------------------------------------------- /app/resource/update_history.md: -------------------------------------------------------------------------------- 1 | # SRecyclerView Update History 2 | 3 | > * 2019.4.12     版本:1.2.9
4 | 优化引用方式;解决bug 5 | 6 | > * 2019.3.7     版本:1.2.8
7 | 支持刷新下滑灵敏度的设置 8 | 9 | > * 2019.1.29     版本:1.2.7
10 | 解决了一些小问题,精简代码 11 | 12 | > * 2018.9.4     版本:1.2.4
13 | 添加对加载中布局,错误布局的支持 14 | 15 | > * 2018.7.11     版本:1.2.2
16 | 完善代码 17 | 18 | > * 2018.7.9     版本:1.2.1
19 | 完善代码,修复bug 20 | 21 | > * 2018.7.3     版本:1.2.0
22 | 完善代码,修改了上滑加载数据的策略,体验更好 23 | 24 | > * 2018.6.28     版本:1.1.8 beta3
25 | 修改空布局的添加方式,完善相关代码 26 | 27 | > * 2018.2.8     版本:1.1.8 beta1
28 | 优化加载更多的逻辑;移除优化AbsLoadFooter的部分方法;增加loadingError()方法; 29 | 30 | > * 2017.10.10     版本:1.1.7
31 | 修复多手势下拉时,刷新头部偏移的bug 32 | 33 | > * 2017.9.25     版本:1.1.6
34 | 完善加载尾部的加载逻辑,完善一些小细节 35 | 36 | > * 2017.9.18     版本:1.1.5
37 | 修改获取全局配置的逻辑,解决头部的手势偏移问题,完善尾部加载逻辑,添加分组测试类 38 | 39 | > * 2017.9.6     版本:1.1.4
40 | 修改添加头部和尾部的逻辑,可以在setAdapter之前或之后添加了 41 | 42 | > * 2017.8.30     版本:1.1.3
43 | 完善自动刷新时,手势操作可能引起的刷新异常 44 | 45 | > * 2017.8.24     版本:1.1.2
46 | 解决多手势下拉时的刷新问题,完善刷新加载逻辑,完善代码 47 | 48 | > * 2017.7.20     版本:1.1.1
49 | 完善下拉手势和加载更多的逻辑,完善测试类及注释 50 | 51 | > * 2017.7.19     版本:1.1.0
52 | 完善刷新逻辑,版本更新为1.1.0 53 | 54 | > * 2017.7.18     版本:1.0.0
55 | 完成SRecyclerView,测试并使用了一段时间后提交到GitHub,同时提交到JCenter 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/ClockView.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.animation.ValueAnimator; 4 | import android.content.Context; 5 | import android.content.res.TypedArray; 6 | import android.graphics.Canvas; 7 | import android.graphics.Color; 8 | import android.graphics.Paint; 9 | import android.util.AttributeSet; 10 | import android.view.View; 11 | import android.view.animation.LinearInterpolator; 12 | 13 | /** 14 | * 功能:时钟View 15 | * Created by hzw on 2016/12/15. 16 | */ 17 | public class ClockView extends View { 18 | 19 | private static final int GRAY_COLOR = Color.parseColor("#434659"); 20 | private Paint paint = new Paint(); 21 | private float sharpRadius;//整点时间的半径 22 | private float otherRadius;//非整点的半径 23 | private float outsideCircleWidth;//外圆宽度 24 | private float outsideDistance;//整点圆心到外圆的距离 25 | private float needleWidth;//指针的宽度 26 | private float hourLength;//时钟的长度 27 | private float minuteLength;//分针的长度 28 | private float centerCircleRadius;//中心圆的半径 29 | private int angle;//时针的角度 30 | private int size;//表盘的宽高 31 | private ValueAnimator animator; 32 | 33 | 34 | public ClockView(Context context) { 35 | super(context); 36 | init(null, 0); 37 | } 38 | 39 | public ClockView(Context context, AttributeSet attrs) { 40 | super(context, attrs); 41 | init(attrs, 0); 42 | } 43 | 44 | public ClockView(Context context, AttributeSet attrs, int defStyleAttr) { 45 | super(context, attrs, defStyleAttr); 46 | init(attrs, defStyleAttr); 47 | } 48 | 49 | private void init(AttributeSet attrs, int defStyleAttr) { 50 | TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ClockView, defStyleAttr, 0); 51 | int color = array.getColor(R.styleable.ClockView_colck_color, GRAY_COLOR);//时钟颜色 52 | array.recycle(); 53 | paint.setAntiAlias(true); 54 | paint.setColor(color); 55 | paint.setStrokeCap(Paint.Cap.ROUND); 56 | } 57 | 58 | @Override 59 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 60 | super.onSizeChanged(w, h, oldw, oldh); 61 | size = Math.min(getWidth(), getHeight()); 62 | int factor = 524;//默认624 63 | sharpRadius = size * 30.0f / (factor * 2.0f); 64 | otherRadius = size * 18.0f / (factor * 2.0f); 65 | outsideCircleWidth = size * 25.0f / factor; 66 | outsideDistance = size * 95.0f / factor; 67 | hourLength = size * 180.0f / factor; 68 | minuteLength = size * 110.0f / factor; 69 | needleWidth = size * 35.0f / (factor * 2.0f); 70 | centerCircleRadius = needleWidth; 71 | } 72 | 73 | @Override 74 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 75 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 76 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); 77 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); 78 | int size = Math.min(widthSize, heightSize); 79 | setMeasuredDimension(size, size); 80 | } 81 | 82 | @Override 83 | protected void onDraw(Canvas canvas) { 84 | super.onDraw(canvas); 85 | 86 | //画背景和外圆 87 | canvas.drawColor(Color.TRANSPARENT); 88 | paint.setStyle(Paint.Style.STROKE); 89 | paint.setStrokeWidth(outsideCircleWidth); 90 | canvas.drawCircle(size / 2, size / 2, (size - outsideCircleWidth) / 2, paint); 91 | //画时间圆点 92 | paint.setStyle(Paint.Style.FILL); 93 | for (int i = 0; i < 12; i++) { 94 | if (i % 3 == 0) { 95 | canvas.drawCircle(size / 2, outsideDistance, sharpRadius, paint); 96 | } else { 97 | canvas.drawCircle(size / 2, outsideDistance, otherRadius, paint); 98 | } 99 | canvas.rotate(30, size / 2, size / 2); 100 | } 101 | 102 | paint.setStrokeWidth(needleWidth); 103 | //画时针 104 | canvas.drawLine(size / 2, size / 2, getX(minuteLength, angle), getY(minuteLength, angle), paint); 105 | //画分针 106 | int hourAngle = (angle % 30) * 12 - 90; 107 | canvas.drawLine(size / 2, size / 2, getX(hourLength, hourAngle), getY(hourLength, hourAngle), paint); 108 | 109 | //画中心圆点 110 | canvas.drawCircle(size / 2, size / 2, centerCircleRadius, paint); 111 | } 112 | 113 | private float getX(float radius, int angle) { 114 | return (float) (size / 2 + radius * Math.cos(angle * 3.14 / 180)); 115 | } 116 | 117 | private float getY(float radius, int angle) { 118 | return (float) (size / 2 + radius * Math.sin(angle * 3.14 / 180)); 119 | } 120 | 121 | public void setClockAngle(int angle) { 122 | angle /= 4; 123 | this.angle = angle - 90; 124 | invalidate(); 125 | } 126 | 127 | public void resetClock() { 128 | stopClockAnim(); 129 | angle = -90; 130 | } 131 | 132 | public void startClockAnim() { 133 | if (animator == null) { 134 | animator = ValueAnimator.ofInt(angle, angle + 360); 135 | animator.setRepeatMode(ValueAnimator.RESTART); 136 | animator.setRepeatCount(ValueAnimator.INFINITE); 137 | animator.setDuration(10000).setInterpolator(new LinearInterpolator()); 138 | animator.start(); 139 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 140 | @Override 141 | public void onAnimationUpdate(ValueAnimator animation) { 142 | angle = (int) animation.getAnimatedValue(); 143 | invalidate(); 144 | } 145 | }); 146 | } else { 147 | animator.setIntValues(angle, angle + 360); 148 | animator.start(); 149 | } 150 | } 151 | 152 | public void stopClockAnim() { 153 | if (animator != null) { 154 | animator.cancel(); 155 | } 156 | } 157 | 158 | 159 | } 160 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.TextView; 13 | import android.widget.Toast; 14 | 15 | import com.hzw.srecyclerview.BaseSRVAdapter; 16 | import com.hzw.srecyclerview.SRVHolder; 17 | import com.hzw.srecyclerview.SRecyclerView; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | 23 | public class MainActivity extends AppCompatActivity { 24 | 25 | private List list = new ArrayList<>(); 26 | private SRecyclerView recyclerView; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_main); 32 | recyclerView = findViewById(R.id.srv_test); 33 | findViewById(R.id.test).setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | showConfigDialog(); 37 | } 38 | }); 39 | 40 | //如果设置了加载监听,就是需要刷新加载功能,如果没有设置加载监听,那么就没有下拉与底部加载 41 | recyclerView.setLoadListener(new SRecyclerView.LoadListener() { 42 | @Override 43 | public void refresh() { 44 | new Handler().postDelayed(new Runnable() { 45 | @Override 46 | public void run() { 47 | if (SRVTestConfig.getInstance() 48 | .isRefreshEmpty()) { 49 | recyclerView.getAdapter() 50 | .notifyDataSetChanged(); 51 | recyclerView.refreshComplete(); 52 | } else if (SRVTestConfig.getInstance() 53 | .isRefreshError()) { 54 | recyclerView.refreshError(); 55 | } else if (SRVTestConfig.getInstance() 56 | .isRefresh()) { 57 | refreshData(); 58 | recyclerView.refreshComplete(); 59 | } 60 | } 61 | }, 2000); 62 | } 63 | 64 | @Override 65 | public void loading() { 66 | new Handler().postDelayed(new Runnable() { 67 | @Override 68 | public void run() { 69 | if (SRVTestConfig.getInstance() 70 | .isLoadingError()) { 71 | recyclerView.loadingError(); 72 | return; 73 | } 74 | if (SRVTestConfig.getInstance() 75 | .isLoadingNoData()) { 76 | recyclerView.loadNoMoreData(); 77 | return; 78 | } 79 | loadData(); 80 | recyclerView.loadingComplete(); 81 | } 82 | }, 2000); 83 | } 84 | }); 85 | 86 | //item的点击事件 87 | recyclerView.setItemClickListener(new SRecyclerView.ItemClickListener() { 88 | @Override 89 | public void click(View v, int position) { 90 | Toast.makeText(getApplication(), "位置: " + position, Toast.LENGTH_SHORT) 91 | .show(); 92 | 93 | list.set(list.size() - 1, "这是通知改变的"); 94 | recyclerView.getAdapter().notifyItemChanged(list.size() - 1); 95 | } 96 | }); 97 | 98 | //可以设置一个EmptyView 99 | //recyclerView.setEmptyView(new View(this)); 100 | 101 | //可以在xml中配置分割线,也可以在代码中设置分割线 102 | recyclerView.setDivider(Color.LTGRAY, 3, 30, 0); 103 | 104 | //可以手动设置一个刷新头部,应该在setAdapter方法之前调用,适用于某个列表需要特殊刷新头的场景 105 | //SRecyclerView的刷新头部和加载尾部的设置有两种种方法: 106 | // 代码设置,全局配置。如果两种方法都没有设置,则使用自带的默认刷新头和加载尾 107 | //recyclerView.setRefreshHeader(new TestRefreshHeader(this)); 108 | //recyclerView.setLoadingFooter(new TestLoadFooter(this)); 109 | //recyclerView.setErrorView(new TestErrorView(this)); 110 | 111 | 112 | //SRecyclerView的刷新头部和加载尾部的全局配置需要新建一个类, 113 | // 并实现SRecyclerViewModule接口,并在AndroidManifest.xml中添加meta-data, 114 | // 对SRV进行全局配置,name为实现类的路径,value必须为接口名称: "SRecyclerViewModule" 115 | //示例如下 116 | // 119 | 120 | 121 | //可以添加一个或多个尾部 122 | View footer = LayoutInflater.from(this) 123 | .inflate(R.layout.footer_test, recyclerView, false); 124 | //recyclerView.addFooter(footer); 125 | 126 | //这里的适配器使用的一个简易的SRV适配器:BaseSRVAdapter,可以很大程度上减少适配器的代码量, 127 | // 这个适配器同样也可以用于普通的RecyclerView,当然这里也可以用原生的适配器 128 | recyclerView.setAdapter(new SRVAdapter(list)); 129 | //recyclerView.setAdapter(new InitAdapter(list, this)); 130 | 131 | //可以添加一个或多个头部 132 | View header = LayoutInflater.from(this) 133 | .inflate(R.layout.header_test, recyclerView, false); 134 | //recyclerView.addHeader(header); 135 | 136 | //SRV的代码刷新,应该在setAdapter方法之后调用,true表示有刷新动画,false无动画 137 | //recyclerView.startRefresh(true); 138 | recyclerView.startRefresh(false); 139 | 140 | 141 | //测试数据 142 | //refreshData(); 143 | 144 | //混淆 145 | //-keep public class * implements com.hzw.srecyclerview.SRecyclerViewModule 146 | } 147 | 148 | private int index; 149 | 150 | private void refreshData() { 151 | list.clear(); 152 | index = 0; 153 | for (int i = 0; i < 15; i++) { 154 | list.add("数据 " + index++); 155 | } 156 | recyclerView.getAdapter() 157 | .notifyDataSetChanged(); 158 | } 159 | 160 | private void loadData() { 161 | int last = list.size(); 162 | for (int i = 0; i < 15; i++) { 163 | list.add("数据 " + index++); 164 | } 165 | recyclerView.getAdapter() 166 | .notifyItemInserted(last); 167 | } 168 | 169 | 170 | /** 171 | * 基于简易适配器的写法 172 | */ 173 | private static class SRVAdapter extends BaseSRVAdapter { 174 | SRVAdapter(List list) { 175 | super(list, R.layout.item_test); 176 | } 177 | 178 | @Override 179 | public void onBindView(SRVHolder holder, String data, int i) { 180 | holder.setTextView(R.id.tv_item_test, data); 181 | // holder.setTextView(0, "123") 182 | // .setTextView(1, "234"); 183 | } 184 | } 185 | 186 | 187 | /** 188 | * 原生的适配器 189 | */ 190 | private static class InitAdapter extends RecyclerView.Adapter { 191 | private LayoutInflater inflater; 192 | private List list; 193 | 194 | InitAdapter(List list, Context context) { 195 | this.list = list; 196 | inflater = LayoutInflater.from(context); 197 | } 198 | 199 | @Override 200 | public Holder onCreateViewHolder(ViewGroup parent, int viewType) { 201 | View v = inflater.inflate(R.layout.item_test, parent, false); 202 | return new Holder(v); 203 | } 204 | 205 | @Override 206 | public void onBindViewHolder(Holder holder, int position) { 207 | holder.textView.setText(list.get(position)); 208 | } 209 | 210 | @Override 211 | public int getItemCount() { 212 | return list.size(); 213 | } 214 | 215 | static class Holder extends RecyclerView.ViewHolder { 216 | private TextView textView; 217 | 218 | Holder(View itemView) { 219 | super(itemView); 220 | textView = itemView.findViewById(R.id.tv_item_test); 221 | } 222 | } 223 | } 224 | 225 | private void reStart() { 226 | recreate(); 227 | } 228 | 229 | 230 | private TestConfigDialog dialog; 231 | private List headers = new ArrayList<>(); 232 | private List footers = new ArrayList<>(); 233 | 234 | private void showConfigDialog() { 235 | if (dialog == null) { 236 | dialog = new TestConfigDialog(this); 237 | dialog.setTestListener(new TestConfigDialog.TestListener() { 238 | @Override 239 | public void addHeader() { 240 | View header = LayoutInflater.from(MainActivity.this) 241 | .inflate(R.layout.header_test, recyclerView, false); 242 | recyclerView.addHeader(header); 243 | headers.add(header); 244 | } 245 | 246 | @Override 247 | public void removeHeader() { 248 | if (headers.size() == 0) return; 249 | View header = headers.get(headers.size() - 1); 250 | recyclerView.removeHeader(header); 251 | headers.remove(header); 252 | } 253 | 254 | @Override 255 | public void addFooter() { 256 | View footer = LayoutInflater.from(MainActivity.this) 257 | .inflate(R.layout.footer_test, recyclerView, false); 258 | recyclerView.addFooter(footer); 259 | footers.add(footer); 260 | } 261 | 262 | @Override 263 | public void removeFooter() { 264 | if (footers.size() == 0) return; 265 | View footer = footers.get(footers.size() - 1); 266 | recyclerView.removeFooter(footer); 267 | footers.remove(footer); 268 | } 269 | 270 | @Override 271 | public void startRefresh() { 272 | recyclerView.startRefresh(true); 273 | } 274 | 275 | @Override 276 | public void resetAdapter() { 277 | list.clear(); 278 | index = 0; 279 | for (int i = 0; i < 15; i++) { 280 | list.add("数据 " + index++); 281 | } 282 | recyclerView.setAdapter(new SRVAdapter(list)); 283 | } 284 | 285 | @Override 286 | public void showEmpty() { 287 | reStart(); 288 | } 289 | 290 | @Override 291 | public void showError() { 292 | reStart(); 293 | } 294 | }); 295 | } 296 | dialog.show(); 297 | } 298 | 299 | } 300 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/SRVApplication.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.app.Application; 4 | import com.squareup.leakcanary.LeakCanary; 5 | 6 | /** 7 | * author: hzw 8 | * time: 2018/7/11 上午11:08 9 | * description: 10 | */ 11 | public class SRVApplication extends Application { 12 | 13 | @Override public void onCreate() { 14 | super.onCreate(); 15 | if (LeakCanary.isInAnalyzerProcess(this)) { 16 | // This process is dedicated to LeakCanary for heap analysis. 17 | // You should not init your app in this process. 18 | return; 19 | } 20 | LeakCanary.install(this); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/SRVTestConfig.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | /** 4 | * author: hzw 5 | * time: 2018/8/12 下午2:58 6 | * description: 7 | */ 8 | public class SRVTestConfig { 9 | 10 | private volatile static SRVTestConfig instance; 11 | 12 | private SRVTestConfig() { 13 | } 14 | 15 | public static SRVTestConfig getInstance() { 16 | if (instance == null) { 17 | synchronized (SRVTestConfig.class) { 18 | if (instance == null) { 19 | instance = new SRVTestConfig(); 20 | } 21 | } 22 | } 23 | return instance; 24 | } 25 | 26 | private boolean refreshError; 27 | private boolean refreshEmpty; 28 | private boolean refresh = true; 29 | 30 | public boolean isRefreshError() { 31 | return refreshError; 32 | } 33 | 34 | public void setRefreshError(boolean refreshError) { 35 | this.refreshError = refreshError; 36 | if (refreshError) { 37 | refreshEmpty = false; 38 | refresh = false; 39 | } 40 | } 41 | 42 | public boolean isRefreshEmpty() { 43 | return refreshEmpty; 44 | } 45 | 46 | public void setRefreshEmpty(boolean refreshEmpty) { 47 | this.refreshEmpty = refreshEmpty; 48 | if (refreshEmpty) { 49 | refreshError = false; 50 | refresh = false; 51 | } 52 | } 53 | 54 | public boolean isRefresh() { 55 | return refresh; 56 | } 57 | 58 | public void setRefresh(boolean refresh) { 59 | this.refresh = refresh; 60 | if (refresh) { 61 | refreshError = false; 62 | refreshEmpty = false; 63 | } 64 | } 65 | 66 | 67 | private boolean loadingNoData; 68 | private boolean loadingError; 69 | 70 | public boolean isLoadingNoData() { 71 | return loadingNoData; 72 | } 73 | 74 | public void setLoadingNoData(boolean loadingNoData) { 75 | this.loadingNoData = loadingNoData; 76 | if (loadingNoData) { 77 | loadingError = false; 78 | } 79 | } 80 | 81 | public boolean isLoadingError() { 82 | return loadingError; 83 | } 84 | 85 | public void setLoadingError(boolean loadingError) { 86 | this.loadingError = loadingError; 87 | if (loadingError) { 88 | loadingNoData = false; 89 | } 90 | } 91 | 92 | public void setLoadingNormal() { 93 | loadingNoData = false; 94 | loadingError = false; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestConfigDialog.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.app.Dialog; 4 | import android.content.Context; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | import android.util.Log; 8 | import android.view.Gravity; 9 | import android.view.View; 10 | import android.view.Window; 11 | import android.view.WindowManager; 12 | import android.widget.Button; 13 | import android.widget.Toast; 14 | 15 | /** 16 | * author: hzw 17 | * time: 2018/8/12 下午4:51 18 | * description: 19 | */ 20 | public class TestConfigDialog extends Dialog implements View.OnClickListener { 21 | 22 | public TestConfigDialog(@NonNull Context context) { 23 | super(context, R.style.dialogStyle); 24 | } 25 | 26 | @Override protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | Window window = getWindow(); 29 | if (window == null) return; 30 | window.setContentView(R.layout.dialog_config); 31 | window.setGravity(Gravity.BOTTOM); 32 | window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); 33 | initView(); 34 | } 35 | 36 | private void initView() { 37 | findViewById(R.id.add_header).setOnClickListener(this); 38 | findViewById(R.id.remove_header).setOnClickListener(this); 39 | findViewById(R.id.refresh).setOnClickListener(this); 40 | findViewById(R.id.add_footer).setOnClickListener(this); 41 | findViewById(R.id.remove_footer).setOnClickListener(this); 42 | findViewById(R.id.reset_adapter).setOnClickListener(this); 43 | findViewById(R.id.refresh_loading).setOnClickListener(this); 44 | findViewById(R.id.footer_loading).setOnClickListener(this); 45 | findViewById(R.id.loading_error).setOnClickListener(this); 46 | findViewById(R.id.empty).setOnClickListener(this); 47 | findViewById(R.id.error).setOnClickListener(this); 48 | findViewById(R.id.loading_nodata).setOnClickListener(this); 49 | } 50 | 51 | @Override public void onClick(View v) { 52 | if (listener == null) return; 53 | switch (v.getId()) { 54 | case R.id.add_header: 55 | listener.addHeader(); 56 | break; 57 | case R.id.remove_header: 58 | listener.removeHeader(); 59 | break; 60 | case R.id.refresh: 61 | listener.startRefresh(); 62 | break; 63 | case R.id.add_footer: 64 | listener.addFooter(); 65 | break; 66 | case R.id.remove_footer: 67 | listener.removeFooter(); 68 | break; 69 | case R.id.reset_adapter: 70 | listener.resetAdapter(); 71 | break; 72 | case R.id.refresh_loading: 73 | SRVTestConfig.getInstance() 74 | .setRefresh(true); 75 | Toast.makeText(getContext(), "再刷新试试看!", Toast.LENGTH_SHORT) 76 | .show(); 77 | break; 78 | case R.id.footer_loading: 79 | SRVTestConfig.getInstance() 80 | .setLoadingNormal(); 81 | Toast.makeText(getContext(), "加载更多正常", Toast.LENGTH_SHORT) 82 | .show(); 83 | break; 84 | case R.id.loading_error: 85 | SRVTestConfig.getInstance() 86 | .setLoadingError(true); 87 | Toast.makeText(getContext(), "加载更多会错误", Toast.LENGTH_SHORT) 88 | .show(); 89 | break; 90 | case R.id.loading_nodata: 91 | SRVTestConfig.getInstance() 92 | .setLoadingNoData(true); 93 | Toast.makeText(getContext(), "加载更多无数据", Toast.LENGTH_SHORT) 94 | .show(); 95 | break; 96 | case R.id.empty: 97 | SRVTestConfig.getInstance() 98 | .setRefreshEmpty(true); 99 | listener.showEmpty(); 100 | break; 101 | case R.id.error: 102 | SRVTestConfig.getInstance() 103 | .setRefreshError(true); 104 | listener.showError(); 105 | break; 106 | } 107 | dismiss(); 108 | } 109 | 110 | 111 | public interface TestListener { 112 | void addHeader(); 113 | 114 | void removeHeader(); 115 | 116 | void addFooter(); 117 | 118 | void removeFooter(); 119 | 120 | void startRefresh(); 121 | 122 | void resetAdapter(); 123 | 124 | void showEmpty(); 125 | 126 | void showError(); 127 | } 128 | 129 | private TestListener listener; 130 | 131 | public void setTestListener(TestListener listener) { 132 | this.listener = listener; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestEmptyView.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | 10 | import com.hzw.srecyclerview.AbsStateView; 11 | 12 | /** 13 | * author: hzw 14 | * time: 2018/2/5 下午5:25 15 | * description: 16 | */ 17 | 18 | public class TestEmptyView extends AbsStateView { 19 | public TestEmptyView(@NonNull Context context) { 20 | super(context); 21 | } 22 | 23 | public TestEmptyView(@NonNull Context context, @Nullable AttributeSet attrs) { 24 | super(context, attrs); 25 | } 26 | 27 | public TestEmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 28 | super(context, attrs, defStyleAttr); 29 | } 30 | 31 | @Override 32 | public void init() { 33 | View view = LayoutInflater.from(getContext()) 34 | .inflate(R.layout.empty_test, this, true); 35 | 36 | //空布局的点击刷新 37 | view.findViewById(R.id.empty_click).setOnClickListener(new OnClickListener() { 38 | @Override 39 | public void onClick(View v) { 40 | retry(true); 41 | } 42 | }); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestErrorView.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | 10 | import com.hzw.srecyclerview.AbsStateView; 11 | 12 | /** 13 | * author: hzw 14 | * time: 2018/2/5 下午5:25 15 | * description: 16 | */ 17 | 18 | public class TestErrorView extends AbsStateView { 19 | public TestErrorView(@NonNull Context context) { 20 | super(context); 21 | } 22 | 23 | public TestErrorView(@NonNull Context context, @Nullable AttributeSet attrs) { 24 | super(context, attrs); 25 | } 26 | 27 | public TestErrorView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 28 | super(context, attrs, defStyleAttr); 29 | } 30 | 31 | @Override 32 | public void init() { 33 | View view = LayoutInflater.from(getContext()) 34 | .inflate(R.layout.error_test, this, true); 35 | 36 | //空布局的点击刷新 37 | view.findViewById(R.id.empty_click).setOnClickListener(new OnClickListener() { 38 | @Override 39 | public void onClick(View v) { 40 | retry(true); 41 | } 42 | }); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestLoadFooter.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.support.annotation.Nullable; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | 11 | import com.hzw.srecyclerview.AbsLoadFooter; 12 | 13 | /** 14 | * 功能: 15 | * Created by 何志伟 on 2017/7/19. 16 | */ 17 | 18 | public class TestLoadFooter extends AbsLoadFooter { 19 | 20 | private View load, noMore, error; 21 | 22 | public TestLoadFooter(Context context) { 23 | super(context); 24 | } 25 | 26 | public TestLoadFooter(Context context, @Nullable AttributeSet attrs) { 27 | super(context, attrs); 28 | } 29 | 30 | public TestLoadFooter(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 31 | super(context, attrs, defStyleAttr); 32 | } 33 | 34 | @Override public void init() { 35 | View loadView = LayoutInflater.from(getContext()) 36 | .inflate(R.layout.srv_load_footer, this, false); 37 | noMore = loadView.findViewById(R.id.tv_src_loadNoMore); 38 | load = loadView.findViewById(R.id.v_srv_loading); 39 | error = loadView.findViewById(R.id.tv_src_loadError); 40 | error.setOnClickListener(new OnClickListener() { 41 | @Override public void onClick(View v) { 42 | errorRetry(); 43 | } 44 | }); 45 | addView(loadView); 46 | } 47 | 48 | @Override public void loadingState(int state) { 49 | switch (state) { 50 | case LOAD_SUCCESS: 51 | load.setVisibility(VISIBLE); 52 | noMore.setVisibility(GONE); 53 | break; 54 | case LOAD_ERROR: 55 | load.setVisibility(GONE); 56 | noMore.setVisibility(GONE); 57 | error.setVisibility(VISIBLE); 58 | break; 59 | case LOAD_NO_MORE: 60 | //据说有一种需求是,没数据时,直接不显示无数据的UI,此时可设置高度为0, 61 | //然后重写reset()方法,当列表刷新时会重置加载尾部 62 | ViewGroup.LayoutParams params = getLayoutParams(); 63 | params.height = 0; 64 | setLayoutParams(params); 65 | break; 66 | case LOAD_BEGIN: 67 | load.setVisibility(VISIBLE); 68 | noMore.setVisibility(GONE); 69 | error.setVisibility(GONE); 70 | break; 71 | } 72 | } 73 | 74 | /** 75 | * 刷新结束后如果需要重置加载尾部,可重写此方法重置LoadFooter 76 | */ 77 | @Override public void reset() { 78 | super.reset(); 79 | ViewGroup.LayoutParams params = getLayoutParams(); 80 | params.height = dip2px(45); 81 | setLayoutParams(params); 82 | } 83 | 84 | 85 | private int dip2px(float value) { 86 | final float scale = Resources.getSystem() 87 | .getDisplayMetrics().density; 88 | return (int) (value * scale + 0.5f); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestLoadingView.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.Nullable; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import com.hzw.srecyclerview.AbsStateView; 10 | 11 | /** 12 | * author: hzw 13 | * time: 2018/2/5 下午5:25 14 | * description: 15 | */ 16 | 17 | public class TestLoadingView extends AbsStateView { 18 | public TestLoadingView(@NonNull Context context) { 19 | super(context); 20 | } 21 | 22 | public TestLoadingView(@NonNull Context context, @Nullable AttributeSet attrs) { 23 | super(context, attrs); 24 | } 25 | 26 | public TestLoadingView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 27 | super(context, attrs, defStyleAttr); 28 | } 29 | 30 | @Override public void init() { 31 | LayoutInflater.from(getContext()) 32 | .inflate(R.layout.error_loading, this, true); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestRefreshHeader.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.support.annotation.Nullable; 6 | import android.util.AttributeSet; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | 11 | import com.hzw.srecyclerview.AbsRefreshHeader; 12 | 13 | /** 14 | * 功能: 15 | * Created by 何志伟 on 2017/7/17. 16 | */ 17 | 18 | public class TestRefreshHeader extends AbsRefreshHeader { 19 | 20 | private TextView refreshText; 21 | private ClockView clockView; 22 | 23 | public TestRefreshHeader(Context context) { 24 | super(context); 25 | } 26 | 27 | public TestRefreshHeader(Context context, @Nullable AttributeSet attrs) { 28 | super(context, attrs); 29 | } 30 | 31 | public TestRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 32 | super(context, attrs, defStyleAttr); 33 | } 34 | 35 | @Override 36 | public void init() { 37 | View view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_view, this, false); 38 | clockView = view.findViewById(R.id.v_refresh); 39 | refreshText = view.findViewById(R.id.tv_refresh); 40 | addView(view); 41 | } 42 | 43 | 44 | /** 45 | * 如果需要设置刷新动画时间,可以重写此方法 46 | */ 47 | @Override 48 | public int getRefreshDuration() { 49 | return 300; 50 | } 51 | 52 | /** 53 | * 如果需要设置头部的Gravity,可以重写此方法 54 | * 55 | * @return HEADER_CENTER,HEADER_BOTTOM 56 | */ 57 | @Override 58 | public int getRefreshGravity() { 59 | return AbsRefreshHeader.HEADER_CENTER; 60 | // return AbsRefreshHeader.HEADER_BOTTOM; 61 | } 62 | 63 | /** 64 | * 如果需要设置刷新高度,也就是刷新临界值,可以重写此方法 65 | */ 66 | @Override 67 | public int getRefreshHeight() { 68 | return dip2px(70); 69 | } 70 | 71 | private int dip2px(float value) { 72 | final float scale = Resources.getSystem().getDisplayMetrics().density; 73 | return (int) (value * scale + 0.5f); 74 | } 75 | 76 | /** 77 | * 刷新时下拉的灵敏度,数值越大越不灵敏 78 | */ 79 | @Override 80 | public int getRefreshSensitivity() { 81 | return 1; 82 | } 83 | 84 | /** 85 | * SRecyclerView的onDetachedFromWindow被调用,可能SRecyclerView所在的界面要被销毁, 86 | * 如果子类中有动画等未完成,可以重写此方法取消动画等耗时操作,避免造成内存泄露 87 | */ 88 | @Override 89 | public void srvDetachedFromWindow() { 90 | if (clockView != null) { 91 | clockView.resetClock(); 92 | } 93 | } 94 | 95 | @Override 96 | public void refresh(int state, int height) { 97 | switch (state) { 98 | case NORMAL: 99 | refreshText.setText("下拉刷新"); 100 | clockView.stopClockAnim(); 101 | break; 102 | case REFRESH: 103 | refreshText.setText("正在刷新..."); 104 | clockView.startClockAnim(); 105 | break; 106 | case PREPARE_NORMAL: 107 | refreshText.setText("下拉刷新"); 108 | clockView.setClockAngle(height); 109 | break; 110 | case PREPARE_REFRESH: 111 | refreshText.setText("释放立即刷新"); 112 | clockView.setClockAngle(height); 113 | break; 114 | } 115 | } 116 | 117 | 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/srecyclerviewproject/TestSRVModule.java: -------------------------------------------------------------------------------- 1 | package com.hzw.srecyclerviewproject; 2 | 3 | import android.content.Context; 4 | 5 | import com.hzw.srecyclerview.AbsStateView; 6 | import com.hzw.srecyclerview.AbsLoadFooter; 7 | import com.hzw.srecyclerview.AbsRefreshHeader; 8 | import com.hzw.srecyclerview.SRecyclerViewModule; 9 | 10 | /** 11 | * 功能: 12 | * Created by 何志伟 on 2017/7/17. 13 | */ 14 | 15 | public class TestSRVModule implements SRecyclerViewModule { 16 | @Override public AbsRefreshHeader getRefreshHeader(Context context) { 17 | return new TestRefreshHeader(context); 18 | } 19 | 20 | /** 21 | * 也可以只配置其中一项,使用默认的加载UI 22 | */ 23 | @Override public AbsLoadFooter getLoadingFooter(Context context) { 24 | return new TestLoadFooter(context); 25 | } 26 | 27 | @Override public AbsStateView getEmptyView(Context context) { 28 | return new TestEmptyView(context); 29 | //return null; 30 | } 31 | 32 | @Override public AbsStateView getErrorView(Context context) { 33 | return new TestErrorView(context); 34 | //return null; 35 | } 36 | 37 | @Override public AbsStateView getLoadingView(Context context) { 38 | return new TestLoadingView(context); 39 | //return null; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/srecyclerview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HzwSunshine/SRecyclerView/5729c610807438ec1fa7d8f293a3c859289b7f08/app/src/main/res/drawable/srecyclerview.gif -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 26 | 27 | 36 | 37 | 38 | 49 | 50 | 51 | 52 | 59 | 60 | -------------------------------------------------------------------------------- /app/src/main/res/layout/dialog_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 |