├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── exampleApp ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── nekocode │ │ └── items │ │ └── example │ │ ├── BaseAdapter.java │ │ ├── MainActivity.kt │ │ ├── java │ │ ├── FooterItem.java │ │ ├── HeaderItem.java │ │ ├── HeaderOrFooterData.java │ │ ├── StringItem.java │ │ └── TestAdapter.java │ │ └── kotlin │ │ ├── KtFooterItemView.kt │ │ ├── KtHeaderItemView.kt │ │ ├── KtStringItemView.kt │ │ └── KtTestAdapter.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_main.xml │ ├── item_footer.xml │ ├── item_header.xml │ └── item_string.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.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 ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── itemsLib ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── cn │ └── nekocode │ └── items │ ├── BaseItem.java │ ├── ItemAdapter.java │ ├── ItemSelector.java │ └── annotation │ ├── AdapterClass.java │ ├── ItemMethod.java │ └── SelectorMethod.java ├── itemsProcessor ├── .gitignore ├── build.gradle └── src │ ├── main │ ├── java │ │ └── cn │ │ │ └── nekocode │ │ │ └── items │ │ │ └── processor │ │ │ ├── AdapterGenerator.kt │ │ │ ├── ItemsProcessor.kt │ │ │ ├── Names.kt │ │ │ ├── model │ │ │ ├── Item.kt │ │ │ └── Selector.kt │ │ │ └── util │ │ │ └── Either.kt │ └── resources │ │ └── META-INF │ │ ├── gradle │ │ └── incremental.annotation.processors │ │ └── services │ │ └── javax.annotation.processing.Processor │ └── test │ └── java │ └── cn │ └── nekocode │ └── items │ └── processor │ └── ItemsProcessorTest.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: oraclejdk8 3 | script: ./gradlew check 4 | 5 | after_success: 6 | - bash <(curl -s https://codecov.io/bash) 7 | 8 | android: 9 | components: 10 | - android-27 11 | - build-tools-28.0.3 12 | 13 | before_cache: 14 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 15 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 16 | 17 | cache: 18 | directories: 19 | - $HOME/.gradle/caches/ 20 | - $HOME/.gradle/wrapper/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Items 2 | 3 | [![Build Status](https://travis-ci.com/nekocode/Items.svg?branch=master)](https://travis-ci.com/nekocode/Items) [![codecov](https://codecov.io/gh/nekocode/Items/branch/master/graph/badge.svg)](https://codecov.io/gh/nekocode/Items) 4 | 5 | 这个库可以为 Android 的 `RecyclerView` 生成基于 **Data-View-Binding** 的 Adapter。 6 | 7 | 对比其他一些类似的开源库,它有以下的一些优势: 8 | * 更好的拓展性。这个库不需要你继承特定的 Adapter 或 ViewHolder 类,你可以继承任何第三方提供的基类; 9 | * 更好的性能。使用 Annotation Processor 意味着实现 Binding 时无需使用反射。支持 [增量处理](https://docs.gradle.org/current/userguide/java_plugin.html#sec:incremental_annotation_processing); 10 | * 更低的侵入性。和传统的 Adapter 写法类似,可以快速从旧的 Codebase 迁移到新的写法; 11 | * 更可靠的代码。提供了单元测试覆盖大部分的 Case。 12 | 13 | ## 集入 14 | 15 | 替换以下代码中的 `${lastest-version}` 为最新版本号 [![](https://jitpack.io/v/nekocode/Items.svg)](https://jitpack.io/#nekocode/Items),并复制到 Android 工程中的 build.gradle 脚本: 16 | 17 | ```gradle 18 | repositories { 19 | maven { url "https://jitpack.io" } 20 | } 21 | dependencies { 22 | implementation "com.github.nekocode.Items:itemsLib:${lastest-version}" 23 | annotationProcessor "com.github.nekocode.Items:itemsProcessor:${lastest-version}" 24 | } 25 | ``` 26 | 27 | 注意,在 Kotlin 工程中,需要使用 `kapt` 关键字代替 `annotationProcessor` 关键字。 28 | 29 | ## 使用 30 | 31 | 使用 `BaseItem` 能够帮助你把 `ViewHolder` 的创建和绑定从 Adapter 中提取出来,并且与特定的数据类型绑定。例如你可以为 `String` 类型的数据创建一个 Item: 32 | 33 | ```java 34 | public class StringItem extends BaseItem { 35 | 36 | public StringItem(ItemAdapter adapter, int viewType) { 37 | super(adapter, viewType); 38 | } 39 | 40 | @NonNull 41 | @Override 42 | public Holder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) { 43 | final View itemView = inflater.inflate(R.layout.item_string, parent, false); 44 | final Holder holder = new Holder(itemView); 45 | holder.button.setOnClickListener(v -> { 46 | if (getCallback() != null) { 47 | getCallback().onButtonClick(holder.data); 48 | } 49 | }); 50 | return holder; 51 | } 52 | 53 | @Override 54 | public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull String data) { 55 | holder.data = data; 56 | holder.textView.setText(data); 57 | } 58 | 59 | static class Holder extends RecyclerView.ViewHolder { 60 | private TextView textView; 61 | private Button button; 62 | private String data; 63 | 64 | Holder(View itemView) { 65 | super(itemView); 66 | textView = itemView.findViewById(R.id.textView); 67 | button = itemView.findViewById(R.id.button); 68 | } 69 | } 70 | 71 | public interface Callback { 72 | void onButtonClick(@NonNull String data); 73 | } 74 | } 75 | ``` 76 | 77 | Item 提供了很好的拓展能力: 78 | * 可以使用任意类型的 `ViewHolder`; 79 | * 可以通过 `Callback` 为 `ViewHolder` 设置 UI 事件回调。 80 | 81 | 接下来你需要创建一个 Adapter 来装载你的所有 Item: 82 | 83 | ```java 84 | @AdapterClass 85 | public abstract class TestAdapter extends RecyclerView.Adapter implements ItemAdapter { 86 | private final LinkedList mList = new LinkedList(); 87 | 88 | @NonNull 89 | public LinkedList list() { 90 | return mList; 91 | } 92 | 93 | @Override 94 | public int getItemCount() { 95 | return mList.size(); 96 | } 97 | 98 | @NonNull 99 | @Override 100 | public T getData(int position) { 101 | return (T) mList.get(position); 102 | } 103 | 104 | /** 105 | * 定义一个任意名称的方法来返回你想装载的 Item 106 | */ 107 | @NonNull 108 | @ItemMethod 109 | public abstract StringItem stringItem(); 110 | } 111 | ``` 112 | 113 | 这个 Adapter 必须实现 `ItemAdapter` 接口的 `getItemCount()` 和 `getData()` 方法。你可以使用任意类型的 Collection(例如上面的 `LinkedList`)来装载你的数据,这个 Adapter 会通过这两个方法来访问你的数据,然后根据数据的类型来选择对应的 Item 来创建 `ViewHolder`。 114 | 115 | 在编译期间,Annotation Processor 会为这个 Adapter 生成一个实现类 `TestAdapterImpl`,你可以通过以下例子来使用这个 Adapter: 116 | 117 | ```java 118 | // 创建 Adapter 实例 119 | TestAdapter adapter = new TestAdapterImpl(); 120 | 121 | // 给 Adapter 插入数据 122 | adapter.list().add("Item1"); 123 | adapter.list().add("Item2"); 124 | 125 | // 给 Item 设置 Callback 126 | adapter.stringItem().setCallback(data -> { 127 | // Button 点击时 128 | }); 129 | 130 | // 为 RecyclerView 设置 Adapter 131 | recyclerView.setAdapter(adapter); 132 | ``` 133 | 134 | 以上就是这个工具的基础使用。 135 | 136 | 此外,你还可以让你的单个数据类型绑定多个 Item: 137 | 138 | ```java 139 | @Adapter 140 | public abstract class TestAdapter extends RecyclerView.Adapter implements ItemAdapter { 141 | // ... 142 | 143 | @NonNull 144 | @ItemMethod 145 | public abstract StringItem stringItem(); 146 | 147 | @NonNull 148 | @ItemMethod 149 | public abstract StringItem2 stringItem2(); 150 | 151 | /** 152 | * 定义一个任意名称的方法来帮助 Adapter 选择绑定了同一数据类型的 Item 153 | */ 154 | @SelectorMethod 155 | public int itemForString(int position, @NonNull String data) { 156 | if (!data.endsWith(2)) { 157 | return stringItem().getViewType(); 158 | } else { 159 | return stringItem2().getViewType(); 160 | } 161 | } 162 | } 163 | ``` 164 | 165 | 最后,还有一个小的 Tip。你可以定义一些 BaseAdapter 来简化你 Adapter 的代码,例如把对集合的操作封装起来,举个例子: 166 | 167 | ```java 168 | public abstract class BaseArrayListAdapter extends RecyclerView.Adapter implements ItemAdapter { 169 | private final ArrayList mList = new ArrayList(); 170 | 171 | public ArrayList getList() { 172 | return mList; 173 | } 174 | 175 | @NonNull 176 | @Override 177 | public T getData(int position) { 178 | return (T) mList.get(position); 179 | } 180 | 181 | @Override 182 | public int getItemCount() { 183 | return mList.size(); 184 | } 185 | } 186 | 187 | @Adapter 188 | public abstract class TestAdapter extends BaseArrayListAdapter { 189 | // ... 190 | } 191 | ``` 192 | 193 | 更详细的应用可以参考这个仓库中的 [exampleApp](exampleApp) 模块。 194 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = "1.3.10" 3 | repositories { 4 | google() 5 | jcenter() 6 | maven { url "https://jitpack.io" } 7 | } 8 | dependencies { 9 | classpath "com.android.tools.build:gradle:3.2.1" 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | classpath "com.github.nekocode:GradleImportAar:0.5" 12 | classpath "com.github.dcendents:android-maven-gradle-plugin:1.5" 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | google() 19 | jcenter() 20 | } 21 | group="com.github.nekocode" 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | -------------------------------------------------------------------------------- /exampleApp/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /exampleApp/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: "com.android.application" 2 | apply plugin: "kotlin-android" 3 | apply plugin: "kotlin-android-extensions" 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion 27 8 | defaultConfig { 9 | applicationId "cn.nekocode.items.example" 10 | minSdkVersion 14 11 | targetSdkVersion 27 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" 19 | } 20 | } 21 | compileOptions { 22 | targetCompatibility 1.8 23 | sourceCompatibility 1.8 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: "libs", include: ["*.jar"]) 29 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | implementation "com.android.support:appcompat-v7:27.1.1" 31 | implementation "com.android.support:recyclerview-v7:27.1.1" 32 | 33 | implementation project(":itemsLib") 34 | kapt project(":itemsProcessor") 35 | } 36 | -------------------------------------------------------------------------------- /exampleApp/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /exampleApp/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/BaseAdapter.java: -------------------------------------------------------------------------------- 1 | package cn.nekocode.items.example; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.RecyclerView; 5 | import cn.nekocode.items.ItemAdapter; 6 | 7 | import java.util.ArrayList; 8 | 9 | public abstract class BaseAdapter extends RecyclerView.Adapter implements ItemAdapter { 10 | private final ArrayList mList = new ArrayList(); 11 | 12 | @NonNull 13 | public ArrayList getList() { 14 | return mList; 15 | } 16 | 17 | @NonNull 18 | @Override 19 | public T getData(int position) { 20 | return (T) mList.get(position); 21 | } 22 | 23 | @Override 24 | public int getItemCount() { 25 | return mList.size(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.nekocode.items.example 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.support.v7.widget.LinearLayoutManager 6 | import android.widget.Toast 7 | import cn.nekocode.items.example.java.HeaderOrFooterData 8 | import cn.nekocode.items.example.kotlin.KtFooterItemView 9 | import cn.nekocode.items.example.kotlin.KtStringItemView 10 | import cn.nekocode.items.example.kotlin.KtTestAdapterImpl 11 | import kotlinx.android.synthetic.main.activity_main.* 12 | 13 | class MainActivity : AppCompatActivity() { 14 | 15 | override fun onCreate(savedInstanceState: Bundle?) { 16 | super.onCreate(savedInstanceState) 17 | setContentView(R.layout.activity_main) 18 | 19 | val adapter = KtTestAdapterImpl() 20 | 21 | // Add data 22 | adapter.list.add(HeaderOrFooterData("Header")) 23 | for (i in 0..10) { 24 | adapter.list.add("Item$i") 25 | } 26 | adapter.list.add(HeaderOrFooterData("Footer", false)) 27 | 28 | // Setup callback for views 29 | adapter.stringItem().setCallback(object : KtStringItemView.Callback { 30 | override fun onButtonClick(data: String?) { 31 | Toast.makeText(this@MainActivity, data, Toast.LENGTH_SHORT).show() 32 | } 33 | }) 34 | adapter.footerItem().setCallback(object : KtFooterItemView.Callback() { 35 | override fun onCheckedChanged(data: HeaderOrFooterData) { 36 | Toast.makeText(this@MainActivity, data.isChecked.toString(), Toast.LENGTH_SHORT).show() 37 | } 38 | }) 39 | 40 | // Setup the recycler view 41 | recyclerView.layoutManager = LinearLayoutManager(this) 42 | recyclerView.adapter = adapter 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/java/FooterItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018. nekocode (nekocode.cn@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cn.nekocode.items.example.java; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.v7.widget.RecyclerView; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.CheckBox; 25 | import android.widget.TextView; 26 | import cn.nekocode.items.BaseItem; 27 | import cn.nekocode.items.ItemAdapter; 28 | import cn.nekocode.items.example.R; 29 | 30 | /** 31 | * @author nekocode (nekocode.cn@gmail.com) 32 | */ 33 | public class FooterItem extends BaseItem { 34 | 35 | public FooterItem(ItemAdapter adapter, int viewType) { 36 | super(adapter, viewType); 37 | } 38 | 39 | @NonNull 40 | @Override 41 | public Holder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) { 42 | final View itemView = inflater.inflate(R.layout.item_footer, parent, false); 43 | final Holder holder = new Holder(itemView); 44 | holder.textView = itemView.findViewById(R.id.textView); 45 | holder.checkBox = itemView.findViewById(R.id.checkBox); 46 | holder.checkBox.setOnCheckedChangeListener((compoundButton, b) -> { 47 | holder.data.setChecked(b); 48 | if (getCallback() != null) { 49 | getCallback().onCheckedChanged(holder.data); 50 | } 51 | }); 52 | return holder; 53 | } 54 | 55 | @Override 56 | public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull HeaderOrFooterData data) { 57 | holder.data = data; 58 | holder.textView.setText(data.getText()); 59 | holder.checkBox.setChecked(data.isChecked()); 60 | } 61 | 62 | 63 | static class Holder extends RecyclerView.ViewHolder { 64 | private TextView textView; 65 | private CheckBox checkBox; 66 | private HeaderOrFooterData data; 67 | 68 | Holder(View itemView) { 69 | super(itemView); 70 | textView = itemView.findViewById(R.id.textView); 71 | } 72 | } 73 | 74 | public static class Callback { 75 | public void onCheckedChanged(HeaderOrFooterData data) { 76 | // do nothing 77 | } 78 | public void otherMethod() { 79 | // do nothing 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/java/HeaderItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018. nekocode (nekocode.cn@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cn.nekocode.items.example.java; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.v7.widget.RecyclerView; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.TextView; 25 | import cn.nekocode.items.BaseItem; 26 | import cn.nekocode.items.ItemAdapter; 27 | import cn.nekocode.items.example.R; 28 | 29 | /** 30 | * @author nekocode (nekocode.cn@gmail.com) 31 | */ 32 | public class HeaderItem extends BaseItem { 33 | 34 | public HeaderItem(ItemAdapter adapter, int viewType) { 35 | super(adapter, viewType); 36 | } 37 | 38 | @NonNull 39 | @Override 40 | public Holder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) { 41 | return new Holder(inflater.inflate(R.layout.item_header, parent, false)); 42 | } 43 | 44 | @Override 45 | public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull HeaderOrFooterData data) { 46 | holder.textView.setText(data.getText()); 47 | } 48 | 49 | static class Holder extends RecyclerView.ViewHolder { 50 | TextView textView; 51 | 52 | Holder(View itemView) { 53 | super(itemView); 54 | textView = itemView.findViewById(R.id.textView); 55 | } 56 | } 57 | 58 | public interface Callback { 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/java/HeaderOrFooterData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018. nekocode (nekocode.cn@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cn.nekocode.items.example.java; 18 | 19 | /** 20 | * @author nekocode (nekocode.cn@gmail.com) 21 | */ 22 | public class HeaderOrFooterData { 23 | private String text; 24 | private boolean isHeader; 25 | private boolean isChecked; 26 | 27 | public HeaderOrFooterData(String text) { 28 | this.text = text; 29 | this.isHeader = true; 30 | } 31 | 32 | public HeaderOrFooterData(String text, boolean isChecked) { 33 | this.text = text; 34 | this.isHeader = false; 35 | this.isChecked = isChecked; 36 | } 37 | 38 | public String getText() { 39 | return text; 40 | } 41 | 42 | public void setText(String text) { 43 | this.text = text; 44 | } 45 | 46 | public boolean isHeader() { 47 | return isHeader; 48 | } 49 | 50 | public void setHeader(boolean header) { 51 | isHeader = header; 52 | } 53 | 54 | public boolean isChecked() { 55 | return isChecked; 56 | } 57 | 58 | public void setChecked(boolean checked) { 59 | isChecked = checked; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/java/StringItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018. nekocode (nekocode.cn@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cn.nekocode.items.example.java; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.v7.widget.RecyclerView; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.Button; 25 | import android.widget.TextView; 26 | import cn.nekocode.items.BaseItem; 27 | import cn.nekocode.items.ItemAdapter; 28 | import cn.nekocode.items.example.R; 29 | 30 | /** 31 | * @author nekocode (nekocode.cn@gmail.com) 32 | */ 33 | public class StringItem extends BaseItem { 34 | 35 | public StringItem(ItemAdapter adapter, int viewType) { 36 | super(adapter, viewType); 37 | } 38 | 39 | @NonNull 40 | @Override 41 | public Holder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) { 42 | final View itemView = inflater.inflate(R.layout.item_string, parent, false); 43 | final Holder holder = new Holder(itemView); 44 | holder.button.setOnClickListener(v -> { 45 | if (getCallback() != null) { 46 | getCallback().onButtonClick(holder.data); 47 | } 48 | }); 49 | return holder; 50 | } 51 | 52 | @Override 53 | public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull String data) { 54 | holder.data = data; 55 | holder.textView.setText(data); 56 | } 57 | 58 | static class Holder extends RecyclerView.ViewHolder { 59 | private TextView textView; 60 | private Button button; 61 | private String data; 62 | 63 | Holder(View itemView) { 64 | super(itemView); 65 | textView = itemView.findViewById(R.id.textView); 66 | button = itemView.findViewById(R.id.button); 67 | } 68 | } 69 | 70 | public interface Callback { 71 | void onButtonClick(@NonNull String data); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/java/TestAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018. nekocode (nekocode.cn@gmail.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cn.nekocode.items.example.java; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.v7.widget.RecyclerView; 21 | import cn.nekocode.items.ItemAdapter; 22 | import cn.nekocode.items.annotation.AdapterClass; 23 | import cn.nekocode.items.annotation.ItemMethod; 24 | import cn.nekocode.items.annotation.SelectorMethod; 25 | 26 | import java.util.LinkedList; 27 | 28 | /** 29 | * @author nekocode (nekocode.cn@gmail.com) 30 | */ 31 | @AdapterClass 32 | public abstract class TestAdapter extends RecyclerView.Adapter implements ItemAdapter { 33 | private final LinkedList mList = new LinkedList(); 34 | 35 | @NonNull 36 | public LinkedList getList() { 37 | return mList; 38 | } 39 | 40 | @Override 41 | public int getItemCount() { 42 | return mList.size(); 43 | } 44 | 45 | @NonNull 46 | @Override 47 | public T getData(int position) { 48 | return (T) mList.get(position); 49 | } 50 | 51 | @NonNull 52 | @ItemMethod 53 | public abstract HeaderItem headerItem(); 54 | 55 | @NonNull 56 | @ItemMethod 57 | public abstract StringItem stringItem(); 58 | 59 | @NonNull 60 | @ItemMethod 61 | public abstract FooterItem footerItem(); 62 | 63 | @SelectorMethod 64 | public int viewForHeaderOrFooter(int position, @NonNull HeaderOrFooterData data) { 65 | if (data.isHeader()) { 66 | return headerItem().getViewType(); 67 | } else { 68 | return footerItem().getViewType(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/kotlin/KtFooterItemView.kt: -------------------------------------------------------------------------------- 1 | package cn.nekocode.items.example.kotlin 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import cn.nekocode.items.BaseItem 8 | import cn.nekocode.items.ItemAdapter 9 | import cn.nekocode.items.example.R 10 | import cn.nekocode.items.example.java.HeaderOrFooterData 11 | import kotlinx.android.synthetic.main.item_footer.view.* 12 | 13 | class KtFooterItemView(adapter: ItemAdapter, viewType: Int) : 14 | BaseItem(adapter, viewType) { 15 | 16 | override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): Holder { 17 | val itemView = inflater.inflate(R.layout.item_footer, parent, false) 18 | val holder = Holder(itemView) 19 | itemView.checkBox.setOnCheckedChangeListener { _, b -> 20 | holder.data?.also { 21 | it.isChecked = b 22 | callback?.onCheckedChanged(it) 23 | } 24 | } 25 | return holder 26 | } 27 | 28 | override fun onBindViewHolder(holder: Holder, position: Int, data: HeaderOrFooterData) { 29 | holder.data = data 30 | holder.itemView.textView.text = data.text 31 | holder.itemView.checkBox.isChecked = data.isChecked 32 | } 33 | 34 | class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) { 35 | var data: HeaderOrFooterData? = null 36 | } 37 | 38 | open class Callback { 39 | open fun onCheckedChanged(data: HeaderOrFooterData) { 40 | // do nothing 41 | } 42 | 43 | open fun otherMethod() { 44 | // do nothing 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/kotlin/KtHeaderItemView.kt: -------------------------------------------------------------------------------- 1 | package cn.nekocode.items.example.kotlin 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import cn.nekocode.items.BaseItem 8 | import cn.nekocode.items.ItemAdapter 9 | import cn.nekocode.items.example.R 10 | import cn.nekocode.items.example.java.HeaderOrFooterData 11 | import kotlinx.android.synthetic.main.item_header.view.* 12 | 13 | class KtHeaderItemView(adapter: ItemAdapter, viewType: Int) : 14 | BaseItem(adapter, viewType) { 15 | 16 | override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): Holder { 17 | val itemView = inflater.inflate(R.layout.item_header, parent, false) 18 | return Holder(itemView) 19 | } 20 | 21 | override fun onBindViewHolder(holder: Holder, position: Int, data: HeaderOrFooterData) { 22 | holder.itemView.textView.text = data.text 23 | } 24 | 25 | class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) 26 | 27 | interface Callback 28 | } -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/kotlin/KtStringItemView.kt: -------------------------------------------------------------------------------- 1 | package cn.nekocode.items.example.kotlin 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import cn.nekocode.items.BaseItem 8 | import cn.nekocode.items.ItemAdapter 9 | import cn.nekocode.items.example.R 10 | import kotlinx.android.synthetic.main.item_string.view.* 11 | 12 | class KtStringItemView(adapter: ItemAdapter, viewType: Int) : 13 | BaseItem(adapter, viewType) { 14 | 15 | override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): Holder { 16 | val itemView = inflater.inflate(R.layout.item_string, parent, false) 17 | val holder = Holder(itemView) 18 | itemView.button.setOnClickListener { 19 | callback?.onButtonClick(holder.data) 20 | } 21 | return holder 22 | } 23 | 24 | override fun onBindViewHolder(holder: Holder, position: Int, data: String) { 25 | holder.data = data 26 | holder.itemView.textView.text = data 27 | } 28 | 29 | class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) { 30 | var data: String? = null 31 | } 32 | 33 | interface Callback { 34 | fun onButtonClick(data: String?) 35 | } 36 | } -------------------------------------------------------------------------------- /exampleApp/src/main/java/cn/nekocode/items/example/kotlin/KtTestAdapter.kt: -------------------------------------------------------------------------------- 1 | package cn.nekocode.items.example.kotlin 2 | 3 | import cn.nekocode.items.annotation.AdapterClass 4 | import cn.nekocode.items.annotation.ItemMethod 5 | import cn.nekocode.items.annotation.SelectorMethod 6 | import cn.nekocode.items.example.BaseAdapter 7 | import cn.nekocode.items.example.java.HeaderOrFooterData 8 | 9 | @AdapterClass 10 | abstract class KtTestAdapter : BaseAdapter() { 11 | 12 | @ItemMethod 13 | abstract fun headerItem(): KtHeaderItemView 14 | 15 | @ItemMethod 16 | abstract fun stringItem(): KtStringItemView 17 | 18 | @ItemMethod 19 | abstract fun footerItem(): KtFooterItemView 20 | 21 | @SelectorMethod 22 | fun viewForHeaderOrFooter(position: Int, data: HeaderOrFooterData) = if (data.isHeader) { 23 | headerItem().viewType 24 | } else { 25 | footerItem().viewType 26 | } 27 | } -------------------------------------------------------------------------------- /exampleApp/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /exampleApp/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /exampleApp/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | -------------------------------------------------------------------------------- /exampleApp/src/main/res/layout/item_footer.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 21 | 22 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /exampleApp/src/main/res/layout/item_header.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /exampleApp/src/main/res/layout/item_string.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 20 | 21 |