├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── jarRepositories.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── ocnyang │ │ └── recyclerviewevent │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── ocnyang │ │ │ └── recyclerviewevent │ │ │ ├── DataManager.java │ │ │ ├── MainActivity.java │ │ │ ├── concat_adapter │ │ │ ├── ConcatAdapterActivity.kt │ │ │ └── ConcatPageAdapter.kt │ │ │ ├── diff_util │ │ │ ├── AdapterDiffCallback.kt │ │ │ ├── DiffUtilActivity.kt │ │ │ └── DiffUtilDataManager.kt │ │ │ ├── notify_item │ │ │ ├── NotifyItemActivity.kt │ │ │ └── NotifyItemDataManager.kt │ │ │ ├── paging3 │ │ │ ├── FruitPagingSource.kt │ │ │ ├── FruitRepository.kt │ │ │ ├── PagingActivity.kt │ │ │ ├── PagingAdapter.kt │ │ │ ├── PagingAdapterDiffCallback.kt │ │ │ ├── PagingFruitBean.kt │ │ │ ├── PagingLoadStateAdapter.kt │ │ │ ├── PagingViewModel.kt │ │ │ └── PagingViewModelFactory.kt │ │ │ ├── reuse_disorder │ │ │ ├── EleganceMethodActivity.kt │ │ │ └── FruitBean.kt │ │ │ └── swipe_and_drag │ │ │ ├── GridViewActivity.java │ │ │ ├── ListViewActivity.java │ │ │ ├── OnRecyclerItemClickListener.java │ │ │ ├── RecyAdapter.java │ │ │ ├── RecyItemTouchHelperCallback.java │ │ │ └── recyembellish │ │ │ ├── DividerGridItemDecoration.java │ │ │ └── DividerListItemDecoration.java │ └── res │ │ ├── drawable-v21 │ │ └── shape_bg_item.xml │ │ ├── drawable │ │ ├── ic_fruit_icons_01.xml │ │ ├── ic_fruit_icons_02.xml │ │ ├── ic_fruit_icons_03.xml │ │ ├── ic_fruit_icons_04.xml │ │ ├── ic_fruit_icons_05.xml │ │ ├── ic_fruit_icons_06.xml │ │ ├── ic_iphone.xml │ │ └── shape_bg_item.xml │ │ ├── layout │ │ ├── activity_concat_adapter.xml │ │ ├── activity_concat_item_header.xml │ │ ├── activity_diff_util.xml │ │ ├── activity_diff_util_item.xml │ │ ├── activity_elegance_method.xml │ │ ├── activity_elegance_method_item.xml │ │ ├── activity_main.xml │ │ ├── activity_notify_item.xml │ │ ├── activity_notify_item_item.xml │ │ ├── activity_paging.xml │ │ ├── activity_paging_item_loadstate.xml │ │ ├── activity_recyclerview.xml │ │ ├── item_gridview.xml │ │ └── item_listview.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── ocnyang │ └── recyclerviewevent │ └── ExampleUnitTest.java ├── build.gradle ├── docs ├── ConcatAdapter.gif ├── DiffUtil.gif ├── Paging3.gif ├── PointRefresh.gif ├── concatadapter.jpg └── recyclervieweventreuse_disorder.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── 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 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | 24 | 25 | 47 | 67 | 68 | 69 | 70 | 71 | 72 | 74 | 75 | 76 | 77 | 78 | 1.8 79 | 80 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.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 | # RecyclerViewEvent 2 | 3 | 本项目展示内容: 4 | - [x] 点击事件、长按事件 5 | - [x] 分割线 6 | - [x] 拖曳排序、侧滑删除 7 | - [x] 拖曳排序 & 首个固定 8 | - [x] 复用错乱(复选框、编辑框) 9 | - [x] 定向刷新 10 | - [x] DiffUtil 的使用 11 | - [x] Paging 3 的使用 12 | - [ ] DiffUtil & 定向刷新 结合使用 13 | - [ ] 条目长按的上下文菜单按钮 14 | - [x] ConcatAdapter (旧名:MergeAdapter) 15 | 16 |


