├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── ittiger │ │ └── indexlist │ │ └── demo │ │ ├── CityActivity.java │ │ ├── ContactActivity.java │ │ ├── IndexStickyViewActivity.java │ │ ├── IndexStickyViewDecoration.java │ │ └── entity │ │ ├── CityEntity.java │ │ ├── ContactEntity.java │ │ ├── MenuEntity.java │ │ └── UserEntity.java │ └── res │ ├── drawable-xxhdpi │ ├── angel.png │ ├── banner.jpg │ ├── bell.png │ ├── christmas.png │ └── snowman.png │ ├── drawable │ ├── ic_toolbar_add.xml │ ├── vector_add.xml │ ├── vector_add_focus.xml │ ├── vector_contact_focus.xml │ └── vector_del.xml │ ├── layout │ ├── activity_city.xml │ ├── activity_contact.xml │ ├── activity_index_sticky_view.xml │ ├── indexsticky_header_contact_banner.xml │ ├── indexsticky_item_contact.xml │ └── indexsticky_item_index.xml │ ├── menu │ └── toolbar_menu.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 │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gif └── contact.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── indexstickyview ├── .gitignore ├── bintrayUpload.gradle ├── build.gradle ├── proguard-rules.pro ├── project.properties └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── ittiger │ │ └── indexlist │ │ ├── IndexStickyView.java │ │ ├── IndexValueBus.java │ │ ├── ItemType.java │ │ ├── SideBar.java │ │ ├── adapter │ │ ├── IndexHeaderFooterAdapter.java │ │ └── IndexStickyViewAdapter.java │ │ ├── entity │ │ ├── BaseEntity.java │ │ └── IndexStickyEntity.java │ │ ├── helper │ │ ├── ComparatorFactory.java │ │ ├── ConvertHelper.java │ │ └── PinYinHelper.java │ │ └── listener │ │ ├── OnItemClickListener.java │ │ └── OnItemLongClickListener.java │ └── res │ ├── drawable │ └── indexstickyview_center_overlay_bg.xml │ └── values │ └── attrs.xml └── settings.gradle /.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 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Abstraction issuesJava 39 | 40 | 41 | Android 42 | 43 | 44 | Android Lint 45 | 46 | 47 | Assignment issuesGroovy 48 | 49 | 50 | Assignment issuesJava 51 | 52 | 53 | Bitwise operation issuesJava 54 | 55 | 56 | C/C++ 57 | 58 | 59 | Class metricsJava 60 | 61 | 62 | Class structureJava 63 | 64 | 65 | Cloning issuesJava 66 | 67 | 68 | Code maturity issuesJava 69 | 70 | 71 | Code style issuesJava 72 | 73 | 74 | Compiler issuesJava 75 | 76 | 77 | Concurrency annotation issuesJava 78 | 79 | 80 | Control FlowGroovy 81 | 82 | 83 | Control flow issuesJava 84 | 85 | 86 | CorrectnessLintAndroid 87 | 88 | 89 | Data flow analysisC/C++ 90 | 91 | 92 | Data flow issuesGroovy 93 | 94 | 95 | Data flow issuesJava 96 | 97 | 98 | Declaration orderC/C++ 99 | 100 | 101 | Declaration redundancyGroovy 102 | 103 | 104 | Declaration redundancyJava 105 | 106 | 107 | Dependency issuesJava 108 | 109 | 110 | Encapsulation issuesJava 111 | 112 | 113 | Error handlingGroovy 114 | 115 | 116 | Error handlingJava 117 | 118 | 119 | Finalization issuesJava 120 | 121 | 122 | FunctionsC/C++ 123 | 124 | 125 | GPath inspectionsGroovy 126 | 127 | 128 | General 129 | 130 | 131 | GeneralC/C++ 132 | 133 | 134 | GeneralJava 135 | 136 | 137 | Google Cloud Endpoints 138 | 139 | 140 | Gradle 141 | 142 | 143 | Groovy 144 | 145 | 146 | HTML 147 | 148 | 149 | ImportsJava 150 | 151 | 152 | Inheritance issuesJava 153 | 154 | 155 | Initialization issuesJava 156 | 157 | 158 | Internationalization issues 159 | 160 | 161 | Internationalization issuesJava 162 | 163 | 164 | J2ME issuesJava 165 | 166 | 167 | JUnit issues 168 | 169 | 170 | JUnit issuesJava 171 | 172 | 173 | Java 174 | 175 | 176 | Java language level issuesJava 177 | 178 | 179 | Java language level migration aidsJava 180 | 181 | 182 | JavaBeans issuesJava 183 | 184 | 185 | Javadoc issuesJava 186 | 187 | 188 | Language Injection 189 | 190 | 191 | LintAndroid 192 | 193 | 194 | Logging issuesJava 195 | 196 | 197 | Manifest 198 | 199 | 200 | Maven 201 | 202 | 203 | Memory issuesJava 204 | 205 | 206 | Method MetricsGroovy 207 | 208 | 209 | Method metricsJava 210 | 211 | 212 | Modularization issuesJava 213 | 214 | 215 | Naming ConventionsGroovy 216 | 217 | 218 | Naming conventionsJava 219 | 220 | 221 | Numeric issuesJava 222 | 223 | 224 | OtherGroovy 225 | 226 | 227 | Packaging issuesJava 228 | 229 | 230 | Performance issuesJava 231 | 232 | 233 | Portability issuesJava 234 | 235 | 236 | Potentially confusing code constructsGroovy 237 | 238 | 239 | Probable bugsGradle 240 | 241 | 242 | Probable bugsGroovy 243 | 244 | 245 | Probable bugsJava 246 | 247 | 248 | Properties Files 249 | 250 | 251 | RELAX NG 252 | 253 | 254 | Resource management issuesJava 255 | 256 | 257 | Security issuesJava 258 | 259 | 260 | Serialization issuesJava 261 | 262 | 263 | TestNG 264 | 265 | 266 | Threading issuesGroovy 267 | 268 | 269 | Threading issuesJava 270 | 271 | 272 | Type checksC/C++ 273 | 274 | 275 | Unused codeC/C++ 276 | 277 | 278 | Validity issuesGroovy 279 | 280 | 281 | Verbose or redundant code constructsJava 282 | 283 | 284 | Visibility issuesJava 285 | 286 | 287 | XML 288 | 289 | 290 | toString() issuesJava 291 | 292 | 293 | 294 | 295 | Android 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 317 | 318 | 319 | 320 | 321 | 322 | 327 | 328 | 329 | 330 | 331 | 332 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 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 | # IndexStickyView 2 | 1. 列表索引 3 | 2. 滑动过程中顶部Title固定 4 | 3. 支持自定义Bar的颜色样式 5 | 4. 支持添加自定义`Header`或`Footer`不同类型的`View` 6 | 5. 支持`ItemClick`和`ItemLongClick`事件 7 | 6. 支持动态添加,删除数据和`Header`及`Footer` 8 | 9 | 10 | ### 个人微信公众号,欢迎扫码关注交流: 11 | ![](https://img-blog.csdnimg.cn/2019052410035231.jpg) 12 | 13 | # 效果Gif 14 | 图片名称 15 | 16 | # 使用 17 | ### gradle 18 | 在项目的`build.gradle`文件中添加如下依赖 19 | `compile 'cn.ittiger:indexstickyview:1.1.0'` 20 | 21 | ### xml布局 22 | ```xml 23 | //SideBar的宽度 35 | ``` 36 | 37 | ### 集成 38 | 1. 数据实体类实现接口`BaseEntity` 39 | ```java 40 | public class CityEntity implements BaseEntity { 41 | private String mCityName; 42 | 43 | public CityEntity(String cityName) { 44 | 45 | mCityName = cityName; 46 | } 47 | 48 | @Override 49 | public String getIndexField() { 50 | 51 | return mCityName; 52 | } 53 | 54 | public String getCityName() { 55 | 56 | return mCityName; 57 | } 58 | ``` 59 | 2. 继承`IndexStickyViewAdapter` 60 | ```java 61 | private class CityAdapter extends IndexStickyViewAdapter { 62 | 63 | public CityAdapter(List originalList) { 64 | 65 | super(originalList);//要展示的数据 66 | } 67 | 68 | @Override 69 | public RecyclerView.ViewHolder onCreateIndexViewHolder(ViewGroup parent) { 70 | ...//索引视图ViewHolder,如:热门城市 视图 71 | } 72 | 73 | @Override 74 | public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) { 75 | ...//城市内容视图ViewHolder 76 | } 77 | 78 | @Override 79 | public void onBindIndexViewHolder(RecyclerView.ViewHolder holder, int position, String indexName) { 80 | ...//给索引视图绑定数据 81 | } 82 | 83 | @Override 84 | public void onBindContentViewHolder(RecyclerView.ViewHolder holder, int position, CityEntity itemData) { 85 | ...//给内容视图绑定数据 86 | } 87 | } 88 | ``` 89 | 3. 设置视图的数据适配器 90 | ```java 91 | CityAdapter adapter = new CityAdapter(initCitys()); 92 | mIndexStickyView.setAdapter(mAdapter);//设置数据适配器 93 | adapter.setOnItemClickListener(this);//设置内容项点击响应 94 | adapter.setOnItemLongClickListener(this);//设置内容项长响应 95 | mIndexStickyView.addItemDecoration(...);//设置列表的ItemDecoration 96 | ``` 97 | 4. 添加自定义`HeaderView`和`FooterView` 98 | 5. 继承`IndexHeaderFooterAdapter`,有两个构造函数,默认构造函数和三参数构造函数 99 | 6. 使用默认构造函不需要任何实体类,其视图需要100%自定义 100 | 7. 使用三参数构造函数`IndexHeaderFooterAdapter(String indexValue, String indexName, List list)`,三个参数分别为`SideBar`上显示的索引文字,索引视图上显示的索引标题,要显示的实体数据列表。实体列表中的泛型可以和主Adapter中的泛型不一样,但是必须是`BaseEntity`实现。 101 | ```java 102 | //三参数构造函数,索引视图与CityAdapter中的索引视图一样 103 | IndexHeaderFooterAdapter hotCityHeaderAdapter = new IndexHeaderFooterAdapter( 104 | "热", "热门城市", initHotCitys() 105 | ) { 106 | @Override 107 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 108 | ...//HeaderView内容视图ViewHolder 109 | } 110 | 111 | @Override 112 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, CityEntity itemData) { 113 | ...//绑定FooterView内容数据 114 | } 115 | }; 116 | ``` 117 | 6. `mIndexStickyView.addIndexHeaderAdapter(hotCityHeaderAdapter);`//添加`HeaderView` 118 | 7. `mIndexStickyView.addIndexFooterAdapter(hotCityHeaderAdapter);`//添加`FooterView` 119 | 8. 默认构造函数实现添加`HeaderView`和`FooterView` 120 | ```java 121 | //默认造函数,无数据绑定,视图完全自定义 122 | IndexHeaderFooterAdapter hotCityHeaderAdapter = new IndexHeaderFooterAdapter() { 123 | @Override 124 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 125 | ...//完全自定义的View 126 | } 127 | 128 | @Override 129 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, BaseEntity itemData) { 130 | ...//完全自定义的View 131 | } 132 | }; 133 | ``` 134 | 135 | 136 | 137 | 具体实现细节请参考`Demo`实现 138 | 139 | 140 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | //butterknife 4 | apply plugin: 'android-apt' 5 | 6 | android { 7 | compileSdkVersion 24 8 | buildToolsVersion "24.0.2" 9 | defaultConfig { 10 | applicationId "cn.ittiger.indexlist.demo" 11 | minSdkVersion 14 12 | targetSdkVersion 24 13 | versionCode 1 14 | versionName "1.0" 15 | vectorDrawables.useSupportLibrary=true 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile project(':indexstickyview') 27 | compile 'com.android.support:appcompat-v7:24.2.1' 28 | 29 | //butterknife 30 | compile 'com.jakewharton:butterknife:8.4.0' 31 | apt 'com.jakewharton:butterknife-compiler:8.4.0' 32 | } 33 | -------------------------------------------------------------------------------- /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 /home/baina/ylhu/program/android-sdk-linux/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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 20 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/CityActivity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo; 2 | 3 | import butterknife.BindView; 4 | import butterknife.ButterKnife; 5 | import cn.ittiger.indexlist.IndexStickyView; 6 | import cn.ittiger.indexlist.adapter.IndexHeaderFooterAdapter; 7 | import cn.ittiger.indexlist.adapter.IndexStickyViewAdapter; 8 | import cn.ittiger.indexlist.demo.entity.CityEntity; 9 | import cn.ittiger.indexlist.demo.entity.ContactEntity; 10 | import cn.ittiger.indexlist.listener.OnItemClickListener; 11 | import cn.ittiger.indexlist.listener.OnItemLongClickListener; 12 | 13 | import android.content.Context; 14 | import android.graphics.Color; 15 | import android.os.Bundle; 16 | import android.support.v7.app.AppCompatActivity; 17 | import android.support.v7.widget.RecyclerView; 18 | import android.support.v7.widget.Toolbar; 19 | import android.view.LayoutInflater; 20 | import android.view.View; 21 | import android.view.ViewGroup; 22 | import android.widget.TextView; 23 | import android.widget.Toast; 24 | 25 | import java.util.ArrayList; 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | /** 30 | * @author: laohu on 2016/12/25 31 | * @site: http://ittiger.cn 32 | */ 33 | public class CityActivity extends AppCompatActivity implements OnItemClickListener, 34 | OnItemLongClickListener { 35 | @BindView(R.id.toolbar) 36 | Toolbar mToolbar; 37 | @BindView(R.id.indexStickyView) 38 | IndexStickyView mIndexStickyView; 39 | CityAdapter mAdapter; 40 | private Context mContext; 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | 45 | super.onCreate(savedInstanceState); 46 | mContext = this; 47 | setContentView(R.layout.activity_city); 48 | ButterKnife.bind(this); 49 | setSupportActionBar(mToolbar); 50 | getSupportActionBar().setTitle("城市列表"); 51 | 52 | mIndexStickyView.addItemDecoration(new IndexStickyViewDecoration(this)); 53 | mAdapter = new CityAdapter(initCitys()); 54 | mAdapter.setOnItemClickListener(this); 55 | mAdapter.setOnItemLongClickListener(this); 56 | mIndexStickyView.setAdapter(mAdapter); 57 | 58 | IndexHeaderFooterAdapter hotCityHeaderAdapter = new IndexHeaderFooterAdapter( 59 | "热", "热门城市", initHotCitys() 60 | ) { 61 | @Override 62 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 63 | 64 | View view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false); 65 | return new CityViewHolder(view); 66 | } 67 | 68 | @Override 69 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, CityEntity itemData) { 70 | CityViewHolder cityViewHolder = (CityViewHolder) holder; 71 | cityViewHolder.mTextView.setText(itemData.getCityName()); 72 | } 73 | }; 74 | hotCityHeaderAdapter.setOnItemClickListener(this); 75 | hotCityHeaderAdapter.setOnItemLongClickListener(this); 76 | mIndexStickyView.addIndexHeaderAdapter(hotCityHeaderAdapter); 77 | 78 | IndexHeaderFooterAdapter locationCityHeaderAdapter = new IndexHeaderFooterAdapter( 79 | "定", "当前城市", initLocationCitys() 80 | ) { 81 | @Override 82 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 83 | 84 | View view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false); 85 | return new CityViewHolder(view); 86 | } 87 | 88 | @Override 89 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, CityEntity itemData) { 90 | CityViewHolder cityViewHolder = (CityViewHolder) holder; 91 | cityViewHolder.mTextView.setText(itemData.getCityName()); 92 | } 93 | }; 94 | locationCityHeaderAdapter.setOnItemClickListener(this); 95 | locationCityHeaderAdapter.setOnItemLongClickListener(this); 96 | mIndexStickyView.addIndexHeaderAdapter(locationCityHeaderAdapter); 97 | 98 | } 99 | 100 | List initCitys() { 101 | List list = new ArrayList<>(); 102 | // 初始化数据 103 | List contactStrings = Arrays.asList(getResources().getStringArray(R.array.city_array)); 104 | for (int i = 0; i < contactStrings.size(); i++) { 105 | CityEntity cityEntity = new CityEntity(contactStrings.get(i)); 106 | list.add(cityEntity); 107 | } 108 | return list; 109 | } 110 | 111 | List initHotCitys() { 112 | List list = new ArrayList<>(); 113 | list.add(new CityEntity("北京")); 114 | list.add(new CityEntity("上海")); 115 | list.add(new CityEntity("深圳")); 116 | list.add(new CityEntity("武汉")); 117 | return list; 118 | } 119 | 120 | List initLocationCitys() { 121 | List list = new ArrayList<>(); 122 | list.add(new CityEntity("武汉")); 123 | return list; 124 | } 125 | 126 | private class CityAdapter extends IndexStickyViewAdapter { 127 | 128 | public CityAdapter(List originalList) { 129 | 130 | super(originalList); 131 | } 132 | 133 | @Override 134 | public RecyclerView.ViewHolder onCreateIndexViewHolder(ViewGroup parent) { 135 | 136 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_item_index, parent, false); 137 | return new IndexViewHolder(view); 138 | } 139 | 140 | @Override 141 | public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) { 142 | 143 | View view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false); 144 | return new CityViewHolder(view); 145 | } 146 | 147 | @Override 148 | public void onBindIndexViewHolder(RecyclerView.ViewHolder holder, int position, String indexName) { 149 | 150 | IndexViewHolder indexViewHolder = (IndexViewHolder) holder; 151 | indexViewHolder.mTextView.setText(indexName); 152 | } 153 | 154 | @Override 155 | public void onBindContentViewHolder(RecyclerView.ViewHolder holder, int position, CityEntity itemData) { 156 | 157 | CityViewHolder cityViewHolder = (CityViewHolder) holder; 158 | cityViewHolder.mTextView.setText(itemData.getCityName()); 159 | } 160 | } 161 | 162 | class CityViewHolder extends RecyclerView.ViewHolder { 163 | TextView mTextView; 164 | public CityViewHolder(View itemView) { 165 | 166 | super(itemView); 167 | mTextView = (TextView) itemView; 168 | } 169 | } 170 | 171 | class IndexViewHolder extends RecyclerView.ViewHolder { 172 | TextView mTextView; 173 | public IndexViewHolder(View itemView) { 174 | 175 | super(itemView); 176 | mTextView = (TextView) itemView.findViewById(R.id.tv_index); 177 | } 178 | } 179 | 180 | @Override 181 | public void onItemClick(View childView, int position, CityEntity item) { 182 | 183 | Toast.makeText(mContext, "点击:" + item.getCityName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 184 | } 185 | 186 | @Override 187 | public void onItemLongClick(View childView, int position, CityEntity item) { 188 | 189 | Toast.makeText(mContext, "长按:" + item.getCityName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/ContactActivity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo; 2 | 3 | import butterknife.BindView; 4 | import butterknife.ButterKnife; 5 | import cn.ittiger.indexlist.IndexStickyView; 6 | import cn.ittiger.indexlist.adapter.IndexHeaderFooterAdapter; 7 | import cn.ittiger.indexlist.adapter.IndexStickyViewAdapter; 8 | import cn.ittiger.indexlist.demo.entity.ContactEntity; 9 | import cn.ittiger.indexlist.demo.entity.MenuEntity; 10 | import cn.ittiger.indexlist.demo.entity.UserEntity; 11 | import cn.ittiger.indexlist.entity.BaseEntity; 12 | import cn.ittiger.indexlist.listener.OnItemClickListener; 13 | import cn.ittiger.indexlist.listener.OnItemLongClickListener; 14 | 15 | import android.content.Context; 16 | import android.os.Bundle; 17 | import android.support.v4.widget.SwipeRefreshLayout; 18 | import android.support.v7.app.AppCompatActivity; 19 | import android.support.v7.widget.RecyclerView; 20 | import android.support.v7.widget.Toolbar; 21 | import android.view.LayoutInflater; 22 | import android.view.Menu; 23 | import android.view.MenuItem; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | import android.widget.ImageView; 27 | import android.widget.TextView; 28 | import android.widget.Toast; 29 | 30 | import java.util.ArrayList; 31 | import java.util.Arrays; 32 | import java.util.List; 33 | 34 | /** 35 | * @author: laohu on 2016/12/25 36 | * @site: http://ittiger.cn 37 | */ 38 | public class ContactActivity extends AppCompatActivity implements 39 | Toolbar.OnMenuItemClickListener, OnItemClickListener, OnItemLongClickListener { 40 | @BindView(R.id.toolbar) 41 | Toolbar mToolbar; 42 | @BindView(R.id.swipeRefreshLayout) 43 | SwipeRefreshLayout mRefreshLayout; 44 | @BindView(R.id.indexStickyView) 45 | IndexStickyView mIndexStickyView; 46 | MyIndexStickyViewAdapter mAdapter; 47 | private Context mContext; 48 | 49 | IndexHeaderFooterAdapter mFavAdapter; 50 | IndexHeaderFooterAdapter mMenuAdapter; 51 | IndexHeaderFooterAdapter mNormalAdapter; 52 | IndexHeaderFooterAdapter mBannerAdapter; 53 | IndexHeaderFooterAdapter mFooterAdapter; 54 | IndexHeaderFooterAdapter mFooterBannerAdapter; 55 | 56 | @Override 57 | protected void onCreate(Bundle savedInstanceState) { 58 | 59 | super.onCreate(savedInstanceState); 60 | mContext = this; 61 | setContentView(R.layout.activity_contact); 62 | ButterKnife.bind(this); 63 | setSupportActionBar(mToolbar); 64 | getSupportActionBar().setTitle("联系人列表"); 65 | mToolbar.setOnMenuItemClickListener(this); 66 | initEditData(); 67 | 68 | mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 69 | @Override 70 | public void onRefresh() { 71 | mRefreshLayout.setRefreshing(true); 72 | mRefreshLayout.postDelayed(new Runnable() { 73 | @Override 74 | public void run() { 75 | mAdapter.reset(initDatas()); 76 | mRefreshLayout.setRefreshing(false); 77 | } 78 | }, 3000); 79 | } 80 | }); 81 | 82 | mAdapter = new MyIndexStickyViewAdapter(initDatas()); 83 | mIndexStickyView.setAdapter(mAdapter); 84 | mIndexStickyView.addItemDecoration(new IndexStickyViewDecoration(this)); 85 | 86 | //自定义添加头部收藏信息 87 | mFavAdapter = new IndexHeaderFooterAdapter("☆", "我的收藏", initFavDatas()) { 88 | @Override 89 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 90 | 91 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_item_contact, parent, false); 92 | return new ContentViewHolder(view); 93 | } 94 | 95 | @Override 96 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, UserEntity itemData) { 97 | 98 | ContentViewHolder contentViewHolder = (ContentViewHolder) holder; 99 | contentViewHolder.mMobile.setText(itemData.getMobile()); 100 | contentViewHolder.mName.setText(itemData.getName()); 101 | } 102 | }; 103 | mFavAdapter.setOnItemClickListener(new OnItemClickListener() { 104 | @Override 105 | public void onItemClick(View childView, int position, UserEntity item) { 106 | Toast.makeText(mContext, "点击:" + item.getName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 107 | } 108 | }); 109 | mIndexStickyView.addIndexHeaderAdapter(mFavAdapter); 110 | 111 | 112 | //添加自定义头部菜单项 113 | mMenuAdapter = new IndexHeaderFooterAdapter("↑", null, initMenuDatas()) { 114 | @Override 115 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 116 | 117 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_item_contact, parent, false); 118 | return new ContentViewHolder(view); 119 | } 120 | 121 | @Override 122 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, MenuEntity itemData) { 123 | 124 | ContentViewHolder contentViewHolder = (ContentViewHolder) holder; 125 | contentViewHolder.mMobile.setVisibility(View.GONE); 126 | contentViewHolder.mName.setText(itemData.getMenuTitle()); 127 | contentViewHolder.mAvatar.setImageResource(itemData.getMenuIconRes()); 128 | } 129 | }; 130 | mMenuAdapter.setOnItemLongClickListener(new OnItemLongClickListener() { 131 | @Override 132 | public void onItemLongClick(View childView, int position, MenuEntity item) { 133 | Toast.makeText(mContext, "长按:" + item.getMenuTitle() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 134 | } 135 | }); 136 | mIndexStickyView.addIndexHeaderAdapter(mMenuAdapter); 137 | 138 | //添加一个长度为1的数据来作为普通视图的数据源 139 | UserEntity[] entitys = {new UserEntity("数据绑定普通HeaderView", "13312345654")}; 140 | mNormalAdapter = new IndexHeaderFooterAdapter(null, null, Arrays.asList(entitys)) { 141 | @Override 142 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 143 | 144 | View view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false); 145 | return new MyViewHolder(view); 146 | } 147 | 148 | @Override 149 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, UserEntity itemData) { 150 | 151 | ((MyViewHolder)holder).mTextView.setText(itemData.getName()); 152 | } 153 | 154 | class MyViewHolder extends RecyclerView.ViewHolder { 155 | TextView mTextView; 156 | 157 | public MyViewHolder(View itemView) { 158 | super(itemView); 159 | mTextView = (TextView) itemView; 160 | } 161 | } 162 | }; 163 | mNormalAdapter.setOnItemClickListener(new OnItemClickListener() { 164 | @Override 165 | public void onItemClick(View childView, int position, UserEntity item) { 166 | Toast.makeText(mContext, "普通Header视图点击:" + item.getName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 167 | } 168 | }); 169 | mIndexStickyView.addIndexHeaderAdapter(mNormalAdapter); 170 | 171 | //自定义添加一个图片作为头部普通视图 172 | mBannerAdapter = new IndexHeaderFooterAdapter() { 173 | @Override 174 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 175 | 176 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_header_contact_banner, parent, false); 177 | ImageViewVH vh = new ImageViewVH(view); 178 | vh.img.setOnClickListener(new View.OnClickListener() { 179 | @Override 180 | public void onClick(View v) { 181 | Toast.makeText(mContext, "头部普通图片视图点击", Toast.LENGTH_SHORT).show(); 182 | } 183 | }); 184 | return vh; 185 | } 186 | 187 | @Override 188 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, BaseEntity itemData) { 189 | 190 | } 191 | 192 | class ImageViewVH extends RecyclerView.ViewHolder { 193 | ImageView img; 194 | 195 | public ImageViewVH(View itemView) { 196 | super(itemView); 197 | img = (ImageView) itemView.findViewById(R.id.img); 198 | } 199 | } 200 | }; 201 | mIndexStickyView.addIndexHeaderAdapter(mBannerAdapter); 202 | 203 | 204 | //添加一个底部自定义列表 205 | mFooterAdapter = new IndexHeaderFooterAdapter("$", "Footer", initFavDatas()) { 206 | @Override 207 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 208 | 209 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_item_contact, parent, false); 210 | return new ContentViewHolder(view); 211 | } 212 | 213 | @Override 214 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, UserEntity itemData) { 215 | ContentViewHolder contentViewHolder = (ContentViewHolder) holder; 216 | contentViewHolder.mMobile.setText(itemData.getMobile()); 217 | contentViewHolder.mName.setText(itemData.getName()); 218 | } 219 | }; 220 | mFooterAdapter.setOnItemClickListener(new OnItemClickListener() { 221 | @Override 222 | public void onItemClick(View childView, int position, UserEntity item) { 223 | Toast.makeText(mContext, "点击Footer:" + item.getName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 224 | } 225 | }); 226 | mIndexStickyView.addIndexFooterAdapter(mFooterAdapter); 227 | 228 | //Footer Banner 229 | mFooterBannerAdapter = new IndexHeaderFooterAdapter() { 230 | @Override 231 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) { 232 | 233 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_header_contact_banner, parent, false); 234 | ImageViewVH vh = new ImageViewVH(view); 235 | vh.img.setOnClickListener(new View.OnClickListener() { 236 | @Override 237 | public void onClick(View v) { 238 | Toast.makeText(mContext, "Footer图片视图点击", Toast.LENGTH_SHORT).show(); 239 | } 240 | }); 241 | return vh; 242 | } 243 | 244 | @Override 245 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, BaseEntity itemData) { 246 | 247 | } 248 | 249 | class ImageViewVH extends RecyclerView.ViewHolder { 250 | ImageView img; 251 | 252 | public ImageViewVH(View itemView) { 253 | super(itemView); 254 | img = (ImageView) itemView.findViewById(R.id.img); 255 | } 256 | } 257 | }; 258 | mIndexStickyView.addIndexFooterAdapter(mFooterBannerAdapter); 259 | 260 | 261 | mAdapter.setOnItemClickListener(this); 262 | mAdapter.setOnItemLongClickListener(this); 263 | } 264 | 265 | private List initDatas() { 266 | 267 | List list = new ArrayList<>(); 268 | // 初始化数据 269 | List contactStrings = Arrays.asList(getResources().getStringArray(R.array.contact_array)); 270 | List mobileStrings = Arrays.asList(getResources().getStringArray(R.array.mobile_array)); 271 | for (int i = 0; i < contactStrings.size(); i++) { 272 | ContactEntity contactEntity = new ContactEntity(contactStrings.get(i), mobileStrings.get(i)); 273 | list.add(contactEntity); 274 | } 275 | return list; 276 | } 277 | 278 | private List initFavDatas() { 279 | List list = new ArrayList<>(); 280 | list.add(new UserEntity("张三", "13298449923")); 281 | list.add(new UserEntity("李四", "13298449923")); 282 | return list; 283 | } 284 | 285 | private List initMenuDatas() { 286 | List list = new ArrayList<>(); 287 | list.add(new MenuEntity("圣诞组", R.drawable.christmas)); 288 | list.add(new MenuEntity("雪人组", R.drawable.snowman)); 289 | list.add(new MenuEntity("天使组", R.drawable.angel)); 290 | list.add(new MenuEntity("铃铛组", R.drawable.bell)); 291 | return list; 292 | } 293 | 294 | class MyIndexStickyViewAdapter extends IndexStickyViewAdapter { 295 | 296 | public MyIndexStickyViewAdapter(List list) { 297 | 298 | super(list); 299 | } 300 | 301 | @Override 302 | public RecyclerView.ViewHolder onCreateIndexViewHolder(ViewGroup parent) { 303 | 304 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_item_index, parent, false); 305 | return new IndexViewHolder(view); 306 | } 307 | 308 | @Override 309 | public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) { 310 | 311 | View view = LayoutInflater.from(mContext).inflate(R.layout.indexsticky_item_contact, parent, false); 312 | return new ContentViewHolder(view); 313 | } 314 | 315 | @Override 316 | public void onBindIndexViewHolder(RecyclerView.ViewHolder holder, int position, String indexName) { 317 | 318 | IndexViewHolder indexViewHolder = (IndexViewHolder) holder; 319 | indexViewHolder.mTextView.setText(indexName); 320 | } 321 | 322 | @Override 323 | public void onBindContentViewHolder(RecyclerView.ViewHolder holder, int position, ContactEntity itemData) { 324 | 325 | ContentViewHolder contentViewHolder = (ContentViewHolder) holder; 326 | contentViewHolder.mMobile.setText(itemData.getMobile()); 327 | contentViewHolder.mName.setText(itemData.getName()); 328 | } 329 | } 330 | 331 | class IndexViewHolder extends RecyclerView.ViewHolder { 332 | TextView mTextView; 333 | 334 | public IndexViewHolder(View itemView) { 335 | 336 | super(itemView); 337 | mTextView = (TextView) itemView.findViewById(R.id.tv_index); 338 | } 339 | } 340 | 341 | class ContentViewHolder extends RecyclerView.ViewHolder { 342 | TextView mName; 343 | TextView mMobile; 344 | ImageView mAvatar; 345 | 346 | public ContentViewHolder(View itemView) { 347 | 348 | super(itemView); 349 | mName = (TextView) itemView.findViewById(R.id.tv_name); 350 | mMobile = (TextView) itemView.findViewById(R.id.tv_mobile); 351 | mAvatar = (ImageView) itemView.findViewById(R.id.img_avatar); 352 | } 353 | } 354 | 355 | @Override 356 | public void onItemClick(View childView, int position, ContactEntity item) { 357 | 358 | Toast.makeText(mContext, "点击" + item.getName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 359 | } 360 | 361 | @Override 362 | public void onItemLongClick(View childView, int position, ContactEntity item) { 363 | 364 | Toast.makeText(mContext, "长按:" + item.getName() + ",位置:" + position, Toast.LENGTH_SHORT).show(); 365 | } 366 | 367 | @Override 368 | public boolean onCreateOptionsMenu(Menu menu) { 369 | 370 | getMenuInflater().inflate(R.menu.toolbar_menu, menu); 371 | return true; 372 | } 373 | 374 | ContactEntity mAdd = new ContactEntity("阿圆add", "15525672987"); 375 | List mAddCollections = new ArrayList<>(); 376 | void initEditData() { 377 | 378 | mAddCollections.add(new ContactEntity("阿圆add Collections1", "15525672987")); 379 | mAddCollections.add(new ContactEntity("阿圆add Collections2", "15525672987")); 380 | } 381 | 382 | 383 | @Override 384 | public boolean onMenuItemClick(MenuItem item) { 385 | 386 | switch (item.getItemId()) { 387 | case R.id.toolbar_add: 388 | mAdapter.add(mAdd); 389 | Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show(); 390 | break; 391 | case R.id.toolbar_add_collections: 392 | mAdapter.add(mAddCollections); 393 | Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show(); 394 | break; 395 | case R.id.toolbar_del: 396 | mAdapter.remove(mAdd); 397 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 398 | break; 399 | case R.id.toolbar_del_collections: 400 | mAdapter.remove(mAddCollections); 401 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 402 | break; 403 | case R.id.toolbar_del_Banner_header: 404 | mAdapter.removeHeader(mBannerAdapter); 405 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 406 | break; 407 | case R.id.toolbar_del_normal_header: 408 | mAdapter.removeHeader(mNormalAdapter); 409 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 410 | break; 411 | case R.id.toolbar_del_all_header: 412 | mAdapter.removeAllHeader(); 413 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 414 | break; 415 | case R.id.toolbar_del_banner_footer: 416 | mAdapter.removeFooter(mFooterBannerAdapter); 417 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 418 | break; 419 | case R.id.toolbar_del_normal_footer: 420 | mAdapter.removeFooter(mFooterAdapter); 421 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 422 | break; 423 | case R.id.toolbar_del_all_footer: 424 | mAdapter.removeAllFooter(); 425 | Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); 426 | break; 427 | case R.id.toolbar_clear: 428 | mAdapter.clear(); 429 | Toast.makeText(mContext, "清除成功", Toast.LENGTH_SHORT).show(); 430 | break; 431 | case R.id.toolbar_reset: 432 | mAdapter.reset(initDatas()); 433 | Toast.makeText(mContext, "重置成功", Toast.LENGTH_SHORT).show(); 434 | break; 435 | default: 436 | break; 437 | } 438 | return true; 439 | } 440 | } 441 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/IndexStickyViewActivity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo; 2 | 3 | import butterknife.BindView; 4 | import butterknife.ButterKnife; 5 | import butterknife.OnClick; 6 | 7 | import android.content.Intent; 8 | import android.os.Bundle; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.support.v7.widget.Toolbar; 11 | import android.view.View; 12 | 13 | public class IndexStickyViewActivity extends AppCompatActivity { 14 | 15 | @BindView(R.id.toolbar) 16 | Toolbar mToolbar; 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_index_sticky_view); 23 | ButterKnife.bind(this); 24 | setSupportActionBar(mToolbar); 25 | } 26 | 27 | @OnClick({R.id.btnContact, R.id.btnCity}) 28 | public void onClick(View view) { 29 | if(view.getId() == R.id.btnCity) { 30 | startActivity(new Intent(this, CityActivity.class)); 31 | } else { 32 | startActivity(new Intent(this, ContactActivity.class)); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/IndexStickyViewDecoration.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo; 2 | 3 | import cn.ittiger.indexlist.ItemType; 4 | import cn.ittiger.indexlist.adapter.IndexStickyViewAdapter; 5 | import cn.ittiger.indexlist.entity.IndexStickyEntity; 6 | 7 | import android.content.Context; 8 | import android.content.res.TypedArray; 9 | import android.graphics.Canvas; 10 | import android.graphics.Rect; 11 | import android.graphics.drawable.ColorDrawable; 12 | import android.graphics.drawable.Drawable; 13 | import android.support.v7.widget.RecyclerView; 14 | import android.view.View; 15 | 16 | /** 17 | * Created by ylhu on 16-12-22. 18 | */ 19 | public class IndexStickyViewDecoration extends RecyclerView.ItemDecoration { 20 | 21 | private final Drawable mDivider; 22 | private final int mSize; 23 | private final int mLeftMargin; 24 | private final int mRightMargin; 25 | 26 | public IndexStickyViewDecoration(Context context) { 27 | 28 | mDivider = new ColorDrawable(context.getResources().getColor(R.color.divider_color)); 29 | mSize = context.getResources().getDimensionPixelSize(R.dimen.global_divider_size); 30 | mLeftMargin = context.getResources().getDimensionPixelSize(R.dimen.contact_item_left_margin); 31 | mRightMargin = context.getResources().getDimensionPixelSize(R.dimen.contact_item_right_margin); 32 | } 33 | 34 | @Override 35 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 36 | 37 | int position = parent.getChildAdapterPosition(view); 38 | IndexStickyViewAdapter adapter = (IndexStickyViewAdapter)parent.getAdapter(); 39 | IndexStickyEntity entity = adapter.getItem(position); 40 | if(entity.getItemType() != ItemType.ITEM_TYPE_INDEX && position < (adapter.getItemCount() - 1) 41 | && adapter.getItem(position + 1).getItemType() != ItemType.ITEM_TYPE_INDEX) { 42 | outRect.set(0, 0, 0, mSize); 43 | } 44 | } 45 | 46 | @Override 47 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 48 | 49 | super.onDraw(c, parent, state); 50 | int top; 51 | int bottom; 52 | int left = parent.getPaddingLeft() + mLeftMargin; 53 | int right = parent.getWidth() - parent.getPaddingRight() - mRightMargin; 54 | final int childCount = parent.getChildCount(); 55 | IndexStickyViewAdapter adapter = (IndexStickyViewAdapter)parent.getAdapter(); 56 | IndexStickyEntity entity; 57 | for (int i = 0; i < childCount; i++) { 58 | final View child = parent.getChildAt(i); 59 | int position = parent.getChildAdapterPosition(child); 60 | entity = adapter.getItem(position); 61 | if(entity.getItemType() != ItemType.ITEM_TYPE_INDEX && 62 | position < (adapter.getItemCount() - 1) && 63 | adapter.getItem(position + 1).getItemType() != ItemType.ITEM_TYPE_INDEX) { 64 | //获得child的布局信息 65 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams(); 66 | top = child.getBottom() + params.bottomMargin; 67 | bottom = top + mSize; 68 | mDivider.setBounds(left, top, right, bottom); 69 | mDivider.draw(c); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/entity/CityEntity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo.entity; 2 | 3 | import cn.ittiger.indexlist.entity.BaseEntity; 4 | 5 | /** 6 | * @author: laohu on 2016/12/25 7 | * @site: http://ittiger.cn 8 | */ 9 | 10 | public class CityEntity implements BaseEntity { 11 | private String mCityName; 12 | 13 | public CityEntity(String cityName) { 14 | 15 | mCityName = cityName; 16 | } 17 | 18 | @Override 19 | public String getIndexField() { 20 | 21 | return mCityName; 22 | } 23 | 24 | public String getCityName() { 25 | 26 | return mCityName; 27 | } 28 | 29 | public void setCityName(String cityName) { 30 | 31 | mCityName = cityName; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/entity/ContactEntity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo.entity; 2 | 3 | import cn.ittiger.indexlist.entity.BaseEntity; 4 | 5 | /** 6 | * @author: laohu on 2016/12/17 7 | * @site: http://ittiger.cn 8 | */ 9 | 10 | public class ContactEntity implements BaseEntity { 11 | 12 | private String name; 13 | private String avatar; 14 | private String mobile; 15 | 16 | public ContactEntity(String name, String mobile) { 17 | this.name = name; 18 | this.mobile = mobile; 19 | } 20 | 21 | 22 | @Override 23 | public String getIndexField() { 24 | 25 | return name; 26 | } 27 | 28 | public String getName() { 29 | 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | 35 | this.name = name; 36 | } 37 | 38 | public String getAvatar() { 39 | 40 | return avatar; 41 | } 42 | 43 | public void setAvatar(String avatar) { 44 | 45 | this.avatar = avatar; 46 | } 47 | 48 | public String getMobile() { 49 | 50 | return mobile; 51 | } 52 | 53 | public void setMobile(String mobile) { 54 | 55 | this.mobile = mobile; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/entity/MenuEntity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo.entity; 2 | 3 | import cn.ittiger.indexlist.entity.BaseEntity; 4 | 5 | /** 6 | * Created by ylhu on 16-12-21. 7 | */ 8 | public class MenuEntity implements BaseEntity { 9 | private long menuId; 10 | private String menuTitle; 11 | private int menuIconRes; 12 | 13 | public MenuEntity(String title, int iconRes) { 14 | 15 | this.menuTitle = title; 16 | this.menuIconRes = iconRes; 17 | } 18 | 19 | public long getMenuId() { 20 | 21 | return menuId; 22 | } 23 | 24 | public void setMenuId(long menuId) { 25 | 26 | this.menuId = menuId; 27 | } 28 | 29 | public String getMenuTitle() { 30 | 31 | return menuTitle; 32 | } 33 | 34 | public void setMenuTitle(String menuTitle) { 35 | 36 | this.menuTitle = menuTitle; 37 | } 38 | 39 | public int getMenuIconRes() { 40 | 41 | return menuIconRes; 42 | } 43 | 44 | public void setMenuIconRes(int menuIconRes) { 45 | 46 | this.menuIconRes = menuIconRes; 47 | } 48 | 49 | @Override 50 | public String getIndexField() { 51 | 52 | return menuTitle; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/cn/ittiger/indexlist/demo/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package cn.ittiger.indexlist.demo.entity; 2 | 3 | import cn.ittiger.indexlist.entity.BaseEntity; 4 | 5 | /** 6 | * Created by ylhu on 16-12-21. 7 | */ 8 | public class UserEntity implements BaseEntity { 9 | 10 | private String name; 11 | private String avatar; 12 | private String mobile; 13 | 14 | public UserEntity(String name, String mobile) { 15 | this.name = name; 16 | this.mobile = mobile; 17 | } 18 | 19 | 20 | @Override 21 | public String getIndexField() { 22 | 23 | return name; 24 | } 25 | 26 | public String getName() { 27 | 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | 33 | this.name = name; 34 | } 35 | 36 | public String getAvatar() { 37 | 38 | return avatar; 39 | } 40 | 41 | public void setAvatar(String avatar) { 42 | 43 | this.avatar = avatar; 44 | } 45 | 46 | public String getMobile() { 47 | 48 | return mobile; 49 | } 50 | 51 | public void setMobile(String mobile) { 52 | 53 | this.mobile = mobile; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/angel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyongli/IndexStickyView/0998b9cb677a17b5044b5de3b258bbf562936120/app/src/main/res/drawable-xxhdpi/angel.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/banner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyongli/IndexStickyView/0998b9cb677a17b5044b5de3b258bbf562936120/app/src/main/res/drawable-xxhdpi/banner.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/bell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyongli/IndexStickyView/0998b9cb677a17b5044b5de3b258bbf562936120/app/src/main/res/drawable-xxhdpi/bell.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/christmas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyongli/IndexStickyView/0998b9cb677a17b5044b5de3b258bbf562936120/app/src/main/res/drawable-xxhdpi/christmas.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/snowman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huyongli/IndexStickyView/0998b9cb677a17b5044b5de3b258bbf562936120/app/src/main/res/drawable-xxhdpi/snowman.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_toolbar_add.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/vector_add.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/vector_add_focus.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/vector_contact_focus.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/vector_del.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_city.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 25 | 26 | 27 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_contact.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 25 | 26 | 27 | 31 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_index_sticky_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 25 | 26 | 27 | 31 |