├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE.txt ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── tellh │ │ └── com │ │ └── recyclerstickyheaderview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── tellh │ │ │ └── com │ │ │ └── recyclerstickyheaderview │ │ │ ├── ItemHeader.java │ │ │ ├── ItemHeaderViewBinder.java │ │ │ ├── MainActivity.java │ │ │ ├── Result.java │ │ │ ├── User.java │ │ │ ├── UserItemViewBinder.java │ │ │ └── UserListAdapter.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── header.xml │ │ └── item_user.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── tellh │ └── com │ └── recyclerstickyheaderview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── raw └── effect.gif ├── settings.gradle └── stickyheaderview-rv ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── tellh │ └── com │ └── stickyheaderview_rv │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── tellh │ │ └── com │ │ └── stickyheaderview_rv │ │ ├── LinkListStack.java │ │ ├── StickyHeaderView.java │ │ └── adapter │ │ ├── DataBean.java │ │ ├── IViewBinder.java │ │ ├── IViewBinderProvider.java │ │ ├── LayoutItemType.java │ │ ├── StickyHeaderViewAdapter.java │ │ └── ViewBinder.java └── res │ └── values │ └── strings.xml └── test └── java └── tellh └── com └── stickyheaderview_rv └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE.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 {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RecyclerStickyHeaderView 2 | [![](https://jitpack.io/v/TellH/RecyclerStickyHeaderView.svg)](https://jitpack.io/#TellH/RecyclerStickyHeaderView)
3 | Sticky header view or suspending view for RecyclerView.
4 | [StickyListHeaders](https://github.com/emilsjolander/StickyListHeaders) is an Android library that makes it easy to integrate section headers 5 | stick to the top in ListView. Inspire by it, I setup this project to implement the same effect in RecyclerView. 6 | ## Effect 7 | ![](https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/master/raw/effect.gif) 8 | 9 | ## Usage 10 | 11 | ### Setup 12 | root build.gradle 13 | ```groovy 14 | allprojects { 15 | repositories { 16 | jcenter() 17 | maven { url "https://jitpack.io" } 18 | } 19 | } 20 | ``` 21 | app build.gradle 22 | ```groovy 23 | dependencies { 24 | compile 'com.github.TellH:RecyclerStickyHeaderView:1.1.0' 25 | } 26 | ``` 27 | 28 | ### Quick Start 29 | 30 | - Place RecylerView into StickyHeaderView 31 | ``` xml 32 | 36 | 37 | 43 | 44 | 45 | ``` 46 | 47 | - Create data bean class for each item type in RecyclerView. They should extend DataBean. Override the method
48 | `public boolean shouldSticky()` to decide whether the item view should be suspended on the top. 49 | ``` java 50 | public class User extends DataBean { 51 | private String login; 52 | private int id; 53 | private String avatar_url; 54 | private boolean shouldSticky; 55 | @Override 56 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 57 | return R.layout.item_user; 58 | } 59 | public void setShouldSticky(boolean shouldSticky) { 60 | this.shouldSticky = shouldSticky; 61 | } 62 | // Decide whether the item view should be suspended on the top. 63 | @Override 64 | public boolean shouldSticky() { 65 | return shouldSticky; 66 | } 67 | } 68 | public class ItemHeader extends DataBean { 69 | private String prefix; 70 | @Override 71 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 72 | return R.layout.header; 73 | } 74 | @Override 75 | public boolean shouldSticky() { 76 | return true; 77 | } 78 | } 79 | ``` 80 | 81 | - Create ViewBinder to bind different type views with specific data beans. 82 | As you see, `provideViewHolder(View itemView)` corresponds for `onCreateViewHolder` in RecyclerView, and `bindView` corresponds for `onBindViewHolder` in RecyclerView. 83 | 84 | ``` java 85 | public class ItemHeaderViewBinder extends ViewBinder { 86 | @Override 87 | public ViewHolder provideViewHolder(View itemView) { 88 | return new ViewHolder(itemView); 89 | } 90 | @Override 91 | public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, ItemHeader entity) { 92 | holder.tvPrefix.setText(entity.getPrefix()); 93 | } 94 | @Override 95 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 96 | return R.layout.header; 97 | } 98 | static class ViewHolder extends ViewBinder.ViewHolder { 99 | TextView tvPrefix; 100 | public ViewHolder(View rootView) { 101 | super(rootView); 102 | this.tvPrefix = (TextView) rootView.findViewById(R.id.tv_prefix); 103 | } 104 | } 105 | } 106 | ``` 107 | 108 | - Instantiate StickyHeaderViewAdapter for RecyclerView and register ViewBinders for each item types. 109 | ``` java 110 | rv = (RecyclerView) findViewById(R.id.recyclerView); 111 | rv.setLayoutManager(new LinearLayoutManager(this)); 112 | List userList = new ArrayList<>(); 113 | adapter = new StickyHeaderViewAdapter(userList) 114 | .RegisterItemType(new UserItemViewBinder()) 115 | .RegisterItemType(new ItemHeaderViewBinder()); 116 | rv.setAdapter(adapter); 117 | ``` 118 | 119 | That is all. 120 | 121 | Please check out the Demo and source code for more information. If you have any question, feel free to raise an issue. Thanks a lot! 122 | 123 | ## Thanks 124 | - [SuspensionBar](https://github.com/wuapnjie/SuspensionBar) 125 | - [NoListAdapter](https://github.com/TellH/NoListAdapter) 126 | 127 | ## License 128 | Copyright 2016 TellH 129 | 130 | Licensed under the Apache License, Version 2.0 (the "License"); 131 | you may not use this file except in compliance with the License. 132 | You may obtain a copy of the License at 133 | http://www.apache.org/licenses/LICENSE-2.0 134 | Unless required by applicable law or agreed to in writing, software 135 | distributed under the License is distributed on an "AS IS" BASIS, 136 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137 | See the License for the specific language governing permissions and 138 | limitations under the License. 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.0" 6 | defaultConfig { 7 | applicationId "tellh.com.recyclerstickyheaderview" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.1.0' 28 | compile 'com.android.support:design:25.1.0' 29 | compile 'com.google.code.gson:gson:2.7' 30 | compile 'com.squareup.picasso:picasso:2.5.2' 31 | testCompile 'junit:junit:4.12' 32 | compile project(':stickyheaderview-rv') 33 | // compile 'com.github.TellH:RecyclerStickyHeaderView:1.0.0' 34 | } 35 | -------------------------------------------------------------------------------- /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 D:\AndroidSDK\AndroidStudio/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/tellh/com/recyclerstickyheaderview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("tellh.com.recyclerstickyheaderview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/ItemHeader.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import tellh.com.stickyheaderview_rv.adapter.DataBean; 4 | import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter; 5 | 6 | /** 7 | * Created by tlh on 2017/1/22 :) 8 | */ 9 | 10 | public class ItemHeader extends DataBean { 11 | private String prefix; 12 | 13 | public String getPrefix() { 14 | return prefix; 15 | } 16 | 17 | public ItemHeader(String prefix) { 18 | this.prefix = prefix; 19 | } 20 | 21 | @Override 22 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 23 | return R.layout.header; 24 | } 25 | 26 | @Override 27 | public boolean shouldSticky() { 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/ItemHeaderViewBinder.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter; 7 | import tellh.com.stickyheaderview_rv.adapter.ViewBinder; 8 | 9 | /** 10 | * Created by tlh on 2017/1/22 :) 11 | */ 12 | 13 | public class ItemHeaderViewBinder extends ViewBinder { 14 | 15 | @Override 16 | public ViewHolder provideViewHolder(View itemView) { 17 | return new ViewHolder(itemView); 18 | } 19 | 20 | @Override 21 | public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, ItemHeader entity) { 22 | holder.tvPrefix.setText(entity.getPrefix()); 23 | } 24 | 25 | @Override 26 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 27 | return R.layout.header; 28 | } 29 | 30 | 31 | static class ViewHolder extends ViewBinder.ViewHolder { 32 | TextView tvPrefix; 33 | 34 | public ViewHolder(View rootView) { 35 | super(rootView); 36 | this.tvPrefix = (TextView) rootView.findViewById(R.id.tv_prefix); 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.Menu; 8 | import android.view.MenuItem; 9 | 10 | import com.google.gson.Gson; 11 | 12 | import java.util.ArrayList; 13 | import java.util.Collections; 14 | import java.util.Comparator; 15 | import java.util.List; 16 | import java.util.Random; 17 | 18 | import tellh.com.stickyheaderview_rv.adapter.DataBean; 19 | import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter; 20 | 21 | public class MainActivity extends AppCompatActivity { 22 | private RecyclerView rv; 23 | private StickyHeaderViewAdapter adapter; 24 | private Random random = new Random(System.currentTimeMillis()); 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | initView(); 31 | initData(); 32 | } 33 | 34 | private void initData() { 35 | Gson gson = new Gson(); 36 | Result result = gson.fromJson(User.dataSource, Result.class); 37 | List userList = result.getItems(); 38 | Collections.sort(userList, new Comparator() { 39 | @Override 40 | public int compare(User o1, User o2) { 41 | return o1.getLogin().compareToIgnoreCase(o2.getLogin()); 42 | } 43 | }); 44 | List userListBak = new ArrayList<>(); 45 | String currentPrefix = userList.get(0).getLogin().substring(0, 1).toUpperCase(); 46 | userListBak.add(new ItemHeader(currentPrefix)); 47 | for (User user : userList) { 48 | if (currentPrefix.compareToIgnoreCase(user.getLogin().substring(0, 1)) == 0) 49 | userListBak.add(user); 50 | else { 51 | currentPrefix = user.getLogin().substring(0, 1).toUpperCase(); 52 | userListBak.add(new ItemHeader(currentPrefix)); 53 | userListBak.add(user); 54 | } 55 | } 56 | adapter = new StickyHeaderViewAdapter(userListBak) 57 | .RegisterItemType(new UserItemViewBinder()) 58 | .RegisterItemType(new ItemHeaderViewBinder()); 59 | rv.setAdapter(adapter); 60 | } 61 | 62 | private void initView() { 63 | rv = (RecyclerView) findViewById(R.id.recyclerView); 64 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 65 | rv.setLayoutManager(linearLayoutManager); 66 | } 67 | 68 | @Override 69 | public boolean onCreateOptionsMenu(Menu menu) { 70 | getMenuInflater().inflate(R.menu.menu_main, menu); 71 | return true; 72 | } 73 | 74 | @Override 75 | public boolean onOptionsItemSelected(MenuItem item) { 76 | switch (item.getItemId()) { 77 | case R.id.action_add_view: 78 | User user = new User("Sticky View", 123, "https://avatars.githubusercontent.com/u/15800681?v=3"); 79 | user.setShouldSticky(random.nextBoolean()); 80 | adapter.append(user); 81 | break; 82 | case R.id.action_clear_all: 83 | adapter.clear(rv); 84 | break; 85 | default: 86 | break; 87 | } 88 | return super.onOptionsItemSelected(item); 89 | } 90 | 91 | 92 | } 93 | -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/Result.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by tlh on 2017/1/21 :) 7 | */ 8 | 9 | public class Result { 10 | private List items; 11 | 12 | public List getItems() { 13 | return items; 14 | } 15 | 16 | public void setItems(List items) { 17 | this.items = items; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/User.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import tellh.com.stickyheaderview_rv.adapter.DataBean; 4 | import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter; 5 | 6 | public class User extends DataBean { 7 | private String login; 8 | private int id; 9 | private String avatar_url; 10 | private boolean shouldSticky; 11 | 12 | public User() { 13 | } 14 | 15 | public User(String login, int id, String avatar_url) { 16 | this.login = login; 17 | this.id = id; 18 | this.avatar_url = avatar_url; 19 | } 20 | 21 | public String getAvatar_url() { 22 | return avatar_url; 23 | } 24 | 25 | public void setAvatar_url(String avatar_url) { 26 | this.avatar_url = avatar_url; 27 | } 28 | 29 | public String getLogin() { 30 | return login; 31 | } 32 | 33 | public void setLogin(String login) { 34 | this.login = login; 35 | } 36 | 37 | public int getId() { 38 | return id; 39 | } 40 | 41 | public void setId(int id) { 42 | this.id = id; 43 | } 44 | 45 | @Override 46 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 47 | return R.layout.item_user; 48 | } 49 | 50 | public void setShouldSticky(boolean shouldSticky) { 51 | this.shouldSticky = shouldSticky; 52 | } 53 | 54 | @Override 55 | public boolean shouldSticky() { 56 | return shouldSticky; 57 | } 58 | 59 | public static String dataSource = "{\"items\": [\n" + 60 | " {\n" + 61 | " \"login\": \"JakeWharton\",\n" + 62 | " \"id\": 66577,\n" + 63 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/66577?v=3\",\n" + 64 | " \"gravatar_id\": \"\",\n" + 65 | " \"url\": \"https://api.github.com/users/JakeWharton\",\n" + 66 | " \"html_url\": \"https://github.com/JakeWharton\",\n" + 67 | " \"followers_url\": \"https://api.github.com/users/JakeWharton/followers\",\n" + 68 | " \"following_url\": \"https://api.github.com/users/JakeWharton/following{/other_user}\",\n" + 69 | " \"gists_url\": \"https://api.github.com/users/JakeWharton/gists{/gist_id}\",\n" + 70 | " \"starred_url\": \"https://api.github.com/users/JakeWharton/starred{/owner}{/repo}\",\n" + 71 | " \"subscriptions_url\": \"https://api.github.com/users/JakeWharton/subscriptions\",\n" + 72 | " \"organizations_url\": \"https://api.github.com/users/JakeWharton/orgs\",\n" + 73 | " \"repos_url\": \"https://api.github.com/users/JakeWharton/repos\",\n" + 74 | " \"events_url\": \"https://api.github.com/users/JakeWharton/events{/privacy}\",\n" + 75 | " \"received_events_url\": \"https://api.github.com/users/JakeWharton/received_events\",\n" + 76 | " \"type\": \"User\",\n" + 77 | " \"site_admin\": false,\n" + 78 | " \"score\": 1.0\n" + 79 | " },\n" + 80 | " {\n" + 81 | " \"login\": \"Trinea\",\n" + 82 | " \"id\": 1169522,\n" + 83 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/1169522?v=3\",\n" + 84 | " \"gravatar_id\": \"\",\n" + 85 | " \"url\": \"https://api.github.com/users/Trinea\",\n" + 86 | " \"html_url\": \"https://github.com/Trinea\",\n" + 87 | " \"followers_url\": \"https://api.github.com/users/Trinea/followers\",\n" + 88 | " \"following_url\": \"https://api.github.com/users/Trinea/following{/other_user}\",\n" + 89 | " \"gists_url\": \"https://api.github.com/users/Trinea/gists{/gist_id}\",\n" + 90 | " \"starred_url\": \"https://api.github.com/users/Trinea/starred{/owner}{/repo}\",\n" + 91 | " \"subscriptions_url\": \"https://api.github.com/users/Trinea/subscriptions\",\n" + 92 | " \"organizations_url\": \"https://api.github.com/users/Trinea/orgs\",\n" + 93 | " \"repos_url\": \"https://api.github.com/users/Trinea/repos\",\n" + 94 | " \"events_url\": \"https://api.github.com/users/Trinea/events{/privacy}\",\n" + 95 | " \"received_events_url\": \"https://api.github.com/users/Trinea/received_events\",\n" + 96 | " \"type\": \"User\",\n" + 97 | " \"site_admin\": false,\n" + 98 | " \"score\": 1.0\n" + 99 | " },\n" + 100 | " {\n" + 101 | " \"login\": \"chrisbanes\",\n" + 102 | " \"id\": 227486,\n" + 103 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/227486?v=3\",\n" + 104 | " \"gravatar_id\": \"\",\n" + 105 | " \"url\": \"https://api.github.com/users/chrisbanes\",\n" + 106 | " \"html_url\": \"https://github.com/chrisbanes\",\n" + 107 | " \"followers_url\": \"https://api.github.com/users/chrisbanes/followers\",\n" + 108 | " \"following_url\": \"https://api.github.com/users/chrisbanes/following{/other_user}\",\n" + 109 | " \"gists_url\": \"https://api.github.com/users/chrisbanes/gists{/gist_id}\",\n" + 110 | " \"starred_url\": \"https://api.github.com/users/chrisbanes/starred{/owner}{/repo}\",\n" + 111 | " \"subscriptions_url\": \"https://api.github.com/users/chrisbanes/subscriptions\",\n" + 112 | " \"organizations_url\": \"https://api.github.com/users/chrisbanes/orgs\",\n" + 113 | " \"repos_url\": \"https://api.github.com/users/chrisbanes/repos\",\n" + 114 | " \"events_url\": \"https://api.github.com/users/chrisbanes/events{/privacy}\",\n" + 115 | " \"received_events_url\": \"https://api.github.com/users/chrisbanes/received_events\",\n" + 116 | " \"type\": \"User\",\n" + 117 | " \"site_admin\": false,\n" + 118 | " \"score\": 1.0\n" + 119 | " },\n" + 120 | " {\n" + 121 | " \"login\": \"romannurik\",\n" + 122 | " \"id\": 100155,\n" + 123 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/100155?v=3\",\n" + 124 | " \"gravatar_id\": \"\",\n" + 125 | " \"url\": \"https://api.github.com/users/romannurik\",\n" + 126 | " \"html_url\": \"https://github.com/romannurik\",\n" + 127 | " \"followers_url\": \"https://api.github.com/users/romannurik/followers\",\n" + 128 | " \"following_url\": \"https://api.github.com/users/romannurik/following{/other_user}\",\n" + 129 | " \"gists_url\": \"https://api.github.com/users/romannurik/gists{/gist_id}\",\n" + 130 | " \"starred_url\": \"https://api.github.com/users/romannurik/starred{/owner}{/repo}\",\n" + 131 | " \"subscriptions_url\": \"https://api.github.com/users/romannurik/subscriptions\",\n" + 132 | " \"organizations_url\": \"https://api.github.com/users/romannurik/orgs\",\n" + 133 | " \"repos_url\": \"https://api.github.com/users/romannurik/repos\",\n" + 134 | " \"events_url\": \"https://api.github.com/users/romannurik/events{/privacy}\",\n" + 135 | " \"received_events_url\": \"https://api.github.com/users/romannurik/received_events\",\n" + 136 | " \"type\": \"User\",\n" + 137 | " \"site_admin\": false,\n" + 138 | " \"score\": 1.0\n" + 139 | " },\n" + 140 | " {\n" + 141 | " \"login\": \"koush\",\n" + 142 | " \"id\": 73924,\n" + 143 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/73924?v=3\",\n" + 144 | " \"gravatar_id\": \"\",\n" + 145 | " \"url\": \"https://api.github.com/users/koush\",\n" + 146 | " \"html_url\": \"https://github.com/koush\",\n" + 147 | " \"followers_url\": \"https://api.github.com/users/koush/followers\",\n" + 148 | " \"following_url\": \"https://api.github.com/users/koush/following{/other_user}\",\n" + 149 | " \"gists_url\": \"https://api.github.com/users/koush/gists{/gist_id}\",\n" + 150 | " \"starred_url\": \"https://api.github.com/users/koush/starred{/owner}{/repo}\",\n" + 151 | " \"subscriptions_url\": \"https://api.github.com/users/koush/subscriptions\",\n" + 152 | " \"organizations_url\": \"https://api.github.com/users/koush/orgs\",\n" + 153 | " \"repos_url\": \"https://api.github.com/users/koush/repos\",\n" + 154 | " \"events_url\": \"https://api.github.com/users/koush/events{/privacy}\",\n" + 155 | " \"received_events_url\": \"https://api.github.com/users/koush/received_events\",\n" + 156 | " \"type\": \"User\",\n" + 157 | " \"site_admin\": false,\n" + 158 | " \"score\": 1.0\n" + 159 | " },\n" + 160 | " {\n" + 161 | " \"login\": \"commonsguy\",\n" + 162 | " \"id\": 103772,\n" + 163 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/103772?v=3\",\n" + 164 | " \"gravatar_id\": \"\",\n" + 165 | " \"url\": \"https://api.github.com/users/commonsguy\",\n" + 166 | " \"html_url\": \"https://github.com/commonsguy\",\n" + 167 | " \"followers_url\": \"https://api.github.com/users/commonsguy/followers\",\n" + 168 | " \"following_url\": \"https://api.github.com/users/commonsguy/following{/other_user}\",\n" + 169 | " \"gists_url\": \"https://api.github.com/users/commonsguy/gists{/gist_id}\",\n" + 170 | " \"starred_url\": \"https://api.github.com/users/commonsguy/starred{/owner}{/repo}\",\n" + 171 | " \"subscriptions_url\": \"https://api.github.com/users/commonsguy/subscriptions\",\n" + 172 | " \"organizations_url\": \"https://api.github.com/users/commonsguy/orgs\",\n" + 173 | " \"repos_url\": \"https://api.github.com/users/commonsguy/repos\",\n" + 174 | " \"events_url\": \"https://api.github.com/users/commonsguy/events{/privacy}\",\n" + 175 | " \"received_events_url\": \"https://api.github.com/users/commonsguy/received_events\",\n" + 176 | " \"type\": \"User\",\n" + 177 | " \"site_admin\": false,\n" + 178 | " \"score\": 1.0\n" + 179 | " },\n" + 180 | " {\n" + 181 | " \"login\": \"hongyangAndroid\",\n" + 182 | " \"id\": 10704521,\n" + 183 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/10704521?v=3\",\n" + 184 | " \"gravatar_id\": \"\",\n" + 185 | " \"url\": \"https://api.github.com/users/hongyangAndroid\",\n" + 186 | " \"html_url\": \"https://github.com/hongyangAndroid\",\n" + 187 | " \"followers_url\": \"https://api.github.com/users/hongyangAndroid/followers\",\n" + 188 | " \"following_url\": \"https://api.github.com/users/hongyangAndroid/following{/other_user}\",\n" + 189 | " \"gists_url\": \"https://api.github.com/users/hongyangAndroid/gists{/gist_id}\",\n" + 190 | " \"starred_url\": \"https://api.github.com/users/hongyangAndroid/starred{/owner}{/repo}\",\n" + 191 | " \"subscriptions_url\": \"https://api.github.com/users/hongyangAndroid/subscriptions\",\n" + 192 | " \"organizations_url\": \"https://api.github.com/users/hongyangAndroid/orgs\",\n" + 193 | " \"repos_url\": \"https://api.github.com/users/hongyangAndroid/repos\",\n" + 194 | " \"events_url\": \"https://api.github.com/users/hongyangAndroid/events{/privacy}\",\n" + 195 | " \"received_events_url\": \"https://api.github.com/users/hongyangAndroid/received_events\",\n" + 196 | " \"type\": \"User\",\n" + 197 | " \"site_admin\": false,\n" + 198 | " \"score\": 1.0\n" + 199 | " },\n" + 200 | " {\n" + 201 | " \"login\": \"romainguy\",\n" + 202 | " \"id\": 869684,\n" + 203 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/869684?v=3\",\n" + 204 | " \"gravatar_id\": \"\",\n" + 205 | " \"url\": \"https://api.github.com/users/romainguy\",\n" + 206 | " \"html_url\": \"https://github.com/romainguy\",\n" + 207 | " \"followers_url\": \"https://api.github.com/users/romainguy/followers\",\n" + 208 | " \"following_url\": \"https://api.github.com/users/romainguy/following{/other_user}\",\n" + 209 | " \"gists_url\": \"https://api.github.com/users/romainguy/gists{/gist_id}\",\n" + 210 | " \"starred_url\": \"https://api.github.com/users/romainguy/starred{/owner}{/repo}\",\n" + 211 | " \"subscriptions_url\": \"https://api.github.com/users/romainguy/subscriptions\",\n" + 212 | " \"organizations_url\": \"https://api.github.com/users/romainguy/orgs\",\n" + 213 | " \"repos_url\": \"https://api.github.com/users/romainguy/repos\",\n" + 214 | " \"events_url\": \"https://api.github.com/users/romainguy/events{/privacy}\",\n" + 215 | " \"received_events_url\": \"https://api.github.com/users/romainguy/received_events\",\n" + 216 | " \"type\": \"User\",\n" + 217 | " \"site_admin\": false,\n" + 218 | " \"score\": 1.0\n" + 219 | " },\n" + 220 | " {\n" + 221 | " \"login\": \"singwhatiwanna\",\n" + 222 | " \"id\": 3346272,\n" + 223 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/3346272?v=3\",\n" + 224 | " \"gravatar_id\": \"\",\n" + 225 | " \"url\": \"https://api.github.com/users/singwhatiwanna\",\n" + 226 | " \"html_url\": \"https://github.com/singwhatiwanna\",\n" + 227 | " \"followers_url\": \"https://api.github.com/users/singwhatiwanna/followers\",\n" + 228 | " \"following_url\": \"https://api.github.com/users/singwhatiwanna/following{/other_user}\",\n" + 229 | " \"gists_url\": \"https://api.github.com/users/singwhatiwanna/gists{/gist_id}\",\n" + 230 | " \"starred_url\": \"https://api.github.com/users/singwhatiwanna/starred{/owner}{/repo}\",\n" + 231 | " \"subscriptions_url\": \"https://api.github.com/users/singwhatiwanna/subscriptions\",\n" + 232 | " \"organizations_url\": \"https://api.github.com/users/singwhatiwanna/orgs\",\n" + 233 | " \"repos_url\": \"https://api.github.com/users/singwhatiwanna/repos\",\n" + 234 | " \"events_url\": \"https://api.github.com/users/singwhatiwanna/events{/privacy}\",\n" + 235 | " \"received_events_url\": \"https://api.github.com/users/singwhatiwanna/received_events\",\n" + 236 | " \"type\": \"User\",\n" + 237 | " \"site_admin\": false,\n" + 238 | " \"score\": 1.0\n" + 239 | " },\n" + 240 | " {\n" + 241 | " \"login\": \"kevinsawicki\",\n" + 242 | " \"id\": 671378,\n" + 243 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/671378?v=3\",\n" + 244 | " \"gravatar_id\": \"\",\n" + 245 | " \"url\": \"https://api.github.com/users/kevinsawicki\",\n" + 246 | " \"html_url\": \"https://github.com/kevinsawicki\",\n" + 247 | " \"followers_url\": \"https://api.github.com/users/kevinsawicki/followers\",\n" + 248 | " \"following_url\": \"https://api.github.com/users/kevinsawicki/following{/other_user}\",\n" + 249 | " \"gists_url\": \"https://api.github.com/users/kevinsawicki/gists{/gist_id}\",\n" + 250 | " \"starred_url\": \"https://api.github.com/users/kevinsawicki/starred{/owner}{/repo}\",\n" + 251 | " \"subscriptions_url\": \"https://api.github.com/users/kevinsawicki/subscriptions\",\n" + 252 | " \"organizations_url\": \"https://api.github.com/users/kevinsawicki/orgs\",\n" + 253 | " \"repos_url\": \"https://api.github.com/users/kevinsawicki/repos\",\n" + 254 | " \"events_url\": \"https://api.github.com/users/kevinsawicki/events{/privacy}\",\n" + 255 | " \"received_events_url\": \"https://api.github.com/users/kevinsawicki/received_events\",\n" + 256 | " \"type\": \"User\",\n" + 257 | " \"site_admin\": true,\n" + 258 | " \"score\": 1.0\n" + 259 | " },\n" + 260 | " {\n" + 261 | " \"login\": \"cyrilmottier\",\n" + 262 | " \"id\": 92794,\n" + 263 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/92794?v=3\",\n" + 264 | " \"gravatar_id\": \"\",\n" + 265 | " \"url\": \"https://api.github.com/users/cyrilmottier\",\n" + 266 | " \"html_url\": \"https://github.com/cyrilmottier\",\n" + 267 | " \"followers_url\": \"https://api.github.com/users/cyrilmottier/followers\",\n" + 268 | " \"following_url\": \"https://api.github.com/users/cyrilmottier/following{/other_user}\",\n" + 269 | " \"gists_url\": \"https://api.github.com/users/cyrilmottier/gists{/gist_id}\",\n" + 270 | " \"starred_url\": \"https://api.github.com/users/cyrilmottier/starred{/owner}{/repo}\",\n" + 271 | " \"subscriptions_url\": \"https://api.github.com/users/cyrilmottier/subscriptions\",\n" + 272 | " \"organizations_url\": \"https://api.github.com/users/cyrilmottier/orgs\",\n" + 273 | " \"repos_url\": \"https://api.github.com/users/cyrilmottier/repos\",\n" + 274 | " \"events_url\": \"https://api.github.com/users/cyrilmottier/events{/privacy}\",\n" + 275 | " \"received_events_url\": \"https://api.github.com/users/cyrilmottier/received_events\",\n" + 276 | " \"type\": \"User\",\n" + 277 | " \"site_admin\": false,\n" + 278 | " \"score\": 1.0\n" + 279 | " },\n" + 280 | " {\n" + 281 | " \"login\": \"jgilfelt\",\n" + 282 | " \"id\": 175697,\n" + 283 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/175697?v=3\",\n" + 284 | " \"gravatar_id\": \"\",\n" + 285 | " \"url\": \"https://api.github.com/users/jgilfelt\",\n" + 286 | " \"html_url\": \"https://github.com/jgilfelt\",\n" + 287 | " \"followers_url\": \"https://api.github.com/users/jgilfelt/followers\",\n" + 288 | " \"following_url\": \"https://api.github.com/users/jgilfelt/following{/other_user}\",\n" + 289 | " \"gists_url\": \"https://api.github.com/users/jgilfelt/gists{/gist_id}\",\n" + 290 | " \"starred_url\": \"https://api.github.com/users/jgilfelt/starred{/owner}{/repo}\",\n" + 291 | " \"subscriptions_url\": \"https://api.github.com/users/jgilfelt/subscriptions\",\n" + 292 | " \"organizations_url\": \"https://api.github.com/users/jgilfelt/orgs\",\n" + 293 | " \"repos_url\": \"https://api.github.com/users/jgilfelt/repos\",\n" + 294 | " \"events_url\": \"https://api.github.com/users/jgilfelt/events{/privacy}\",\n" + 295 | " \"received_events_url\": \"https://api.github.com/users/jgilfelt/received_events\",\n" + 296 | " \"type\": \"User\",\n" + 297 | " \"site_admin\": false,\n" + 298 | " \"score\": 1.0\n" + 299 | " },\n" + 300 | " {\n" + 301 | " \"login\": \"greenrobot\",\n" + 302 | " \"id\": 242242,\n" + 303 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/242242?v=3\",\n" + 304 | " \"gravatar_id\": \"\",\n" + 305 | " \"url\": \"https://api.github.com/users/greenrobot\",\n" + 306 | " \"html_url\": \"https://github.com/greenrobot\",\n" + 307 | " \"followers_url\": \"https://api.github.com/users/greenrobot/followers\",\n" + 308 | " \"following_url\": \"https://api.github.com/users/greenrobot/following{/other_user}\",\n" + 309 | " \"gists_url\": \"https://api.github.com/users/greenrobot/gists{/gist_id}\",\n" + 310 | " \"starred_url\": \"https://api.github.com/users/greenrobot/starred{/owner}{/repo}\",\n" + 311 | " \"subscriptions_url\": \"https://api.github.com/users/greenrobot/subscriptions\",\n" + 312 | " \"organizations_url\": \"https://api.github.com/users/greenrobot/orgs\",\n" + 313 | " \"repos_url\": \"https://api.github.com/users/greenrobot/repos\",\n" + 314 | " \"events_url\": \"https://api.github.com/users/greenrobot/events{/privacy}\",\n" + 315 | " \"received_events_url\": \"https://api.github.com/users/greenrobot/received_events\",\n" + 316 | " \"type\": \"User\",\n" + 317 | " \"site_admin\": false,\n" + 318 | " \"score\": 1.0\n" + 319 | " },\n" + 320 | " {\n" + 321 | " \"login\": \"drakeet\",\n" + 322 | " \"id\": 5214214,\n" + 323 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/5214214?v=3\",\n" + 324 | " \"gravatar_id\": \"\",\n" + 325 | " \"url\": \"https://api.github.com/users/drakeet\",\n" + 326 | " \"html_url\": \"https://github.com/drakeet\",\n" + 327 | " \"followers_url\": \"https://api.github.com/users/drakeet/followers\",\n" + 328 | " \"following_url\": \"https://api.github.com/users/drakeet/following{/other_user}\",\n" + 329 | " \"gists_url\": \"https://api.github.com/users/drakeet/gists{/gist_id}\",\n" + 330 | " \"starred_url\": \"https://api.github.com/users/drakeet/starred{/owner}{/repo}\",\n" + 331 | " \"subscriptions_url\": \"https://api.github.com/users/drakeet/subscriptions\",\n" + 332 | " \"organizations_url\": \"https://api.github.com/users/drakeet/orgs\",\n" + 333 | " \"repos_url\": \"https://api.github.com/users/drakeet/repos\",\n" + 334 | " \"events_url\": \"https://api.github.com/users/drakeet/events{/privacy}\",\n" + 335 | " \"received_events_url\": \"https://api.github.com/users/drakeet/received_events\",\n" + 336 | " \"type\": \"User\",\n" + 337 | " \"site_admin\": false,\n" + 338 | " \"score\": 1.0\n" + 339 | " },\n" + 340 | " {\n" + 341 | " \"login\": \"unclebob\",\n" + 342 | " \"id\": 36901,\n" + 343 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/36901?v=3\",\n" + 344 | " \"gravatar_id\": \"\",\n" + 345 | " \"url\": \"https://api.github.com/users/unclebob\",\n" + 346 | " \"html_url\": \"https://github.com/unclebob\",\n" + 347 | " \"followers_url\": \"https://api.github.com/users/unclebob/followers\",\n" + 348 | " \"following_url\": \"https://api.github.com/users/unclebob/following{/other_user}\",\n" + 349 | " \"gists_url\": \"https://api.github.com/users/unclebob/gists{/gist_id}\",\n" + 350 | " \"starred_url\": \"https://api.github.com/users/unclebob/starred{/owner}{/repo}\",\n" + 351 | " \"subscriptions_url\": \"https://api.github.com/users/unclebob/subscriptions\",\n" + 352 | " \"organizations_url\": \"https://api.github.com/users/unclebob/orgs\",\n" + 353 | " \"repos_url\": \"https://api.github.com/users/unclebob/repos\",\n" + 354 | " \"events_url\": \"https://api.github.com/users/unclebob/events{/privacy}\",\n" + 355 | " \"received_events_url\": \"https://api.github.com/users/unclebob/received_events\",\n" + 356 | " \"type\": \"User\",\n" + 357 | " \"site_admin\": false,\n" + 358 | " \"score\": 1.0\n" + 359 | " },\n" + 360 | " {\n" + 361 | " \"login\": \"rengwuxian\",\n" + 362 | " \"id\": 4454687,\n" + 363 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/4454687?v=3\",\n" + 364 | " \"gravatar_id\": \"\",\n" + 365 | " \"url\": \"https://api.github.com/users/rengwuxian\",\n" + 366 | " \"html_url\": \"https://github.com/rengwuxian\",\n" + 367 | " \"followers_url\": \"https://api.github.com/users/rengwuxian/followers\",\n" + 368 | " \"following_url\": \"https://api.github.com/users/rengwuxian/following{/other_user}\",\n" + 369 | " \"gists_url\": \"https://api.github.com/users/rengwuxian/gists{/gist_id}\",\n" + 370 | " \"starred_url\": \"https://api.github.com/users/rengwuxian/starred{/owner}{/repo}\",\n" + 371 | " \"subscriptions_url\": \"https://api.github.com/users/rengwuxian/subscriptions\",\n" + 372 | " \"organizations_url\": \"https://api.github.com/users/rengwuxian/orgs\",\n" + 373 | " \"repos_url\": \"https://api.github.com/users/rengwuxian/repos\",\n" + 374 | " \"events_url\": \"https://api.github.com/users/rengwuxian/events{/privacy}\",\n" + 375 | " \"received_events_url\": \"https://api.github.com/users/rengwuxian/received_events\",\n" + 376 | " \"type\": \"User\",\n" + 377 | " \"site_admin\": false,\n" + 378 | " \"score\": 1.0\n" + 379 | " },\n" + 380 | " {\n" + 381 | " \"login\": \"rovo89\",\n" + 382 | " \"id\": 1573299,\n" + 383 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/1573299?v=3\",\n" + 384 | " \"gravatar_id\": \"\",\n" + 385 | " \"url\": \"https://api.github.com/users/rovo89\",\n" + 386 | " \"html_url\": \"https://github.com/rovo89\",\n" + 387 | " \"followers_url\": \"https://api.github.com/users/rovo89/followers\",\n" + 388 | " \"following_url\": \"https://api.github.com/users/rovo89/following{/other_user}\",\n" + 389 | " \"gists_url\": \"https://api.github.com/users/rovo89/gists{/gist_id}\",\n" + 390 | " \"starred_url\": \"https://api.github.com/users/rovo89/starred{/owner}{/repo}\",\n" + 391 | " \"subscriptions_url\": \"https://api.github.com/users/rovo89/subscriptions\",\n" + 392 | " \"organizations_url\": \"https://api.github.com/users/rovo89/orgs\",\n" + 393 | " \"repos_url\": \"https://api.github.com/users/rovo89/repos\",\n" + 394 | " \"events_url\": \"https://api.github.com/users/rovo89/events{/privacy}\",\n" + 395 | " \"received_events_url\": \"https://api.github.com/users/rovo89/received_events\",\n" + 396 | " \"type\": \"User\",\n" + 397 | " \"site_admin\": false,\n" + 398 | " \"score\": 1.0\n" + 399 | " },\n" + 400 | " {\n" + 401 | " \"login\": \"jfeinstein10\",\n" + 402 | " \"id\": 1269143,\n" + 403 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/1269143?v=3\",\n" + 404 | " \"gravatar_id\": \"\",\n" + 405 | " \"url\": \"https://api.github.com/users/jfeinstein10\",\n" + 406 | " \"html_url\": \"https://github.com/jfeinstein10\",\n" + 407 | " \"followers_url\": \"https://api.github.com/users/jfeinstein10/followers\",\n" + 408 | " \"following_url\": \"https://api.github.com/users/jfeinstein10/following{/other_user}\",\n" + 409 | " \"gists_url\": \"https://api.github.com/users/jfeinstein10/gists{/gist_id}\",\n" + 410 | " \"starred_url\": \"https://api.github.com/users/jfeinstein10/starred{/owner}{/repo}\",\n" + 411 | " \"subscriptions_url\": \"https://api.github.com/users/jfeinstein10/subscriptions\",\n" + 412 | " \"organizations_url\": \"https://api.github.com/users/jfeinstein10/orgs\",\n" + 413 | " \"repos_url\": \"https://api.github.com/users/jfeinstein10/repos\",\n" + 414 | " \"events_url\": \"https://api.github.com/users/jfeinstein10/events{/privacy}\",\n" + 415 | " \"received_events_url\": \"https://api.github.com/users/jfeinstein10/received_events\",\n" + 416 | " \"type\": \"User\",\n" + 417 | " \"site_admin\": false,\n" + 418 | " \"score\": 1.0\n" + 419 | " },\n" + 420 | " {\n" + 421 | " \"login\": \"SimonVT\",\n" + 422 | " \"id\": 549365,\n" + 423 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/549365?v=3\",\n" + 424 | " \"gravatar_id\": \"\",\n" + 425 | " \"url\": \"https://api.github.com/users/SimonVT\",\n" + 426 | " \"html_url\": \"https://github.com/SimonVT\",\n" + 427 | " \"followers_url\": \"https://api.github.com/users/SimonVT/followers\",\n" + 428 | " \"following_url\": \"https://api.github.com/users/SimonVT/following{/other_user}\",\n" + 429 | " \"gists_url\": \"https://api.github.com/users/SimonVT/gists{/gist_id}\",\n" + 430 | " \"starred_url\": \"https://api.github.com/users/SimonVT/starred{/owner}{/repo}\",\n" + 431 | " \"subscriptions_url\": \"https://api.github.com/users/SimonVT/subscriptions\",\n" + 432 | " \"organizations_url\": \"https://api.github.com/users/SimonVT/orgs\",\n" + 433 | " \"repos_url\": \"https://api.github.com/users/SimonVT/repos\",\n" + 434 | " \"events_url\": \"https://api.github.com/users/SimonVT/events{/privacy}\",\n" + 435 | " \"received_events_url\": \"https://api.github.com/users/SimonVT/received_events\",\n" + 436 | " \"type\": \"User\",\n" + 437 | " \"site_admin\": false,\n" + 438 | " \"score\": 1.0\n" + 439 | " },\n" + 440 | " {\n" + 441 | " \"login\": \"shiffman\",\n" + 442 | " \"id\": 191758,\n" + 443 | " \"avatar_url\": \"https://avatars.githubusercontent.com/u/191758?v=3\",\n" + 444 | " \"gravatar_id\": \"\",\n" + 445 | " \"url\": \"https://api.github.com/users/shiffman\",\n" + 446 | " \"html_url\": \"https://github.com/shiffman\",\n" + 447 | " \"followers_url\": \"https://api.github.com/users/shiffman/followers\",\n" + 448 | " \"following_url\": \"https://api.github.com/users/shiffman/following{/other_user}\",\n" + 449 | " \"gists_url\": \"https://api.github.com/users/shiffman/gists{/gist_id}\",\n" + 450 | " \"starred_url\": \"https://api.github.com/users/shiffman/starred{/owner}{/repo}\",\n" + 451 | " \"subscriptions_url\": \"https://api.github.com/users/shiffman/subscriptions\",\n" + 452 | " \"organizations_url\": \"https://api.github.com/users/shiffman/orgs\",\n" + 453 | " \"repos_url\": \"https://api.github.com/users/shiffman/repos\",\n" + 454 | " \"events_url\": \"https://api.github.com/users/shiffman/events{/privacy}\",\n" + 455 | " \"received_events_url\": \"https://api.github.com/users/shiffman/received_events\",\n" + 456 | " \"type\": \"User\",\n" + 457 | " \"site_admin\": false,\n" + 458 | " \"score\": 1.0\n" + 459 | " }\n" + 460 | " ]" + 461 | "}"; 462 | 463 | 464 | } -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/UserItemViewBinder.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import android.view.View; 4 | import android.widget.ImageView; 5 | import android.widget.TextView; 6 | 7 | import com.squareup.picasso.Picasso; 8 | 9 | import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter; 10 | import tellh.com.stickyheaderview_rv.adapter.ViewBinder; 11 | 12 | /** 13 | * Created by tlh on 2017/1/22 :) 14 | */ 15 | 16 | public class UserItemViewBinder extends ViewBinder { 17 | @Override 18 | public ViewHolder provideViewHolder(View itemView) { 19 | return new ViewHolder(itemView); 20 | } 21 | 22 | @Override 23 | public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, User entity) { 24 | holder.tvId.setText(String.valueOf(entity.getId())); 25 | holder.tvName.setText(entity.getLogin()); 26 | Picasso.with(holder.ivAvatar.getContext()) 27 | .load(entity.getAvatar_url()) 28 | .placeholder(R.mipmap.ic_launcher) 29 | .into(holder.ivAvatar); 30 | } 31 | 32 | @Override 33 | public int getItemLayoutId(StickyHeaderViewAdapter adapter) { 34 | return R.layout.item_user; 35 | } 36 | 37 | static class ViewHolder extends ViewBinder.ViewHolder { 38 | public TextView tvId; 39 | public ImageView ivAvatar; 40 | public TextView tvName; 41 | 42 | public ViewHolder(View rootView) { 43 | super(rootView); 44 | this.tvId = (TextView) rootView.findViewById(R.id.tv_id); 45 | this.ivAvatar = (ImageView) rootView.findViewById(R.id.iv_avatar); 46 | this.tvName = (TextView) rootView.findViewById(R.id.tv_name); 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/tellh/com/recyclerstickyheaderview/UserListAdapter.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.ImageView; 8 | import android.widget.TextView; 9 | 10 | import com.squareup.picasso.Picasso; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * Created by tlh on 2017/1/21 :) 16 | */ 17 | public class UserListAdapter extends RecyclerView.Adapter { 18 | private List items; 19 | 20 | public UserListAdapter(List items) { 21 | this.items = items; 22 | } 23 | 24 | @Override 25 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 26 | View v = LayoutInflater.from(parent.getContext()) 27 | .inflate(R.layout.item_user, parent, false); 28 | return new ViewHolder(v); 29 | } 30 | 31 | @Override 32 | public void onBindViewHolder(ViewHolder holder, int position) { 33 | User item = items.get(position); 34 | holder.tvId.setText(String.valueOf(item.getId())); 35 | holder.tvName.setText(item.getLogin()); 36 | Picasso.with(holder.ivAvatar.getContext()) 37 | .load(item.getAvatar_url()) 38 | .placeholder(R.mipmap.ic_launcher) 39 | .into(holder.ivAvatar); 40 | } 41 | 42 | @Override 43 | public int getItemCount() { 44 | if (items == null) { 45 | return 0; 46 | } 47 | return items.size(); 48 | } 49 | 50 | public static class ViewHolder extends RecyclerView.ViewHolder { 51 | private TextView tvId; 52 | private ImageView ivAvatar; 53 | private TextView tvName; 54 | 55 | public ViewHolder(View rootView) { 56 | super(rootView); 57 | this.tvId = (TextView) rootView.findViewById(R.id.tv_id); 58 | this.ivAvatar = (ImageView) rootView.findViewById(R.id.iv_avatar); 59 | this.tvName = (TextView) rootView.findViewById(R.id.tv_name); 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/header.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_user.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 23 | 24 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RecyclerStickyHeaderView 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/tellh/com/recyclerstickyheaderview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package tellh.com.recyclerstickyheaderview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | 13 | // JitPack 14 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | jcenter() 21 | maven { url 'https://jitpack.io' } 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /raw/effect.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TellH/RecyclerStickyHeaderView/aaeb10929cd879dcb2018e6ab8800776402dd14c/raw/effect.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':stickyheaderview-rv' 2 | -------------------------------------------------------------------------------- /stickyheaderview-rv/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /stickyheaderview-rv/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | // JitPack 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | group='com.github.TellH' 5 | android { 6 | compileSdkVersion 25 7 | buildToolsVersion "25.0.0" 8 | 9 | defaultConfig { 10 | minSdkVersion 14 11 | targetSdkVersion 25 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 29 | exclude group: 'com.android.support', module: 'support-annotations' 30 | }) 31 | compile 'com.android.support:appcompat-v7:25.1.0' 32 | compile 'com.android.support:design:25.1.0' 33 | compile "com.android.support:recyclerview-v7:25.1.0" 34 | testCompile 'junit:junit:4.12' 35 | } 36 | -------------------------------------------------------------------------------- /stickyheaderview-rv/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 D:\AndroidSDK\AndroidStudio/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 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/androidTest/java/tellh/com/stickyheaderview_rv/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("tellh.com.stickyheaderview_rv.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/LinkListStack.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv; 2 | 3 | import java.util.EmptyStackException; 4 | import java.util.LinkedList; 5 | 6 | /** 7 | * Created by tlh on 2017/5/11 :) 8 | */ 9 | 10 | public class LinkListStack { 11 | private LinkedList list = new LinkedList<>(); 12 | 13 | public E push(E item) { 14 | list.addLast(item); 15 | return item; 16 | } 17 | 18 | public E pop() { 19 | if (list.isEmpty()) 20 | return null; 21 | return list.removeLast(); 22 | } 23 | 24 | public synchronized E peek() { 25 | if (list.size() == 0) 26 | throw new EmptyStackException(); 27 | return list.peekLast(); 28 | } 29 | 30 | public boolean isEmpty() { 31 | return list.size() == 0; 32 | } 33 | 34 | public void clear() { 35 | list.clear(); 36 | } 37 | 38 | public boolean remove(E item) { 39 | return list.remove(item); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/StickyHeaderView.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.AttributeSet; 8 | import android.util.SparseArray; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.FrameLayout; 13 | 14 | import tellh.com.stickyheaderview_rv.adapter.DataBean; 15 | import tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter; 16 | import tellh.com.stickyheaderview_rv.adapter.ViewBinder; 17 | 18 | /** 19 | * Created by tlh on 2017/1/21 :) 20 | */ 21 | 22 | public class StickyHeaderView extends FrameLayout 23 | implements StickyHeaderViewAdapter.DataSetChangeListener { 24 | private boolean hasInit = false; 25 | private FrameLayout mHeaderContainer; 26 | private RecyclerView mRecyclerView; 27 | private int mHeaderHeight = -1; 28 | private StickyHeaderViewAdapter adapter; 29 | private LinearLayoutManager layoutManager; 30 | private LinkListStack stickyHeaderStack = new LinkListStack<>(); 31 | private SparseArray mViewHolderCache; 32 | 33 | public StickyHeaderView(Context context) { 34 | super(context); 35 | } 36 | 37 | public StickyHeaderView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | } 40 | 41 | public StickyHeaderView(Context context, AttributeSet attrs, int defStyleAttr) { 42 | super(context, attrs, defStyleAttr); 43 | } 44 | 45 | private void initView() { 46 | if (hasInit) { 47 | return; 48 | } 49 | hasInit = true; 50 | View view = getChildAt(0); 51 | if (!(view instanceof RecyclerView)) 52 | throw new RuntimeException("RecyclerView should be the first child view."); 53 | mRecyclerView = (RecyclerView) view; 54 | mHeaderContainer = new FrameLayout(getContext()); 55 | mHeaderContainer.setBackgroundColor(Color.WHITE); 56 | mHeaderContainer.setLayoutParams( 57 | new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 58 | addView(mHeaderContainer); 59 | mRecyclerView.addOnScrollListener( 60 | new RecyclerView.OnScrollListener() { 61 | @Override 62 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 63 | super.onScrollStateChanged(recyclerView, newState); 64 | if (mHeaderHeight == -1 || adapter == null || layoutManager == null) { 65 | mHeaderHeight = mHeaderContainer.getHeight(); 66 | RecyclerView.Adapter adapter = mRecyclerView.getAdapter(); 67 | if (!(adapter instanceof StickyHeaderViewAdapter)) 68 | throw new RuntimeException 69 | ("Your RecyclerView.Adapter should be the type of StickyHeaderViewAdapter."); 70 | StickyHeaderView.this.adapter = (StickyHeaderViewAdapter) adapter; 71 | StickyHeaderView.this.adapter.setDataSetChangeListener(StickyHeaderView.this); 72 | layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); 73 | mViewHolderCache = new SparseArray<>(); 74 | } /*else if (mHeaderHeight == 0) { 75 | mHeaderHeight = mHeaderContainer.getHeight(); 76 | }*/ 77 | 78 | } 79 | 80 | @Override 81 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 82 | super.onScrolled(recyclerView, dx, dy); 83 | if (mHeaderHeight == -1 || adapter == null || layoutManager == null) 84 | return; 85 | if (stickyHeaderStack.isEmpty() && adapter.getDisplayListSize() != 0) 86 | stickyHeaderStack.push(adapter.get(findFirstVisibleStickyHeaderPosition(0))); 87 | int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); 88 | if (firstVisibleItemPosition == -1) 89 | return; 90 | int firstVisibleStickyHeaderPosition = findFirstVisibleStickyHeaderPosition(firstVisibleItemPosition); 91 | int currentStickyHeaderPosition = adapter.getPosition(stickyHeaderStack.peek()); 92 | if (firstVisibleStickyHeaderPosition - firstVisibleItemPosition > 1) { 93 | return; 94 | } 95 | // 如果两个连续两个都是StickyView, 取下面那个View 96 | DataBean dataBean = adapter.get(firstVisibleStickyHeaderPosition + 1); 97 | if (dataBean != null && dataBean.shouldSticky()) 98 | firstVisibleStickyHeaderPosition++; 99 | View firstVisibleStickyHeader = layoutManager.findViewByPosition(firstVisibleStickyHeaderPosition); 100 | if (firstVisibleStickyHeader == null) 101 | return; 102 | int headerTop = firstVisibleStickyHeader.getTop(); 103 | if (headerTop > 0 && headerTop <= mHeaderHeight) { //吸顶正在更替的状态 104 | mHeaderContainer.setY(-(mHeaderHeight - headerTop)); 105 | if (firstVisibleStickyHeaderPosition == currentStickyHeaderPosition) { 106 | stickyHeaderStack.pop(); 107 | if (!stickyHeaderStack.isEmpty()) 108 | updateHeaderView(stickyHeaderStack.peek()); 109 | } 110 | } else if (headerTop <= 0) { //吸顶稳定在最上方的状态 111 | mHeaderContainer.setY(0); 112 | updateHeaderView(adapter.get(firstVisibleItemPosition)); 113 | } 114 | 115 | // Cache the StickyHeader position. 116 | if (firstVisibleStickyHeaderPosition > currentStickyHeaderPosition) 117 | stickyHeaderStack.push(adapter.get(firstVisibleStickyHeaderPosition)); 118 | else if (firstVisibleStickyHeaderPosition < currentStickyHeaderPosition) 119 | stickyHeaderStack.pop(); 120 | } 121 | } 122 | 123 | ); 124 | 125 | } 126 | 127 | private int findFirstVisibleStickyHeaderPosition(int firstVisibleItemPosition) { 128 | int i = firstVisibleItemPosition; 129 | for (; i < adapter.getDisplayListSize(); i++) { 130 | if (!adapter.get(i).shouldSticky()) 131 | continue; 132 | break; 133 | } 134 | return i; 135 | } 136 | 137 | private void updateHeaderView(DataBean entity) { 138 | int layoutId = entity.getItemLayoutId(adapter); 139 | clearHeaderView(); 140 | RecyclerView.ViewHolder viewHolder = mViewHolderCache.get(layoutId); 141 | ViewBinder headerViewBinder = adapter.getViewBinder(layoutId); 142 | if (viewHolder == null) { 143 | View v = LayoutInflater.from(mHeaderContainer.getContext()) 144 | .inflate(layoutId, mHeaderContainer, false); 145 | viewHolder = headerViewBinder.provideViewHolder(v); 146 | mViewHolderCache.put(layoutId, viewHolder); 147 | } 148 | mHeaderContainer.addView(viewHolder.itemView); 149 | mHeaderHeight = mHeaderContainer.getHeight(); 150 | headerViewBinder.bindView(adapter, viewHolder, adapter.getPosition(entity), entity); 151 | } 152 | 153 | // Remove the Header View 154 | private void clearHeaderView() { 155 | mHeaderContainer.removeAllViews(); 156 | } 157 | 158 | @Override 159 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 160 | initView(); 161 | super.onLayout(changed, left, top, right, bottom); 162 | } 163 | 164 | @Override 165 | public void onClearAll() { 166 | stickyHeaderStack.clear(); 167 | clearHeaderView(); 168 | } 169 | 170 | @Override 171 | public void remove(int pos) { 172 | DataBean entity = adapter.get(pos); 173 | if (stickyHeaderStack.peek() == entity) 174 | clearHeaderView(); 175 | if (entity != null && entity.shouldSticky()) { 176 | stickyHeaderStack.remove(entity); 177 | } 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/adapter/DataBean.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv.adapter; 2 | 3 | import android.support.v4.util.SparseArrayCompat; 4 | 5 | public abstract class DataBean implements IViewBinderProvider, LayoutItemType { 6 | private IViewBinder viewBinder; 7 | 8 | @Override 9 | public final IViewBinder provideViewBinder(StickyHeaderViewAdapter adapter, SparseArrayCompat viewBinderPool, int position) { 10 | if (viewBinder == null) { 11 | viewBinder = viewBinderPool.get(getItemLayoutId(adapter)); 12 | } 13 | return viewBinder; 14 | } 15 | 16 | public boolean shouldSticky() { 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/adapter/IViewBinder.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv.adapter; 2 | 3 | import android.view.View; 4 | 5 | public interface IViewBinder extends LayoutItemType { 6 | VH provideViewHolder(View itemView); 7 | 8 | void bindView(StickyHeaderViewAdapter adapter, VH holder, int position, T entity); 9 | } -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/adapter/IViewBinderProvider.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv.adapter; 2 | 3 | import android.support.v4.util.SparseArrayCompat; 4 | 5 | public interface IViewBinderProvider { 6 | IViewBinder provideViewBinder(StickyHeaderViewAdapter adapter, SparseArrayCompat viewBinderPool, int position); 7 | } -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/adapter/LayoutItemType.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv.adapter; 2 | 3 | public interface LayoutItemType { 4 | int getItemLayoutId(StickyHeaderViewAdapter adapter); 5 | } -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/adapter/StickyHeaderViewAdapter.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv.adapter; 2 | 3 | import android.support.annotation.LayoutRes; 4 | import android.support.v4.util.SparseArrayCompat; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class StickyHeaderViewAdapter extends RecyclerView.Adapter { 14 | private static int SIZE_VIEW_BINDER_POOL = 10; 15 | private SparseArrayCompat viewBinderPool; 16 | protected List displayList; 17 | private DataSetChangeListener onDataSetChangeListener; 18 | 19 | public StickyHeaderViewAdapter(List displayList) { 20 | viewBinderPool = new SparseArrayCompat<>(SIZE_VIEW_BINDER_POOL); 21 | // if (displayList == null) 22 | this.displayList = new ArrayList<>(); 23 | this.displayList.addAll(displayList); 24 | } 25 | 26 | private List getDisplayList() { 27 | return displayList; 28 | } 29 | 30 | public int getDisplayListSize() { 31 | return displayList == null ? 0 : displayList.size(); 32 | } 33 | 34 | public DataBean get(int i) { 35 | if (i < getDisplayListSize()) 36 | return displayList.get(i); 37 | else return null; 38 | } 39 | 40 | public int getPosition(DataBean dataBean) { 41 | return displayList.indexOf(dataBean); 42 | } 43 | 44 | @Override 45 | public int getItemViewType(int position) { 46 | return displayList.get(position).getItemLayoutId(this); 47 | } 48 | 49 | 50 | @Override 51 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 52 | ViewBinder viewBinder = getViewBinder(viewType); 53 | View itemView = LayoutInflater.from(parent.getContext()) 54 | .inflate(viewBinder.getItemLayoutId(this), parent, false); 55 | return viewBinder.provideViewHolder(itemView); 56 | } 57 | 58 | public ViewBinder getViewBinder(int viewType) { 59 | return viewBinderPool.get(viewType); 60 | } 61 | 62 | @Override 63 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 64 | //normal view 65 | DataBean dataBean = displayList.get(position); 66 | dataBean.provideViewBinder(this, viewBinderPool, position).bindView(this, holder, position, dataBean); 67 | } 68 | 69 | @Override 70 | public int getItemCount() { 71 | return displayList == null ? 0 : displayList.size(); 72 | } 73 | 74 | public ViewBinder findViewBinderFromPool(@LayoutRes int layoutId) { 75 | return viewBinderPool.get(layoutId); 76 | } 77 | 78 | public static void setViewBinderPoolInitSize(int size) { 79 | SIZE_VIEW_BINDER_POOL = size; 80 | } 81 | 82 | public void clearViewBinderCache() { 83 | viewBinderPool.clear(); 84 | } 85 | 86 | public void append(List list) { 87 | if (list == null) 88 | return; 89 | displayList.addAll(list); 90 | notifyDataSetChanged(); 91 | } 92 | 93 | public void append(DataBean dataBean) { 94 | displayList.add(dataBean); 95 | notifyItemInserted(displayList.size() - 1); 96 | } 97 | 98 | public void refresh(List list) { 99 | if (list == null) 100 | return; 101 | onDataSetChangeListener.onClearAll(); 102 | displayList.clear(); 103 | displayList.addAll(list); 104 | notifyDataSetChanged(); 105 | } 106 | 107 | public void delete(int pos) { 108 | onDataSetChangeListener.remove(pos); 109 | displayList.remove(pos); 110 | notifyItemRemoved(pos); 111 | } 112 | 113 | public void clear(RecyclerView recyclerView) { 114 | onDataSetChangeListener.onClearAll(); 115 | displayList.clear(); 116 | notifyDataSetChanged(); 117 | recyclerView.scrollToPosition(0); 118 | } 119 | 120 | public StickyHeaderViewAdapter RegisterItemType(ViewBinder viewBinder) { 121 | viewBinderPool.put(viewBinder.getItemLayoutId(this), viewBinder); 122 | return this; 123 | } 124 | 125 | // Don't Call this Method 126 | public void setDataSetChangeListener(DataSetChangeListener listener) { 127 | this.onDataSetChangeListener = listener; 128 | } 129 | 130 | public interface DataSetChangeListener { 131 | void onClearAll(); 132 | 133 | void remove(int pos); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/java/tellh/com/stickyheaderview_rv/adapter/ViewBinder.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv.adapter; 2 | 3 | import android.support.annotation.IdRes; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.View; 6 | 7 | public abstract class ViewBinder implements IViewBinder { 8 | public abstract VH provideViewHolder(View itemView); 9 | 10 | public abstract void bindView(StickyHeaderViewAdapter adapter, VH holder, int position, T entity); 11 | 12 | public static class ViewHolder extends RecyclerView.ViewHolder { 13 | public ViewHolder(View rootView) { 14 | super(rootView); 15 | } 16 | 17 | protected T findViewById(@IdRes int id) { 18 | return (T) itemView.findViewById(id); 19 | } 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /stickyheaderview-rv/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | StickyHeaderView-rv 3 | 4 | -------------------------------------------------------------------------------- /stickyheaderview-rv/src/test/java/tellh/com/stickyheaderview_rv/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package tellh.com.stickyheaderview_rv; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } --------------------------------------------------------------------------------