17 | 18 | ## 效果图   19 | 20 | 1. swipe and drag.拖曳 + 滑动删除 21 | 2. drag. 拖曳 + 首个 item 固定 22 | 23 | PointRefreshPointRefresh 24 | 25 | ## 1. 基础讲解 26 | 27 | 这次主要是把 RecyclerView 比较常用的基本的点,在这里集中整理一下。从这篇文章主要梳理以下几点: 28 | 29 | * 优雅的实现:item 点击事件 & item 长点击事件 30 | * RecyclerView 添加 divider 的标准姿势 31 | * RecyclerView 实现 item 的拖曳排序和滑动删除 32 | * 拖曳排序时,限制首个 item 固定的实现 33 | 34 | 详解介绍请看本项目对应的讲解文章: 35 | **[RecyclerView 梳理:点击&长按事件、分割线、拖曳排序、滑动删除](https://juejin.im/post/5a320ffcf265da43200342a3)** 36 | 37 | 38 | ## 2. 解决 RecyclerView 复用错乱 之 优雅方式 39 | 40 | 当 RcyclerView 中存在 CheckBox 或 EditText 时,因为复用机制的存在,会在滚动时造成数据混乱。 41 | 虽然网上流传的有多种方法来解决,但都比较繁琐或存在一定缺陷。 42 | 这里给大家提供一种比较优雅而合理的方式来解决复用错乱的问题: 43 | 44 | [![reuse disorder](https://github.com/OCNYang/RecyclerViewEvent/blob/master/docs/recyclervieweventreuse_disorder.gif?raw=true)](https://cdn.jsdelivr.net/gh/ocnyang/gallery@master/github/recyclervieweventreuse_disorder.gif) 45 | [图片显示失败?点击查看](https://cdn.jsdelivr.net/gh/ocnyang/gallery@master/github/recyclervieweventreuse_disorder.gif) 46 | 47 | 方法详情和讲解请看[源码](https://github.com/OCNYang/RecyclerViewEvent/blob/master/app/src/main/java/com/ocnyang/recyclerviewevent/reuse_disorder/EleganceMethodActivity.kt) 48 | 49 | ## 3. 使用 `notifyItemChanged(int position, @Nullable Object payload)` 定向刷新 50 | 51 | PointRefresh 52 | 53 | > 仔细观察效果图,会发现:非定向刷新时条目会有闪动;定向刷新不会有闪动 54 | 55 | 具体查看源码说明:[NotifyItemAdapter.onBindViewHolder(holder: NotifyItemViewHolder, position: Int, payloads: MutableList)](https://github.com/OCNYang/RecyclerViewEvent/blob/master/app/src/main/java/com/ocnyang/recyclerviewevent/notify_item/NotifyItemActivity.kt) 56 | 57 | ## 4. 使用 DiffUtil 实现局部刷新 58 | DiffUtil 59 | 60 | 具体查看源码说明:[Activity](https://github.com/OCNYang/RecyclerViewEvent/blob/master/app/src/main/java/com/ocnyang/recyclerviewevent/diff_util/DiffUtilActivity.kt) 61 | [AdapterDiffCallback](https://github.com/OCNYang/RecyclerViewEvent/blob/master/app/src/main/java/com/ocnyang/recyclerviewevent/diff_util/AdapterDiffCallback.kt) 62 | 63 | ## 5. 使用 Paging 3 分页加载 64 | Paging3 65 | 66 | 具体查看源码说明:[paging3 相关代码](https://github.com/OCNYang/RecyclerViewEvent/blob/master/app/src/main/java/com/ocnyang/recyclerviewevent/paging3) 67 | 68 | ## 6. ConcatAdapter 实现多个不同的 Adapter 合并成一个 69 | 70 | ConcatAdapter 71 | 72 | 使用方式: 73 | ```java 74 | recyclerView.adapter = ConcatAdapter(mHeaderAdapter, mAdapter, mFooterAdapter) 75 | ``` 76 | 77 | * 可以使不同的 Adapter (不同数据、不同布局) 合并成一个 Adapter 设置给 RecyclerView 显示; 78 | * 功能有点类似多 ItemViewType 的 Adapter,不同的是每个 Adapter 是从上到下按顺序显示的,不能混排 79 | * 您可能使用过 `ViewHolder.getAdapterPosition` 来获得 Adapter 中某个 ViewHolder 的位置。现在,因为我们合并了多个 Adapter,作为代替,您需要调用 `ViewHolder.getBindingAdapterPosition ()`。 80 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion 29 6 | defaultConfig { 7 | applicationId "com.ocnyang.recyclerviewevent" 8 | minSdkVersion 19 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | defaultConfig { 21 | vectorDrawables.useSupportLibrary = true 22 | } 23 | compileOptions { 24 | sourceCompatibility JavaVersion.VERSION_1_8 25 | targetCompatibility JavaVersion.VERSION_1_8 26 | } 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(include: ['*.jar'], dir: 'libs') 31 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 32 | implementation 'com.google.android.material:material:1.3.0' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 35 | implementation 'androidx.appcompat:appcompat:1.3.0' 36 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 37 | implementation 'androidx.recyclerview:recyclerview:1.2.1' 38 | testImplementation 'junit:junit:4.13.1' 39 | 40 | def paging_version = "3.0.0" 41 | implementation "androidx.paging:paging-runtime-ktx:$paging_version" 42 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3' 43 | } 44 | -------------------------------------------------------------------------------- /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 F:\OCNApp\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/ocnyang/recyclerviewevent/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.ocnyang.recyclerviewevent", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 25 | 29 | 33 | 37 | 42 | 47 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/DataManager.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /******************************************************************* 8 | * * * * * * * * * * * Created by OCN.Yang 9 | * * * * * * * Time:2017/8/2 10:07. 10 | * * * * * * * Email address:ocnyang@gmail.com 11 | * * * * * * * * * * *.Yang Web site:www.ocnyang.com 12 | *******************************************************************/ 13 | 14 | 15 | public class DataManager { 16 | private static List sStringList = Arrays.asList("香蕉", "苹果", "草莓", "橙子", 17 | "柠檬", "梨", "樱桃", "哈密瓜", "猕猴桃", "葡萄"); 18 | 19 | public static final List getData(int number) { 20 | List stringList = new ArrayList<>(); 21 | for (int i = 0; i < number; i++) { 22 | stringList.add(sStringList.get(i % sStringList.size()) + " ?"); 23 | } 24 | return stringList; 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | import androidx.appcompat.app.AppCompatActivity; 8 | 9 | import com.ocnyang.recyclerviewevent.concat_adapter.ConcatAdapterActivity; 10 | import com.ocnyang.recyclerviewevent.diff_util.DiffUtilActivity; 11 | import com.ocnyang.recyclerviewevent.notify_item.NotifyItemActivity; 12 | import com.ocnyang.recyclerviewevent.paging3.PagingActivity; 13 | import com.ocnyang.recyclerviewevent.reuse_disorder.EleganceMethodActivity; 14 | import com.ocnyang.recyclerviewevent.swipe_and_drag.GridViewActivity; 15 | import com.ocnyang.recyclerviewevent.swipe_and_drag.ListViewActivity; 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_main); 23 | } 24 | 25 | public void showListView(View view) { 26 | startActivity(new Intent(this, ListViewActivity.class)); 27 | } 28 | 29 | public void showGridView(View view) { 30 | startActivity(new Intent(this, GridViewActivity.class)); 31 | } 32 | 33 | public void skipToEleganceMethod(View view) { 34 | startActivity(new Intent(this, EleganceMethodActivity.class)); 35 | } 36 | 37 | public void skipToDiffUtil(View view) { 38 | startActivity(new Intent(this, DiffUtilActivity.class)); 39 | } 40 | 41 | public void skipToNotifyItem(View view) { 42 | startActivity(new Intent(this, NotifyItemActivity.class)); 43 | } 44 | 45 | public void skipToPaging3(View view) { 46 | startActivity(new Intent(this, PagingActivity.class)); 47 | } 48 | 49 | public void skipToConcatAdapter(View view) { 50 | startActivity(new Intent(this, ConcatAdapterActivity.class)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/concat_adapter/ConcatAdapterActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.concat_adapter 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import android.os.Handler 6 | import androidx.core.os.HandlerCompat 7 | import androidx.paging.LoadState 8 | import androidx.recyclerview.widget.ConcatAdapter 9 | import androidx.recyclerview.widget.LinearLayoutManager 10 | import androidx.recyclerview.widget.RecyclerView 11 | import com.ocnyang.recyclerviewevent.R 12 | import com.ocnyang.recyclerviewevent.diff_util.DiffUtilAdapter 13 | import com.ocnyang.recyclerviewevent.diff_util.DiffUtilDataManager 14 | 15 | class ConcatAdapterActivity : AppCompatActivity() { 16 | private lateinit var mAdapter: ConcatPageAdapter 17 | private lateinit var mHeaderAdapter: HeaderAdapter 18 | private lateinit var mFooterAdapter: FooterAdapter 19 | private var lastVisibleItemPosition: Int = 0 20 | private val mHandler by lazy { HandlerCompat.createAsync(this.mainLooper) } 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setContentView(R.layout.activity_concat_adapter) 25 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) 26 | 27 | mAdapter = ConcatPageAdapter(this, DiffUtilDataManager.getRandomData((16..19).random())) 28 | mHeaderAdapter = HeaderAdapter(this, DiffUtilDataManager.getRandomData((1..3).random())) 29 | mFooterAdapter = FooterAdapter(this) 30 | mFooterAdapter.loadState = LoadState.NotLoading(false) 31 | 32 | findViewById(R.id.recyclerView).apply { 33 | layoutManager = LinearLayoutManager(this@ConcatAdapterActivity) 34 | adapter = ConcatAdapter(mHeaderAdapter, mAdapter, mFooterAdapter) 35 | 36 | addOnScrollListener(object : RecyclerView.OnScrollListener() { 37 | override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { 38 | if (RecyclerView.SCROLL_STATE_IDLE == newState) { 39 | var childCount = (layoutManager as LinearLayoutManager).childCount 40 | var itemCount = (layoutManager as LinearLayoutManager).itemCount 41 | if (!checkIsLoadEnd()) { 42 | if (lastVisibleItemPosition == itemCount - 1) { 43 | loadMoreData() 44 | } 45 | } else { 46 | mFooterAdapter.loadState = LoadState.Error(Throwable()) 47 | } 48 | } 49 | } 50 | 51 | override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { 52 | super.onScrolled(recyclerView, dx, dy) 53 | lastVisibleItemPosition = (layoutManager as LinearLayoutManager).findLastVisibleItemPosition() 54 | } 55 | }) 56 | } 57 | } 58 | 59 | fun checkIsLoadEnd(): Boolean = mAdapter.data.size > 38 60 | fun loadMoreData() { 61 | mFooterAdapter.loadState = LoadState.Loading 62 | mHandler.postDelayed({ 63 | mAdapter.data = mAdapter.data.apply { 64 | addAll(DiffUtilDataManager.getRandomData((6..8).random())) 65 | } 66 | mAdapter.notifyDataSetChanged() 67 | mFooterAdapter.loadState = LoadState.NotLoading(false) 68 | }, 1500) 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/concat_adapter/ConcatPageAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.concat_adapter 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.ProgressBar 8 | import androidx.paging.LoadState 9 | import com.ocnyang.recyclerviewevent.R 10 | import com.ocnyang.recyclerviewevent.diff_util.DiffUtilAdapter 11 | import com.ocnyang.recyclerviewevent.diff_util.DiffUtilFruitBean 12 | import com.ocnyang.recyclerviewevent.diff_util.DiffUtilViewHolder 13 | import com.ocnyang.recyclerviewevent.paging3.LoadStateViewHolder 14 | import com.ocnyang.recyclerviewevent.paging3.PagingLoadStateAdapter 15 | 16 | class ConcatPageAdapter(context: Context, data: MutableList) : DiffUtilAdapter(context, data) 17 | 18 | class HeaderAdapter(context: Context, data: MutableList) : DiffUtilAdapter(context, data) { 19 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DiffUtilViewHolder { 20 | val itemView = 21 | LayoutInflater.from(context).inflate(R.layout.activity_concat_item_header, parent, false) 22 | return DiffUtilViewHolder(itemView) 23 | } 24 | } 25 | 26 | class FooterAdapter(context: Context) : PagingLoadStateAdapter(context) { 27 | override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) { 28 | holder.tvLoadState.text = when (loadState) { 29 | is LoadState.Loading -> { 30 | holder.progressBar.visibility = View.VISIBLE 31 | "正在加载" 32 | } 33 | is LoadState.Error -> { 34 | holder.progressBar.visibility = View.GONE 35 | "没有更多数据" 36 | } 37 | is LoadState.NotLoading -> { 38 | holder.progressBar.visibility = View.GONE 39 | "加载完成" 40 | } 41 | } 42 | } 43 | 44 | override fun displayLoadStateAsItem(loadState: LoadState): Boolean { 45 | return loadState is LoadState.NotLoading || super.displayLoadStateAsItem(loadState) 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/diff_util/AdapterDiffCallback.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.diff_util 2 | 3 | import androidx.recyclerview.widget.DiffUtil 4 | 5 | /******************************************************************* 6 | * * * * * * * * * * * @Author: OCN.Yang 7 | * * * * * * * @CreateDate: 2021/4/10 10:29 AM. 8 | * * * * * * * @Email: ocnyang@gmail.com 9 | * * * * * * * * * * *.Yang @GitHub: https://github.com/OCNYang 10 | *******************************************************************/ 11 | 12 | class AdapterDiffCallback( 13 | val oldData: List, 14 | val newData: List 15 | ) : DiffUtil.Callback() { 16 | 17 | override fun getOldListSize(): Int = oldData.size 18 | 19 | override fun getNewListSize(): Int = newData.size 20 | 21 | /** 22 | * 判断两个 item 是不是同一种类型,因为 RecyclerView 支持不同的条目布局 23 | * 这里的 Demo 因为比较简单不涉及多类型条目,所以直接比较两种数据类型(实际这里每次返回的都是 true) 24 | */ 25 | override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) = 26 | oldData[oldItemPosition].javaClass == newData[newItemPosition].javaClass 27 | 28 | /** 29 | * 判断两个 item 的数据内容是否相同,如果 [areItemsTheSame] 判断已经不相同了,就不会判断此方法了 30 | * 这里的 Demo 直接判断两条数据内容是否相同。 31 | * ps: 因为这里的 [DiffUtilFruitBean] 采用的是 Kotlin 的数据类,所以可以直接用 == 判断 32 | */ 33 | override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) = 34 | oldData[oldItemPosition] == newData[newItemPosition] 35 | 36 | /** 37 | * 判断 item 中具体哪些内容改变了,可以定向刷新。 38 | */ 39 | override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? { 40 | return super.getChangePayload(oldItemPosition, newItemPosition) 41 | } 42 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/diff_util/DiffUtilActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.diff_util 2 | 3 | import android.content.Context 4 | import android.os.Bundle 5 | import android.view.LayoutInflater 6 | import android.view.MenuItem 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import android.widget.ImageView 10 | import android.widget.TextView 11 | import androidx.appcompat.app.AppCompatActivity 12 | import androidx.recyclerview.widget.DiffUtil 13 | import androidx.recyclerview.widget.LinearLayoutManager 14 | import androidx.recyclerview.widget.RecyclerView 15 | import com.ocnyang.recyclerviewevent.R 16 | 17 | class DiffUtilActivity : AppCompatActivity() { 18 | private lateinit var mAdapter: DiffUtilAdapter 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_diff_util) 22 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) 23 | 24 | findViewById(R.id.recyclerView).apply { 25 | this.layoutManager = LinearLayoutManager(this@DiffUtilActivity) 26 | mAdapter = DiffUtilAdapter( 27 | this@DiffUtilActivity, 28 | DiffUtilDataManager.getRandomData((6..9).random()) 29 | ) 30 | this.adapter = mAdapter 31 | } 32 | 33 | findViewById(R.id.btn_change).setOnClickListener { 34 | // 生成新的列表数据 35 | // 这里的数据为了达到部分相同部分变化,数据取自相同的数据源集合 36 | val randomData = DiffUtilDataManager.getRandomData((6..9).random()) 37 | 38 | /** 39 | * 通过 [AdapterDiffCallback] 比较新老数据的差别 40 | */ 41 | val diffResult = DiffUtil.calculateDiff( 42 | AdapterDiffCallback( 43 | oldData = mAdapter.data, 44 | newData = randomData 45 | ) 46 | ) 47 | // 将差别结果作用于列表的 Adapter 以刷新数据显示 48 | diffResult.dispatchUpdatesTo(mAdapter) 49 | } 50 | } 51 | 52 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 53 | if (item.itemId == android.R.id.home) { 54 | finish() 55 | } 56 | return super.onOptionsItemSelected(item) 57 | } 58 | } 59 | 60 | open class DiffUtilAdapter(var context: Context, var data: MutableList) : 61 | RecyclerView.Adapter() { 62 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DiffUtilViewHolder { 63 | val itemView = 64 | LayoutInflater.from(context).inflate(R.layout.activity_diff_util_item, parent, false) 65 | return DiffUtilViewHolder(itemView) 66 | } 67 | 68 | override fun onBindViewHolder(holder: DiffUtilViewHolder, position: Int) { 69 | holder.ivImg.setImageResource(data[position].imgId) 70 | holder.tvTitle.text = data[position].name 71 | } 72 | 73 | override fun getItemCount(): Int = data.size 74 | } 75 | 76 | class DiffUtilViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 77 | var ivImg: ImageView = itemView.findViewById(R.id.iv_img) 78 | var tvTitle: TextView = itemView.findViewById(R.id.tv_title) 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/diff_util/DiffUtilDataManager.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.diff_util 2 | 3 | import com.ocnyang.recyclerviewevent.R 4 | 5 | /******************************************************************* 6 | * * * * * * * * * * * @Author: OCN.Yang 7 | * * * * * * * @CreateDate: 2021/4/10 9:59 AM. 8 | * * * * * * * @Email: ocnyang@gmail.com 9 | * * * * * * * * * * *.Yang @GitHub: https://github.com/OCNYang 10 | *******************************************************************/ 11 | 12 | class DiffUtilDataManager { 13 | companion object { 14 | @JvmStatic 15 | val dataList = listOf( 16 | DiffUtilFruitBean("香蕉?", R.drawable.ic_fruit_icons_01), 17 | DiffUtilFruitBean("苹果?", R.drawable.ic_fruit_icons_02), 18 | DiffUtilFruitBean("草莓?", R.drawable.ic_fruit_icons_03), 19 | DiffUtilFruitBean("橙子?", R.drawable.ic_fruit_icons_04), 20 | DiffUtilFruitBean("柠檬?", R.drawable.ic_fruit_icons_05), 21 | DiffUtilFruitBean("梨?", R.drawable.ic_fruit_icons_06), 22 | DiffUtilFruitBean("樱桃?", R.drawable.ic_fruit_icons_01), 23 | DiffUtilFruitBean("哈密瓜?", R.drawable.ic_fruit_icons_02), 24 | DiffUtilFruitBean("猕猴桃?", R.drawable.ic_fruit_icons_03), 25 | DiffUtilFruitBean("葡萄?", R.drawable.ic_fruit_icons_04), 26 | ) 27 | 28 | @JvmStatic 29 | fun getRandomData(number: Int): MutableList { 30 | var randomStartPosition = dataList.indices.random() 31 | var num = number 32 | val list = mutableListOf() 33 | while (num >= 0) { 34 | list.add(dataList[randomStartPosition]) 35 | randomStartPosition = ((randomStartPosition + 1) % (dataList.size - 1)) 36 | num-- 37 | } 38 | return list 39 | } 40 | } 41 | } 42 | 43 | 44 | data class DiffUtilFruitBean(var name: String, var imgId: Int) -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/notify_item/NotifyItemActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.notify_item 2 | 3 | import android.content.Context 4 | import androidx.appcompat.app.AppCompatActivity 5 | import android.os.Bundle 6 | import android.view.LayoutInflater 7 | import android.view.MenuItem 8 | import android.view.View 9 | import android.view.ViewGroup 10 | import android.widget.CheckBox 11 | import android.widget.ImageView 12 | import android.widget.TextView 13 | import androidx.recyclerview.widget.LinearLayoutManager 14 | import androidx.recyclerview.widget.RecyclerView 15 | import com.ocnyang.recyclerviewevent.R 16 | 17 | class NotifyItemActivity : AppCompatActivity() { 18 | private lateinit var mAdapter: NotifyItemAdapter 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_notify_item) 22 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) 23 | 24 | findViewById(R.id.recyclerView).apply { 25 | this.layoutManager = LinearLayoutManager(this@NotifyItemActivity) 26 | mAdapter = NotifyItemAdapter( 27 | this@NotifyItemActivity, 28 | NotifyItemDataManager.getData() 29 | ) 30 | this.adapter = mAdapter 31 | } 32 | 33 | // 刷新所有条目的选中状态,(注意观察条目刷新时,整个条目的闪动) 34 | findViewById(R.id.cb_check_all_left).setOnCheckedChangeListener { _, isChecked -> 35 | mAdapter.apply { 36 | data.forEachIndexed { index, bean -> 37 | bean.checked = isChecked 38 | notifyItemChanged(index) 39 | } 40 | } 41 | } 42 | 43 | // 刷新所有条目的选中状态,(此时其实只是刷新条目的复选框的状态,并不会重绘整个条目,所以不会有条目闪动) 44 | findViewById(R.id.cb_check_all_right).setOnCheckedChangeListener { _, isChecked -> 45 | mAdapter.apply { 46 | data.forEachIndexed { index, bean -> 47 | bean.checked = isChecked 48 | notifyItemChanged(index, NotifyItemAdapter.PAYLOAD_CHECKBOX) 49 | } 50 | } 51 | } 52 | 53 | // 交换条目1和条目2的图片并刷新这两个条目 54 | findViewById(R.id.btn_change_item1_left).setOnClickListener { 55 | mAdapter.apply { 56 | // 交换条目1、条目2数据中的图片资源 57 | data[0].imgId = data[1].imgId.also { 58 | data[1].imgId = data[0].imgId 59 | } 60 | notifyItemRangeChanged(0, 2) 61 | } 62 | } 63 | 64 | // 交换条目1和条目2的图片并刷新这两个条目 (此时只是重新加载两个条目的图片,不会重绘其他控件,不会闪动) 65 | findViewById(R.id.btn_change_item1_right).setOnClickListener { 66 | mAdapter.apply { 67 | data[0].imgId = data[1].imgId.also { 68 | data[1].imgId = data[0].imgId 69 | } 70 | notifyItemRangeChanged(0, 2, NotifyItemAdapter.PAYLOAD_IMAGE) 71 | } 72 | } 73 | } 74 | 75 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 76 | if (item.itemId == android.R.id.home) { 77 | finish() 78 | } 79 | return super.onOptionsItemSelected(item) 80 | } 81 | } 82 | 83 | 84 | class NotifyItemAdapter(var context: Context, var data: MutableList) : 85 | RecyclerView.Adapter() { 86 | companion object { 87 | @JvmStatic 88 | val PAYLOAD_CHECKBOX = 0x00000001 89 | 90 | @JvmStatic 91 | val PAYLOAD_IMAGE = 0x00000010 92 | } 93 | 94 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotifyItemViewHolder = 95 | NotifyItemViewHolder( 96 | LayoutInflater.from(context).inflate(R.layout.activity_notify_item_item, parent, false) 97 | ) 98 | 99 | 100 | override fun onBindViewHolder(holder: NotifyItemViewHolder, position: Int) { 101 | holder.ivImg.setImageResource(data[position].imgId) 102 | holder.tvTitle.text = data[position].name 103 | holder.cb.setOnCheckedChangeListener(null) 104 | holder.cb.isChecked = data[position].checked 105 | holder.cb.setOnCheckedChangeListener { _, isChecked -> 106 | data[position].checked = isChecked 107 | } 108 | } 109 | 110 | /** 111 | * 当使用 [RecyclerView.Adapter.notifyItemChanged] 两个参数的方法 刷新条目时会走此方法刷新布局 112 | */ 113 | override fun onBindViewHolder( 114 | holder: NotifyItemViewHolder, position: Int, payloads: MutableList 115 | ) { 116 | if (payloads.isEmpty()) { 117 | onBindViewHolder(holder, position) 118 | } else { 119 | when (payloads[0]) { 120 | PAYLOAD_CHECKBOX -> { 121 | val checked = data[position].checked 122 | if (checked != holder.cb.isChecked) { 123 | holder.cb.setOnCheckedChangeListener(null) 124 | holder.cb.isChecked = checked 125 | holder.cb.setOnCheckedChangeListener { _, isChecked -> 126 | data[position].checked = isChecked 127 | } 128 | } 129 | } 130 | PAYLOAD_IMAGE -> holder.ivImg.setImageResource(data[position].imgId) 131 | } 132 | } 133 | } 134 | 135 | override fun getItemCount(): Int = data.size 136 | } 137 | 138 | class NotifyItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 139 | var ivImg: ImageView = itemView.findViewById(R.id.iv_img) 140 | var tvTitle: TextView = itemView.findViewById(R.id.tv_title) 141 | var cb: CheckBox = itemView.findViewById(R.id.cb) 142 | } 143 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/notify_item/NotifyItemDataManager.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.notify_item 2 | 3 | import com.ocnyang.recyclerviewevent.R 4 | 5 | class NotifyItemDataManager { 6 | companion object { 7 | @JvmStatic 8 | fun getData() = mutableListOf( 9 | NotifyItemBean("香蕉?", R.drawable.ic_fruit_icons_01), 10 | NotifyItemBean("苹果?", R.drawable.ic_fruit_icons_02), 11 | NotifyItemBean("草莓?", R.drawable.ic_fruit_icons_03), 12 | NotifyItemBean("橙子?", R.drawable.ic_fruit_icons_04), 13 | NotifyItemBean("柠檬?", R.drawable.ic_fruit_icons_05), 14 | NotifyItemBean("雪梨?", R.drawable.ic_fruit_icons_06), 15 | NotifyItemBean("樱桃?", R.drawable.ic_fruit_icons_01), 16 | NotifyItemBean("哈密瓜?", R.drawable.ic_fruit_icons_02), 17 | NotifyItemBean("猕猴桃?", R.drawable.ic_fruit_icons_03), 18 | NotifyItemBean("葡萄?", R.drawable.ic_fruit_icons_04), 19 | ) 20 | } 21 | } 22 | 23 | data class NotifyItemBean(var name: String, var imgId: Int, var checked: Boolean = false) -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/FruitPagingSource.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import androidx.paging.PagingSource 4 | import androidx.paging.PagingState 5 | import java.lang.Exception 6 | 7 | class FruitPagingSource( 8 | private val repository: FruitRepository, 9 | private val queryFruitName: String 10 | ) : PagingSource() { 11 | 12 | override suspend fun load(params: LoadParams): LoadResult { 13 | return try { 14 | var nextPageNumber = params.key ?: 1 15 | val baseBean = repository.requestFruitList(queryFruitName, nextPageNumber) 16 | 17 | if (baseBean.isSuccess) { 18 | val currentPage = baseBean.data.currentPage 19 | val prevPageNumber = if (currentPage > 1) currentPage - 1 else null 20 | val nextPageNumber = if (currentPage < baseBean.data.totalPage) currentPage + 1 else null 21 | 22 | LoadResult.Page( 23 | data = baseBean.data.fruits, 24 | prevKey = prevPageNumber, 25 | nextKey = nextPageNumber 26 | ) 27 | } else { 28 | LoadResult.Error(Exception()) 29 | } 30 | } catch (e: Exception) { 31 | LoadResult.Error(e) 32 | } 33 | } 34 | 35 | override fun getRefreshKey(state: PagingState): Int? { 36 | // TODO("Not yet implemented") 37 | return 1 38 | } 39 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/FruitRepository.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import com.ocnyang.recyclerviewevent.R 4 | import kotlinx.coroutines.delay 5 | 6 | class FruitRepository { 7 | 8 | companion object { 9 | const val PAGE_MAX = 6 10 | val IMG_IDS = listOf( 11 | R.drawable.ic_fruit_icons_01, 12 | R.drawable.ic_fruit_icons_02, 13 | R.drawable.ic_fruit_icons_03, 14 | R.drawable.ic_fruit_icons_04, 15 | R.drawable.ic_fruit_icons_05, 16 | R.drawable.ic_fruit_icons_06, 17 | ) 18 | } 19 | 20 | suspend fun requestFruitList( 21 | fruitName: String, 22 | pageNumber: Int 23 | ): BaseBean { 24 | delay(1000) 25 | 26 | val fruitList = mutableListOf() 27 | for (i in 0..9) { 28 | fruitList.add( 29 | PagingFruitBean( 30 | "$fruitName $pageNumber$i 代", 31 | imgId = IMG_IDS[(pageNumber + i) % IMG_IDS.size] 32 | ) 33 | ) 34 | } 35 | 36 | return BaseBean( 37 | isSuccess = true, 38 | msg = "success", 39 | data = PagingFruitResultBean( 40 | fruits = fruitList, 41 | currentPage = pageNumber, 42 | totalPage = PAGE_MAX 43 | ) 44 | ) 45 | } 46 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.lifecycle.ViewModelProvider 6 | import androidx.recyclerview.widget.LinearLayoutManager 7 | import androidx.recyclerview.widget.RecyclerView 8 | import com.ocnyang.recyclerviewevent.R 9 | 10 | class PagingActivity : AppCompatActivity() { 11 | private lateinit var mAdapter: PagingAdapter 12 | private val viewModel by lazy { 13 | ViewModelProvider(this, PagingViewModelFactory()).get( 14 | PagingViewModel::class.java 15 | ) 16 | } 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(R.layout.activity_paging) 21 | 22 | findViewById(R.id.recyclerView).apply { 23 | layoutManager = LinearLayoutManager(context) 24 | mAdapter = PagingAdapter(context) 25 | adapter = mAdapter.withLoadStateHeaderAndFooter( 26 | header = PagingLoadStateAdapter(context), 27 | footer = PagingLoadStateAdapter(context) 28 | ) 29 | } 30 | 31 | viewModel.requestFruit("苹果") { 32 | mAdapter.submitData(it) 33 | } 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import android.content.Context 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 | import androidx.paging.PagingDataAdapter 10 | import androidx.recyclerview.widget.RecyclerView 11 | import com.ocnyang.recyclerviewevent.R 12 | 13 | class PagingAdapter(val context: Context) : 14 | PagingDataAdapter(PagingAdapterDiffCallback()) { 15 | 16 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagingViewHolder = 17 | PagingViewHolder( 18 | LayoutInflater.from(context).inflate(R.layout.activity_diff_util_item, parent, false) 19 | ) 20 | 21 | override fun onBindViewHolder(holder: PagingViewHolder, position: Int) { 22 | val itemBean = getItem(position) 23 | holder.ivImg.setImageResource(itemBean?.imgId ?: R.drawable.ic_fruit_icons_01) 24 | holder.tvTitle.text = itemBean?.fruitName ?: "" 25 | } 26 | 27 | } 28 | 29 | 30 | class PagingViewHolder(view: View) : RecyclerView.ViewHolder(view) { 31 | var ivImg: ImageView = itemView.findViewById(R.id.iv_img) 32 | var tvTitle: TextView = itemView.findViewById(R.id.tv_title) 33 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingAdapterDiffCallback.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import androidx.recyclerview.widget.DiffUtil 4 | 5 | class PagingAdapterDiffCallback : DiffUtil.ItemCallback() { 6 | 7 | override fun areItemsTheSame(oldItem: PagingFruitBean, newItem: PagingFruitBean): Boolean { 8 | return oldItem.javaClass == newItem.javaClass 9 | } 10 | 11 | override fun areContentsTheSame(oldItem: PagingFruitBean, newItem: PagingFruitBean): Boolean { 12 | return oldItem == newItem 13 | } 14 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingFruitBean.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import androidx.annotation.DrawableRes 4 | 5 | data class PagingFruitBean(var fruitName: String,@DrawableRes var imgId: Int) 6 | 7 | data class PagingFruitResultBean( 8 | var fruits: List, 9 | var currentPage: Int, 10 | var totalPage: Int 11 | ) 12 | 13 | data class BaseBean(var data: T, var isSuccess: Boolean, var msg: String) 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingLoadStateAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.ProgressBar 8 | import android.widget.TextView 9 | import androidx.paging.LoadState 10 | import androidx.paging.LoadStateAdapter 11 | import androidx.recyclerview.widget.RecyclerView 12 | import com.ocnyang.recyclerviewevent.R 13 | 14 | open class PagingLoadStateAdapter(val context: Context) : LoadStateAdapter() { 15 | override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadStateViewHolder { 16 | return LoadStateViewHolder( 17 | LayoutInflater.from(context) 18 | .inflate(R.layout.activity_paging_item_loadstate, parent, false) 19 | ) 20 | } 21 | 22 | override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) { 23 | holder.tvLoadState.text = when (loadState) { 24 | is LoadState.Loading -> "正在加载" 25 | is LoadState.Error -> "加载错误" 26 | is LoadState.NotLoading -> "没有加载" 27 | } 28 | } 29 | } 30 | 31 | class LoadStateViewHolder(view: View) : RecyclerView.ViewHolder(view) { 32 | var tvLoadState: TextView = view.findViewById(R.id.tvLoadState) 33 | var progressBar: ProgressBar = view.findViewById(R.id.progressBar) 34 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.asLiveData 5 | import androidx.lifecycle.viewModelScope 6 | import androidx.paging.Pager 7 | import androidx.paging.PagingConfig 8 | import androidx.paging.PagingData 9 | import androidx.paging.cachedIn 10 | import kotlinx.coroutines.flow.collectLatest 11 | import kotlinx.coroutines.launch 12 | 13 | class PagingViewModel(private val repository: FruitRepository) : ViewModel() { 14 | 15 | private fun fruitFlow(fruitName: String) = Pager( 16 | config = PagingConfig(pageSize = 10, initialLoadSize = 20, enablePlaceholders = false), 17 | pagingSourceFactory = { 18 | FruitPagingSource(repository = repository, queryFruitName = fruitName) 19 | }).flow 20 | 21 | 22 | // fun requestFruit(fruitName: String) = Pager( 23 | // config = PagingConfig(pageSize = 10, initialLoadSize = 20, enablePlaceholders = false), 24 | // pagingSourceFactory = { 25 | // FruitPagingSource(repository = repository, queryFruitName = fruitName) 26 | // }) 27 | // .flow 28 | // .cachedIn(viewModelScope) 29 | 30 | 31 | fun requestFruit( 32 | fruitName: String, 33 | action: suspend (value: PagingData) -> Unit 34 | ) { 35 | viewModelScope.launch { 36 | fruitFlow(fruitName).collectLatest(action = action) 37 | } 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/paging3/PagingViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.paging3 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.ViewModelProvider 5 | import java.lang.IllegalArgumentException 6 | 7 | class PagingViewModelFactory : ViewModelProvider.Factory { 8 | override fun create(modelClass: Class): T { 9 | if (modelClass.isAssignableFrom(PagingViewModel::class.java)) { 10 | return PagingViewModel(FruitRepository()) as T 11 | } 12 | 13 | throw IllegalArgumentException("Unknown ViewModel class") 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/reuse_disorder/EleganceMethodActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.reuse_disorder 2 | 3 | import android.content.Context 4 | import androidx.appcompat.app.AppCompatActivity 5 | import android.os.Bundle 6 | import android.text.Editable 7 | import android.text.TextWatcher 8 | import android.view.LayoutInflater 9 | import android.view.MenuItem 10 | import android.view.View 11 | import android.view.ViewGroup 12 | import android.widget.* 13 | import androidx.recyclerview.widget.LinearLayoutManager 14 | import androidx.recyclerview.widget.RecyclerView 15 | import com.ocnyang.recyclerviewevent.DataManager 16 | import com.ocnyang.recyclerviewevent.R 17 | 18 | class EleganceMethodActivity : AppCompatActivity() { 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_elegance_method) 22 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) 23 | 24 | findViewById(R.id.recyclerView).apply { 25 | this.layoutManager = LinearLayoutManager(this@EleganceMethodActivity) 26 | this.adapter = EleganceMethodAdapter( 27 | this@EleganceMethodActivity, 28 | DataManager.getData(20).map { FruitBean(title = it) }.toMutableList() 29 | ) 30 | } 31 | } 32 | 33 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 34 | if (item.itemId == android.R.id.home) { 35 | finish() 36 | } 37 | return super.onOptionsItemSelected(item) 38 | } 39 | } 40 | 41 | class EleganceMethodAdapter(private val context: Context, var data: MutableList) : 42 | RecyclerView.Adapter() { 43 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EleganceMethodViewHolder { 44 | val itemView = 45 | LayoutInflater.from(context) 46 | .inflate(R.layout.activity_elegance_method_item, parent, false) 47 | return EleganceMethodViewHolder(itemView) 48 | } 49 | 50 | override fun onBindViewHolder(holder: EleganceMethodViewHolder, position: Int) { 51 | holder.ivImg.setImageResource(imgList[position % imgList.size]) 52 | holder.tvTitle.text = data[position].title 53 | 54 | // 通过只给 CheckBox 设置点击事件 来解决复用错乱 55 | // holder.checkBox.setOnClickListener { 56 | // data[position].checked = !data[position].checked 57 | // } 58 | // 59 | // if (holder.checkBox.isChecked != data[position].checked) { 60 | // holder.checkBox.isChecked = data[position].checked 61 | // } 62 | 63 | /** 64 | * CheckBox 解决复用方法: 65 | * 1. 将监听 CheckBox 状态变换的监听器设为 null。(主要目的是为了防止,设置 CheckBox 选中状态时回调监听) 66 | * 2. 根据 条目数据 设置 CheckBox 的选中状态 67 | * 3. 给 CheckBox 设置一个状态变化时的监听器,当状态变化时,将对应的 itemBean 的状态值对应改变 68 | */ 69 | holder.checkBox.setOnCheckedChangeListener(null) 70 | holder.checkBox.isChecked = data[position].checked 71 | val onCheckedChangeListener = 72 | CompoundButton.OnCheckedChangeListener { _, isChecked -> 73 | data[position].checked = isChecked 74 | } 75 | holder.checkBox.setOnCheckedChangeListener(onCheckedChangeListener) 76 | 77 | 78 | /** 79 | * EditText 解决复用方法讲解: 80 | * 主要思路同上 CheckBox 的解决方法。 81 | * 但 EditText 的文本变化监听器,不能通过 addTextChangedListener(null) 清空, 82 | * 只能通过 removeTextChangedListener(TextWatcher watcher) 移除掉监听器,参数是你要移除的监听器 83 | * 84 | * 所以我们要解决的就是怎么保存监听器,当需要移除时需要获取它 85 | * 最好的方法,就是和 EditText 一样将 TextWatcher 保存到 ViewHolder 86 | * 这样同样能保证当 RecyclerView 通过复用机制在滚动中复用 item 时(其实就是复用的 ViewHolder)时, 复用的 EdiText 和 TextWatcher 是对应的 87 | * 88 | */ 89 | holder.editText.removeTextChangedListener(holder.textWatcher) 90 | holder.editText.setText(data[position].inputStr) 91 | val textWatcher = object : TextWatcher { 92 | override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { 93 | } 94 | 95 | override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { 96 | } 97 | 98 | override fun afterTextChanged(s: Editable?) { 99 | data[position].inputStr = s.toString() 100 | } 101 | } 102 | holder.editText.addTextChangedListener(textWatcher) 103 | holder.textWatcher = textWatcher 104 | } 105 | 106 | override fun getItemCount(): Int = data.size 107 | 108 | companion object { 109 | val imgList = listOf( 110 | R.drawable.ic_fruit_icons_01, R.drawable.ic_fruit_icons_02, 111 | R.drawable.ic_fruit_icons_03, R.drawable.ic_fruit_icons_04, 112 | R.drawable.ic_fruit_icons_05, R.drawable.ic_fruit_icons_06 113 | ) 114 | } 115 | } 116 | 117 | class EleganceMethodViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 118 | var ivImg: ImageView = itemView.findViewById(R.id.iv_img) 119 | var checkBox: CheckBox = itemView.findViewById(R.id.checkbox) 120 | var tvTitle: TextView = itemView.findViewById(R.id.tv_title) 121 | var editText: EditText = itemView.findViewById(R.id.editText) 122 | var textWatcher: TextWatcher? = null 123 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/reuse_disorder/FruitBean.kt: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.reuse_disorder 2 | 3 | /******************************************************************* 4 | * * * * * * * * * * * @Author: OCN.Yang 5 | * * * * * * * @CreateDate: 2020/11/3 3:13 PM. 6 | * * * * * * * @Email: ocnyang@gmail.com 7 | * * * * * * * * * * *.Yang @GitHub: https://github.com/OCNYang 8 | *******************************************************************/ 9 | 10 | data class FruitBean( 11 | var title: String = "", 12 | var checked: Boolean = false, 13 | var inputStr: String = "" 14 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/GridViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag; 2 | 3 | import android.os.Bundle; 4 | 5 | import androidx.appcompat.app.AppCompatActivity; 6 | import androidx.recyclerview.widget.GridLayoutManager; 7 | import androidx.recyclerview.widget.RecyclerView; 8 | import androidx.recyclerview.widget.ItemTouchHelper; 9 | 10 | import android.view.MenuItem; 11 | import android.widget.Toast; 12 | 13 | import com.ocnyang.recyclerviewevent.DataManager; 14 | import com.ocnyang.recyclerviewevent.R; 15 | import com.ocnyang.recyclerviewevent.swipe_and_drag.recyembellish.DividerGridItemDecoration; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | public class GridViewActivity extends AppCompatActivity { 21 | RecyclerView mRecyclerView; 22 | List mStringList; 23 | RecyAdapter mRecyAdapter; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_recyclerview); 29 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 30 | mRecyclerView = (RecyclerView) findViewById(R.id.recy); 31 | initRecy(); 32 | } 33 | 34 | private void initRecy() { 35 | if (mStringList == null) { 36 | mStringList = new ArrayList<>(); 37 | } 38 | mStringList.addAll(DataManager.getData(20 - mStringList.size())); 39 | mRecyAdapter = new RecyAdapter(R.layout.item_gridview, mStringList, true); 40 | 41 | mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4)); 42 | mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this)); 43 | mRecyclerView.setHasFixedSize(true); 44 | 45 | RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter, false, true); 46 | final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback); 47 | itemTouchHelper.attachToRecyclerView(mRecyclerView); 48 | 49 | mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) { 50 | @Override 51 | public void onItemClick(RecyclerView.ViewHolder viewHolder) { 52 | RecyAdapter.ViewHolder viewHolder1 = (RecyAdapter.ViewHolder) viewHolder; 53 | String tvString = viewHolder1.mTextView.getText().toString(); 54 | Toast.makeText(GridViewActivity.this, "逗逗~" + tvString, Toast.LENGTH_SHORT).show(); 55 | } 56 | 57 | @Override 58 | public void onLongClick(RecyclerView.ViewHolder viewHolder) { 59 | Toast.makeText(GridViewActivity.this, "" + "讨厌,不要老是摸人家啦...", Toast.LENGTH_SHORT).show(); 60 | if (viewHolder.getLayoutPosition() != 0) { 61 | itemTouchHelper.startDrag(viewHolder); 62 | } 63 | } 64 | }); 65 | mRecyclerView.setAdapter(mRecyAdapter); 66 | } 67 | 68 | @Override 69 | public boolean onOptionsItemSelected(MenuItem item) { 70 | if (item.getItemId() == android.R.id.home) { 71 | finish(); 72 | } 73 | return super.onOptionsItemSelected(item); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/ListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag; 2 | 3 | import android.os.Bundle; 4 | import android.view.MenuItem; 5 | import android.widget.Toast; 6 | 7 | import androidx.appcompat.app.AppCompatActivity; 8 | import androidx.recyclerview.widget.ItemTouchHelper; 9 | import androidx.recyclerview.widget.LinearLayoutManager; 10 | import androidx.recyclerview.widget.RecyclerView; 11 | 12 | import com.ocnyang.recyclerviewevent.DataManager; 13 | import com.ocnyang.recyclerviewevent.R; 14 | import com.ocnyang.recyclerviewevent.swipe_and_drag.recyembellish.DividerListItemDecoration; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class ListViewActivity extends AppCompatActivity { 20 | 21 | RecyclerView mRecyclerView; 22 | List mStringList; 23 | RecyAdapter mRecyAdapter; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_recyclerview); 29 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 30 | mRecyclerView = (RecyclerView) findViewById(R.id.recy); 31 | initRecy(); 32 | } 33 | 34 | private void initRecy() { 35 | if (mStringList == null) { 36 | mStringList = new ArrayList<>(); 37 | } 38 | mStringList.addAll(DataManager.getData(15 - mStringList.size())); 39 | mRecyAdapter = new RecyAdapter(R.layout.item_listview, mStringList); 40 | 41 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 42 | mRecyclerView.addItemDecoration(new DividerListItemDecoration(this, LinearLayoutManager.VERTICAL)); 43 | mRecyclerView.setHasFixedSize(true); 44 | 45 | RecyItemTouchHelperCallback itemTouchHelperCallback = new RecyItemTouchHelperCallback(mRecyAdapter); 46 | final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback); 47 | itemTouchHelper.attachToRecyclerView(mRecyclerView); 48 | 49 | mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) { 50 | @Override 51 | public void onItemClick(RecyclerView.ViewHolder viewHolder) { 52 | RecyAdapter.ViewHolder viewHolder1 = (RecyAdapter.ViewHolder) viewHolder; 53 | String tvString = viewHolder1.mTextView.getText().toString(); 54 | Toast.makeText(ListViewActivity.this, "逗逗~" + tvString, Toast.LENGTH_SHORT).show(); 55 | } 56 | 57 | @Override 58 | public void onLongClick(RecyclerView.ViewHolder viewHolder) { 59 | Toast.makeText(ListViewActivity.this, "" + "讨厌,不要老是摸人家啦...", Toast.LENGTH_SHORT).show(); 60 | } 61 | }); 62 | mRecyclerView.setAdapter(mRecyAdapter); 63 | } 64 | 65 | @Override 66 | public boolean onOptionsItemSelected(MenuItem item) { 67 | if (item.getItemId() == android.R.id.home) { 68 | finish(); 69 | } 70 | return super.onOptionsItemSelected(item); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/OnRecyclerItemClickListener.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag; 2 | 3 | import androidx.core.view.GestureDetectorCompat; 4 | import androidx.recyclerview.widget.RecyclerView; 5 | 6 | import android.view.GestureDetector; 7 | import android.view.MotionEvent; 8 | import android.view.View; 9 | 10 | /******************************************************************* 11 | * * * * * * * * * * * Created by OCN.Yang 12 | * * * * * * * Time:2017/8/2 10:30. 13 | * * * * * * * Email address:ocnyang@gmail.com 14 | * * * * * * * * * * *.Yang Web site:www.ocnyang.com 15 | *******************************************************************/ 16 | 17 | 18 | public abstract class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener { 19 | private GestureDetectorCompat mGestureDetectorCompat; 20 | private RecyclerView mRecyclerView; 21 | 22 | public OnRecyclerItemClickListener(RecyclerView recyclerView) { 23 | mRecyclerView = recyclerView; 24 | mGestureDetectorCompat = new GestureDetectorCompat(mRecyclerView.getContext(), 25 | new ItemTouchHelperGestureListener()); 26 | } 27 | 28 | @Override 29 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 30 | mGestureDetectorCompat.onTouchEvent(e); 31 | return false; 32 | } 33 | 34 | @Override 35 | public void onTouchEvent(RecyclerView rv, MotionEvent e) { 36 | mGestureDetectorCompat.onTouchEvent(e); 37 | } 38 | 39 | @Override 40 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 41 | 42 | } 43 | 44 | public abstract void onItemClick(RecyclerView.ViewHolder viewHolder); 45 | 46 | public abstract void onLongClick(RecyclerView.ViewHolder viewHolder); 47 | 48 | private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener { 49 | @Override 50 | public boolean onSingleTapUp(MotionEvent e) { 51 | View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); 52 | if (childViewUnder != null) { 53 | RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder); 54 | onItemClick(childViewHolder); 55 | } 56 | return true; 57 | } 58 | 59 | @Override 60 | public void onLongPress(MotionEvent e) { 61 | View childViewUnder = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); 62 | if (childViewUnder != null) { 63 | RecyclerView.ViewHolder childViewHolder = mRecyclerView.getChildViewHolder(childViewUnder); 64 | onLongClick(childViewHolder); 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/RecyAdapter.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag; 2 | 3 | import android.graphics.Color; 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 androidx.recyclerview.widget.RecyclerView; 11 | 12 | import com.ocnyang.recyclerviewevent.R; 13 | 14 | import java.util.Arrays; 15 | import java.util.List; 16 | 17 | /******************************************************************* 18 | * * * * * * * * * * * Created by OCN.Yang 19 | * * * * * * * Time:2017/8/2 09:57. 20 | * * * * * * * Email address:ocnyang@gmail.com 21 | * * * * * * * * * * *.Yang Web site:www.ocnyang.com 22 | *******************************************************************/ 23 | 24 | 25 | public class RecyAdapter extends RecyclerView.Adapter { 26 | private int item_layout; 27 | private List mDataList; 28 | private List mInts; 29 | private boolean isFirstSpecial; 30 | 31 | public RecyAdapter(int item_layout, List dataList) { 32 | this.item_layout = item_layout; 33 | mDataList = dataList; 34 | mInts = Arrays.asList(R.drawable.ic_fruit_icons_01, R.drawable.ic_fruit_icons_02, 35 | R.drawable.ic_fruit_icons_03, R.drawable.ic_fruit_icons_04, 36 | R.drawable.ic_fruit_icons_05, R.drawable.ic_fruit_icons_06); 37 | } 38 | 39 | public RecyAdapter(int item_layout, List dataList, boolean isFirstSpecial) { 40 | this(item_layout, dataList); 41 | this.isFirstSpecial = isFirstSpecial; 42 | } 43 | 44 | @Override 45 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 46 | View view = LayoutInflater.from(parent.getContext()).inflate(item_layout, parent, false); 47 | return new ViewHolder(view); 48 | } 49 | 50 | @Override 51 | public void onBindViewHolder(ViewHolder holder, int position) { 52 | String string = mDataList.get(position); 53 | if (isFirstSpecial && position == 0) { 54 | holder.itemView.setBackgroundColor(Color.LTGRAY); 55 | holder.mTextView.setText("iPhone"); 56 | holder.mImageView.setImageResource(R.drawable.ic_iphone); 57 | } else { 58 | holder.itemView.setBackgroundColor(Color.WHITE); 59 | holder.mTextView.setText(string); 60 | holder.mImageView.setImageResource(mInts.get(position % mInts.size())); 61 | } 62 | } 63 | 64 | @Override 65 | public int getItemCount() { 66 | return mDataList == null ? 0 : mDataList.size(); 67 | } 68 | 69 | public List getDataList() { 70 | return mDataList; 71 | } 72 | 73 | class ViewHolder extends RecyclerView.ViewHolder { 74 | TextView mTextView; 75 | ImageView mImageView; 76 | 77 | ViewHolder(View itemView) { 78 | super(itemView); 79 | mTextView = itemView.findViewById(R.id.tv_item); 80 | mImageView = itemView.findViewById(R.id.img_item); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/RecyItemTouchHelperCallback.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag; 2 | 3 | import android.graphics.Color; 4 | 5 | import androidx.recyclerview.widget.GridLayoutManager; 6 | import androidx.recyclerview.widget.RecyclerView; 7 | import androidx.recyclerview.widget.ItemTouchHelper; 8 | 9 | import com.ocnyang.recyclerviewevent.swipe_and_drag.RecyAdapter; 10 | 11 | import java.util.Collections; 12 | 13 | /******************************************************************* 14 | * * * * * * * * * * * Created by OCN.Yang 15 | * * * * * * * Time:2017/8/2 11:37. 16 | * * * * * * * Email address:ocnyang@gmail.com 17 | * * * * * * * * * * *.Yang Web site:www.ocnyang.com 18 | *******************************************************************/ 19 | 20 | 21 | public class RecyItemTouchHelperCallback extends ItemTouchHelper.Callback { 22 | RecyclerView.Adapter mAdapter; 23 | boolean isSwipeEnable; 24 | boolean isFirstDragUnable; 25 | 26 | public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter) { 27 | mAdapter = adapter; 28 | isSwipeEnable = true; 29 | isFirstDragUnable = false; 30 | } 31 | 32 | public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter, boolean isSwipeEnable, boolean isFirstDragUnable) { 33 | mAdapter = adapter; 34 | this.isSwipeEnable = isSwipeEnable; 35 | this.isFirstDragUnable = isFirstDragUnable; 36 | } 37 | 38 | @Override 39 | public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 40 | if (recyclerView.getLayoutManager() instanceof GridLayoutManager) { 41 | int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | 42 | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; 43 | int swipeFlags = 0; 44 | return makeMovementFlags(dragFlags, swipeFlags); 45 | } else { 46 | int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 47 | int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; 48 | return makeMovementFlags(dragFlags, swipeFlags); 49 | } 50 | } 51 | 52 | @Override 53 | public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 54 | int fromPosition = viewHolder.getAdapterPosition(); 55 | int toPosition = target.getAdapterPosition(); 56 | if (isFirstDragUnable && toPosition == 0) { 57 | return false; 58 | } 59 | if (fromPosition < toPosition) { 60 | for (int i = fromPosition; i < toPosition; i++) { 61 | Collections.swap(((RecyAdapter) mAdapter).getDataList(), i, i + 1); 62 | } 63 | } else { 64 | for (int i = fromPosition; i > toPosition; i--) { 65 | Collections.swap(((RecyAdapter) mAdapter).getDataList(), i, i - 1); 66 | } 67 | } 68 | mAdapter.notifyItemMoved(fromPosition, toPosition); 69 | return true; 70 | } 71 | 72 | @Override 73 | public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 74 | int adapterPosition = viewHolder.getAdapterPosition(); 75 | mAdapter.notifyItemRemoved(adapterPosition); 76 | ((RecyAdapter) mAdapter).getDataList().remove(adapterPosition); 77 | } 78 | 79 | @Override 80 | public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { 81 | if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { 82 | viewHolder.itemView.setBackgroundColor(Color.LTGRAY); 83 | } 84 | super.onSelectedChanged(viewHolder, actionState); 85 | } 86 | 87 | @Override 88 | public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 89 | super.clearView(recyclerView, viewHolder); 90 | viewHolder.itemView.setBackgroundColor(Color.WHITE); 91 | } 92 | 93 | @Override 94 | public boolean isLongPressDragEnabled() { 95 | return !isFirstDragUnable; 96 | } 97 | 98 | @Override 99 | public boolean isItemViewSwipeEnabled() { 100 | return isSwipeEnable; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/recyembellish/DividerGridItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag.recyembellish; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Rect; 8 | import android.graphics.drawable.ColorDrawable; 9 | import android.graphics.drawable.Drawable; 10 | 11 | import androidx.recyclerview.widget.GridLayoutManager; 12 | import androidx.recyclerview.widget.RecyclerView; 13 | import androidx.recyclerview.widget.RecyclerView.LayoutManager; 14 | import androidx.recyclerview.widget.RecyclerView.State; 15 | import androidx.recyclerview.widget.StaggeredGridLayoutManager; 16 | 17 | import android.view.View; 18 | 19 | /******************************************************************* 20 | * * * * * * * * * * * Created by OCN.Yang 21 | * * * * * * * Time:2017/8/2 11:30. 22 | * * * * * * * Email address:ocnyang@gmail.com 23 | * * * * * * * * * * *.Yang Web site:www.ocnyang.com 24 | *******************************************************************/ 25 | 26 | 27 | public class DividerGridItemDecoration extends RecyclerView.ItemDecoration { 28 | 29 | private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 30 | private Drawable mDivider; 31 | private int lineWidth = 1; 32 | 33 | public DividerGridItemDecoration(Context context) { 34 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 35 | mDivider = a.getDrawable(0); 36 | a.recycle(); 37 | } 38 | 39 | public DividerGridItemDecoration(int color) { 40 | mDivider = new ColorDrawable(color); 41 | } 42 | 43 | public DividerGridItemDecoration() { 44 | this(Color.parseColor("#cccccc")); 45 | } 46 | 47 | @Override 48 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 49 | drawHorizontal(c, parent); 50 | drawVertical(c, parent); 51 | } 52 | 53 | private int getSpanCount(RecyclerView parent) { 54 | // 列数 55 | int spanCount = -1; 56 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 57 | if (layoutManager instanceof GridLayoutManager) { 58 | 59 | spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); 60 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 61 | spanCount = ((StaggeredGridLayoutManager) layoutManager) 62 | .getSpanCount(); 63 | } 64 | return spanCount; 65 | } 66 | 67 | public void drawHorizontal(Canvas c, RecyclerView parent) { 68 | int childCount = parent.getChildCount(); 69 | for (int i = 0; i < childCount; i++) { 70 | final View child = parent.getChildAt(i); 71 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 72 | .getLayoutParams(); 73 | final int left = child.getLeft() - params.leftMargin; 74 | final int right = child.getRight() + params.rightMargin 75 | + lineWidth; 76 | final int top = child.getBottom() + params.bottomMargin; 77 | final int bottom = top + lineWidth; 78 | mDivider.setBounds(left, top, right, bottom); 79 | mDivider.draw(c); 80 | } 81 | } 82 | 83 | public void drawVertical(Canvas c, RecyclerView parent) { 84 | final int childCount = parent.getChildCount(); 85 | for (int i = 0; i < childCount; i++) { 86 | final View child = parent.getChildAt(i); 87 | 88 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 89 | final int top = child.getTop() - params.topMargin; 90 | final int bottom = child.getBottom() + params.bottomMargin; 91 | final int left = child.getRight() + params.rightMargin; 92 | final int right = left + lineWidth; 93 | 94 | mDivider.setBounds(left, top, right, bottom); 95 | mDivider.draw(c); 96 | } 97 | } 98 | 99 | private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) { 100 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 101 | if (layoutManager instanceof GridLayoutManager) { 102 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 103 | { 104 | return true; 105 | } 106 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 107 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 108 | .getOrientation(); 109 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 110 | if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边 111 | { 112 | return true; 113 | } 114 | } else { 115 | childCount = childCount - childCount % spanCount; 116 | if (pos >= childCount)// 如果是最后一列,则不需要绘制右边 117 | return true; 118 | } 119 | } 120 | return false; 121 | } 122 | 123 | private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) { 124 | LayoutManager layoutManager = parent.getLayoutManager(); 125 | if (layoutManager instanceof GridLayoutManager) { 126 | childCount = childCount - childCount % spanCount; 127 | if (pos >= childCount)// 如果是最后一行,则不需要绘制底部 128 | return true; 129 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 130 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 131 | .getOrientation(); 132 | // StaggeredGridLayoutManager 且纵向滚动 133 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 134 | childCount = childCount - childCount % spanCount; 135 | // 如果是最后一行,则不需要绘制底部 136 | if (pos >= childCount) 137 | return true; 138 | } else 139 | // StaggeredGridLayoutManager 且横向滚动 140 | { 141 | // 如果是最后一行,则不需要绘制底部 142 | if ((pos + 1) % spanCount == 0) { 143 | return true; 144 | } 145 | } 146 | } 147 | return false; 148 | } 149 | 150 | @Override 151 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) { 152 | boolean b = state.willRunPredictiveAnimations(); 153 | int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); 154 | int spanCount = getSpanCount(parent); 155 | int childCount = parent.getAdapter().getItemCount(); 156 | // if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行,则不需要绘制底部 157 | // { 158 | // outRect.set(0, 0, lineWidth, 0); 159 | // } 160 | // else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列,则不需要绘制右边 161 | // { 162 | //// if (b){ 163 | //// outRect.set(0, 0, lineWidth, lineWidth); 164 | //// }else { 165 | // outRect.set(0, 0, 0, lineWidth); 166 | //// } 167 | // } 168 | // else { 169 | outRect.set(0, 0, lineWidth, lineWidth); 170 | // } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /app/src/main/java/com/ocnyang/recyclerviewevent/swipe_and_drag/recyembellish/DividerListItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent.swipe_and_drag.recyembellish; 2 | 3 | /******************************************************************* 4 | * * * * * * * * * * * Created by OCN.Yang 5 | * * * * * * * Time:2017/8/2 11:30. 6 | * * * * * * * Email address:ocnyang@gmail.com 7 | * * * * * * * * * * *.Yang Web site:www.ocnyang.com 8 | *******************************************************************/ 9 | 10 | 11 | import android.content.Context; 12 | import android.content.res.TypedArray; 13 | import android.graphics.Canvas; 14 | import android.graphics.Rect; 15 | import android.graphics.drawable.Drawable; 16 | 17 | import androidx.core.content.ContextCompat; 18 | import androidx.recyclerview.widget.LinearLayoutManager; 19 | import androidx.recyclerview.widget.RecyclerView; 20 | 21 | import android.view.View; 22 | 23 | /** 24 | * 竖直或者水平方向上的分割线,适用于类似于listview效果的分割线,不适用于GrivdView效果的分割线* 25 | */ 26 | public class DividerListItemDecoration extends RecyclerView.ItemDecoration { 27 | private static final int[] ATTRS = new int[]{ 28 | android.R.attr.listDivider 29 | }; 30 | 31 | public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 32 | 33 | public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 34 | 35 | private Drawable mDivider; 36 | 37 | private int mOrientation; 38 | 39 | public DividerListItemDecoration(Context context, int orientation) { 40 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 41 | mDivider = a.getDrawable(0); 42 | a.recycle(); 43 | setOrientation(orientation); 44 | } 45 | 46 | public DividerListItemDecoration(Context context, int orientation, int drawableId) { 47 | mDivider = ContextCompat.getDrawable(context, drawableId); 48 | setOrientation(orientation); 49 | } 50 | 51 | public void setOrientation(int orientation) { 52 | if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { 53 | throw new IllegalArgumentException("invalid orientation"); 54 | } 55 | mOrientation = orientation; 56 | } 57 | 58 | @Override 59 | public void onDraw(Canvas c, RecyclerView parent) { 60 | if (mOrientation == VERTICAL_LIST) { 61 | drawVertical(c, parent); 62 | } else { 63 | drawHorizontal(c, parent); 64 | } 65 | 66 | } 67 | 68 | 69 | public void drawVertical(Canvas c, RecyclerView parent) { 70 | final int left = parent.getPaddingLeft(); 71 | final int right = parent.getWidth() - parent.getPaddingRight(); 72 | 73 | final int childCount = parent.getChildCount(); 74 | for (int i = 0; i < childCount; i++) { 75 | final View child = parent.getChildAt(i); 76 | RecyclerView v = new RecyclerView(parent.getContext()); 77 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 78 | .getLayoutParams(); 79 | final int top = child.getBottom() + params.bottomMargin; 80 | final int bottom = top + mDivider.getIntrinsicHeight(); 81 | mDivider.setBounds(left, top, right, bottom); 82 | mDivider.draw(c); 83 | } 84 | } 85 | 86 | public void drawHorizontal(Canvas c, RecyclerView parent) { 87 | final int top = parent.getPaddingTop(); 88 | final int bottom = parent.getHeight() - parent.getPaddingBottom(); 89 | 90 | final int childCount = parent.getChildCount(); 91 | for (int i = 0; i < childCount; i++) { 92 | final View child = parent.getChildAt(i); 93 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 94 | .getLayoutParams(); 95 | final int left = child.getRight() + params.rightMargin; 96 | final int right = left + mDivider.getIntrinsicHeight(); 97 | mDivider.setBounds(left, top, right, bottom); 98 | mDivider.draw(c); 99 | } 100 | } 101 | 102 | @Override 103 | public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { 104 | if (mOrientation == VERTICAL_LIST) { 105 | outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); 106 | } else { 107 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v21/shape_bg_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_fruit_icons_01.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 | 39 | 42 | 45 | 48 | 51 | 54 | 57 | 60 | 63 | 66 | 69 | 72 | 75 | 78 | 81 | 84 | 87 | 90 | 93 | 96 | 99 | 102 | 105 | 106 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_fruit_icons_02.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 | 39 | 42 | 43 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_fruit_icons_03.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_fruit_icons_04.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_fruit_icons_05.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_fruit_icons_06.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_iphone.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_bg_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_concat_adapter.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_concat_item_header.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 22 | 23 | 28 | 29 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_diff_util.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 24 | 25 | 26 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_diff_util_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | 19 | 20 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_elegance_method.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_elegance_method_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 20 | 21 | 27 | 28 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 19 | 20 | 28 | 29 | 37 | 38 | 39 | 47 | 48 | 56 | 57 | 65 | 66 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_notify_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 17 | 18 | 22 | 23 | 32 | 33 | 42 | 43 | 44 | 48 | 49 | 58 | 59 | 68 | 69 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_notify_item_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 16 | 17 | 25 | 26 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_paging.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_paging_item_loadstate.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 16 | 17 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_recyclerview.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_gridview.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_listview.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 16 | 17 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #ff212224 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RecyclerViewEvent 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/ocnyang/recyclerviewevent/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.ocnyang.recyclerviewevent; 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 | ext { 5 | kotlin_version = '1.4.10' 6 | } 7 | repositories { 8 | jcenter() 9 | google() 10 | } 11 | dependencies { 12 | classpath 'com.android.tools.build:gradle:4.1.0' 13 | classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20' 14 | 15 | // NOTE: Do not place your application dependencies here; they belong 16 | // in the individual module build.gradle files 17 | } 18 | } 19 | 20 | allprojects { 21 | repositories { 22 | jcenter() 23 | google() 24 | } 25 | } 26 | 27 | task clean(type: Delete) { 28 | delete rootProject.buildDir 29 | } 30 | -------------------------------------------------------------------------------- /docs/ConcatAdapter.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/docs/ConcatAdapter.gif -------------------------------------------------------------------------------- /docs/DiffUtil.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/docs/DiffUtil.gif -------------------------------------------------------------------------------- /docs/Paging3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/docs/Paging3.gif -------------------------------------------------------------------------------- /docs/PointRefresh.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/docs/PointRefresh.gif -------------------------------------------------------------------------------- /docs/concatadapter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/docs/concatadapter.jpg -------------------------------------------------------------------------------- /docs/recyclervieweventreuse_disorder.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/docs/recyclervieweventreuse_disorder.gif -------------------------------------------------------------------------------- /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 | android.enableJetifier=true 13 | android.useAndroidX=true 14 | org.gradle.jvmargs=-Xmx1536m 15 | 16 | # When configured, Gradle will run in incubating parallel mode. 17 | # This option should only be used with decoupled projects. More details, visit 18 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 19 | # org.gradle.parallel=true 20 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OCNYang/RecyclerViewEvent/7512c079f13e3bb53f70a97efd6ea24ae52008ec/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Nov 03 14:48:24 CST 2020 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-6.5-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 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------