├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ └── styles.xml │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ ├── drawable │ │ │ └── ic_delete.xml │ │ └── layout │ │ │ ├── activity_main.xml │ │ │ └── item_footballer.xml │ │ ├── java │ │ └── com │ │ │ └── zuluft │ │ │ └── giodz │ │ │ └── autorendereradaptersample │ │ │ ├── models │ │ │ └── FootballerModel.java │ │ │ ├── simpleSample │ │ │ ├── renderers │ │ │ │ └── FootballerRenderer.java │ │ │ └── SimpleSampleActivity.java │ │ │ └── sortedAutoAdapterSample │ │ │ ├── renderers │ │ │ └── FootballerOrderableRenderer.java │ │ │ └── SortedAutoAdapterSampleActivity.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── autoAdapter ├── .gitignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── java │ │ └── com │ │ │ └── zuluft │ │ │ └── autoadapter │ │ │ ├── renderables │ │ │ ├── IRenderer.java │ │ │ ├── Renderer.java │ │ │ ├── OrderableRenderer.java │ │ │ └── AutoViewHolder.java │ │ │ ├── listeners │ │ │ ├── ViewHolderCreationListener.java │ │ │ └── ItemInfo.java │ │ │ ├── factories │ │ │ └── AutoViewHolderFactory.java │ │ │ ├── structures │ │ │ ├── IAdapter.java │ │ │ ├── ISortedAdapter.java │ │ │ └── SortedAdapterDataStructure.java │ │ │ ├── AutoAdapter.java │ │ │ ├── SortedAutoAdapter.java │ │ │ └── BaseAutoAdapter.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── processor ├── .gitignore ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── services │ │ │ └── javax.annotation.processing.Processor │ │ └── java │ │ └── com │ │ └── zuluft │ │ └── autoAdapterAnnotationsProcessor │ │ ├── ViewInfo.java │ │ ├── ViewHolderInfo.java │ │ ├── ClassNameHelper.java │ │ └── Processor.java └── build.gradle ├── autoAdapterAnnotations ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── zuluft │ └── autoadapterannotations │ ├── ViewField.java │ └── Render.java ├── gradle.properties ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── vcs.xml ├── runConfigurations.xml ├── modules.xml ├── gradle.xml └── misc.xml ├── gradlew.bat ├── gradlew ├── LICENSE └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /autoAdapter/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /processor/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /autoAdapterAnnotations/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536m 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':autoAdapter', ':autoAdapterAnnotations', ':processor' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zuluft/AutoAdapter/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /autoAdapter/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AutoRendererAdapter 3 | 4 | -------------------------------------------------------------------------------- /processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | com.zuluft.autoAdapterAnnotationsProcessor.Processor -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zuluft/AutoAdapter/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zuluft/AutoAdapter/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zuluft/AutoAdapter/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zuluft/AutoAdapter/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zuluft/AutoAdapter/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.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/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AutoRendererAdapterSample 3 | 4 | Number: %1$d 5 | 6 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/renderables/IRenderer.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.renderables; 2 | 3 | public interface IRenderer { 4 | 5 | void apply(T viewHolder); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /autoAdapterAnnotations/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group='com.github.Zuluft' 5 | 6 | dependencies { 7 | implementation fileTree(dir: 'libs', include: ['*.jar']) 8 | } 9 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/renderables/Renderer.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.renderables; 2 | 3 | @SuppressWarnings("unused") 4 | public abstract class Renderer implements IRenderer { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jul 18 15:43:11 GET 2018 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-4.6-all.zip 7 | -------------------------------------------------------------------------------- /autoAdapterAnnotations/src/main/java/com/zuluft/autoadapterannotations/ViewField.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapterannotations; 2 | 3 | 4 | /** 5 | * Created by giodz on 9/20/2017. 6 | */ 7 | 8 | public @interface ViewField { 9 | int id(); 10 | 11 | String name(); 12 | 13 | Class type(); 14 | } 15 | -------------------------------------------------------------------------------- /autoAdapter/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /processor/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group='com.github.Zuluft' 5 | 6 | dependencies { 7 | implementation fileTree(dir: 'libs', include: ['*.jar']) 8 | compile project(":autoAdapterAnnotations") 9 | compile 'com.squareup:javapoet:1.9.0' 10 | } 11 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/listeners/ViewHolderCreationListener.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.listeners; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.zuluft.autoadapter.renderables.AutoViewHolder; 6 | 7 | public interface ViewHolderCreationListener { 8 | 9 | void onViewHolderCreated(@NonNull AutoViewHolder autoViewHolder); 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_delete.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | -------------------------------------------------------------------------------- /autoAdapterAnnotations/src/main/java/com/zuluft/autoadapterannotations/Render.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapterannotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface Render { 11 | int layout(); 12 | 13 | ViewField[] views() default {}; 14 | } 15 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /processor/src/main/java/com/zuluft/autoAdapterAnnotationsProcessor/ViewInfo.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoAdapterAnnotationsProcessor; 2 | 3 | import javax.lang.model.element.TypeElement; 4 | 5 | 6 | @SuppressWarnings("WeakerAccess") 7 | public class ViewInfo { 8 | public final int id; 9 | public final String name; 10 | public final TypeElement canonicalName; 11 | 12 | 13 | public ViewInfo(final int id, 14 | final String name, 15 | final TypeElement canonicalName) { 16 | this.id = id; 17 | this.name = name; 18 | this.canonicalName = canonicalName; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/listeners/ItemInfo.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.listeners; 2 | 3 | 4 | import com.zuluft.autoadapter.renderables.IRenderer; 5 | import com.zuluft.autoadapter.renderables.AutoViewHolder; 6 | 7 | 8 | @SuppressWarnings("WeakerAccess") 9 | public final class ItemInfo { 10 | public final int position; 11 | public final T renderer; 12 | public final V viewHolder; 13 | 14 | public ItemInfo(int position, T renderer, V viewHolder) { 15 | this.position = position; 16 | this.renderer = renderer; 17 | this.viewHolder = viewHolder; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\giodz\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /autoAdapter/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\giodz\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/factories/AutoViewHolderFactory.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.factories; 2 | 3 | import android.view.LayoutInflater; 4 | import android.view.View; 5 | import android.view.ViewGroup; 6 | 7 | import com.zuluft.autoadapter.renderables.AutoViewHolder; 8 | import com.zuluft.autoadapter.renderables.IRenderer; 9 | 10 | 11 | public abstract class AutoViewHolderFactory { 12 | 13 | public abstract int getLayoutId(IRenderer renderer); 14 | 15 | public abstract AutoViewHolder createViewHolder(ViewGroup parent, int layoutId); 16 | 17 | protected final View createView(ViewGroup parent, int layoutId) { 18 | return LayoutInflater.from(parent.getContext()) 19 | .inflate(layoutId, parent, false); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/zuluft/giodz/autorendereradaptersample/models/FootballerModel.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.giodz.autorendereradaptersample.models; 2 | 3 | 4 | public final class FootballerModel { 5 | private final String name; 6 | private final int number; 7 | private final String club; 8 | 9 | public FootballerModel(final String name, 10 | final int number, 11 | final String club) { 12 | this.name = name; 13 | this.number = number; 14 | this.club = club; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public int getNumber() { 22 | return number; 23 | } 24 | 25 | public String getClub() { 26 | return club; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/structures/IAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.structures; 2 | 3 | 4 | import android.support.annotation.NonNull; 5 | import com.zuluft.autoadapter.renderables.IRenderer; 6 | 7 | import java.util.List; 8 | 9 | @SuppressWarnings({"unchecked", "unused"}) 10 | public interface IAdapter { 11 | 12 | 13 | void add(@NonNull T item); 14 | 15 | void addAll(@NonNull T... items); 16 | 17 | void addAll(@NonNull List items); 18 | 19 | void remove(int position); 20 | 21 | void remove(@NonNull T item); 22 | 23 | void removeAll(); 24 | 25 | void update(int position, @NonNull T newItem); 26 | 27 | int indexOf(@NonNull T item); 28 | 29 | T getItem(int position); 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/structures/ISortedAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.structures; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.zuluft.autoadapter.renderables.OrderableRenderer; 6 | 7 | import java.util.List; 8 | 9 | @SuppressWarnings("unused") 10 | public interface ISortedAdapter { 11 | 12 | int compare(@NonNull OrderableRenderer item1, 13 | @NonNull OrderableRenderer item2); 14 | 15 | boolean areContentsTheSame(@NonNull OrderableRenderer item1, 16 | @NonNull OrderableRenderer item2); 17 | 18 | boolean areItemsTheSame(@NonNull OrderableRenderer item1, 19 | @NonNull OrderableRenderer item2); 20 | 21 | void beginUpdate(); 22 | 23 | void commitUpdate(); 24 | 25 | void updateAll(List list); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /processor/src/main/java/com/zuluft/autoAdapterAnnotationsProcessor/ViewHolderInfo.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoAdapterAnnotationsProcessor; 2 | 3 | import com.zuluft.autoadapterannotations.ViewField; 4 | 5 | @SuppressWarnings("WeakerAccess") 6 | public class ViewHolderInfo { 7 | public final String name; 8 | public final ViewInfo[] viewInfos; 9 | 10 | public ViewHolderInfo(final String name, 11 | final ViewField[] viewFields) { 12 | this.name = name; 13 | viewInfos = new ViewInfo[viewFields.length]; 14 | for (int i = 0; i < viewInfos.length; i++) { 15 | ViewField viewField = viewFields[i]; 16 | ViewInfo viewInfo = new ViewInfo(viewField.id(), viewField.name(), 17 | ClassNameHelper.getViewClassInfo(viewField)); 18 | viewInfos[i] = viewInfo; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 21 | -------------------------------------------------------------------------------- /autoAdapter/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | group = 'com.github.Zuluft' 5 | 6 | android { 7 | compileSdkVersion 28 8 | buildToolsVersion '28.0.3' 9 | 10 | defaultConfig { 11 | minSdkVersion 14 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'com.android.support:recyclerview-v7:28.0.0' 28 | //rx2 29 | api 'io.reactivex.rxjava2:rxandroid:2.0.2' 30 | api 'io.reactivex.rxjava2:rxjava:2.1.9' 31 | //rx2 bindings 32 | api 'com.jakewharton.rxbinding2:rxbinding:2.0.0' 33 | } -------------------------------------------------------------------------------- /processor/src/main/java/com/zuluft/autoAdapterAnnotationsProcessor/ClassNameHelper.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoAdapterAnnotationsProcessor; 2 | 3 | 4 | import com.zuluft.autoadapterannotations.ViewField; 5 | 6 | import javax.lang.model.element.TypeElement; 7 | import javax.lang.model.type.DeclaredType; 8 | import javax.lang.model.type.MirroredTypeException; 9 | 10 | @SuppressWarnings({"WeakerAccess", "unused"}) 11 | public final class ClassNameHelper { 12 | 13 | public static TypeElement getViewClassInfo(final ViewField viewField) { 14 | TypeElement typeElement = null; 15 | try { 16 | Class type = viewField.type(); 17 | String simpleName = type.getSimpleName(); 18 | String canonicalName = type.getCanonicalName(); 19 | } catch (MirroredTypeException mte) { 20 | DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror(); 21 | typeElement = (TypeElement) classTypeMirror.asElement(); 22 | 23 | } 24 | return typeElement; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | 4 | android { 5 | compileSdkVersion 28 6 | buildToolsVersion '28.0.3' 7 | defaultConfig { 8 | applicationId "com.example.giodz.autorendereradaptersample" 9 | minSdkVersion 14 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | compileOptions { 22 | sourceCompatibility JavaVersion.VERSION_1_8 23 | targetCompatibility JavaVersion.VERSION_1_8 24 | } 25 | 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(include: ['*.jar'], dir: 'libs') 30 | implementation 'com.android.support:appcompat-v7:28.0.0' 31 | implementation 'com.android.support:recyclerview-v7:28.0.0' 32 | implementation 'com.annimon:stream:1.2.1' 33 | implementation project(':autoAdapterAnnotations') 34 | implementation project(':autoAdapter') 35 | annotationProcessor project(':processor') 36 | } 37 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/renderables/OrderableRenderer.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.renderables; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | @SuppressWarnings("WeakerAccess") 6 | public abstract class OrderableRenderer 7 | implements 8 | IRenderer { 9 | 10 | public final int compareOrderIds(@NonNull OrderableRenderer item) { 11 | int result = Integer.valueOf(getOrderId()).compareTo(item.getOrderId()); 12 | if (result == 0) { 13 | return compareTo(item); 14 | } 15 | return result; 16 | } 17 | 18 | public final boolean hasSameOrderIds(@NonNull OrderableRenderer item) { 19 | int result = Integer.valueOf(getOrderId()).compareTo(item.getOrderId()); 20 | return result == 0 && areItemsTheSame(item); 21 | } 22 | 23 | public abstract int compareTo(@NonNull OrderableRenderer item); 24 | 25 | public abstract boolean areContentsTheSame(@NonNull OrderableRenderer item); 26 | 27 | public abstract boolean areItemsTheSame(@NonNull OrderableRenderer item); 28 | 29 | public int getOrderId() { 30 | return -1; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_footballer.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 18 | 19 | 28 | 29 | 38 | 39 | 47 | 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/zuluft/giodz/autorendereradaptersample/simpleSample/renderers/FootballerRenderer.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.giodz.autorendereradaptersample.simpleSample.renderers; 2 | 3 | 4 | import android.content.Context; 5 | import android.widget.TextView; 6 | 7 | import com.zuluft.autoadapter.renderables.Renderer; 8 | import com.zuluft.autoadapterannotations.Render; 9 | import com.zuluft.autoadapterannotations.ViewField; 10 | import com.zuluft.generated.FootballerRendererViewHolder; 11 | import com.zuluft.giodz.autorendereradaptersample.R; 12 | import com.zuluft.giodz.autorendereradaptersample.models.FootballerModel; 13 | 14 | @Render(layout = R.layout.item_footballer, 15 | views = { 16 | @ViewField( 17 | id = R.id.tvName, 18 | name = "tvName", 19 | type = TextView.class 20 | ), 21 | @ViewField( 22 | id = R.id.tvNumber, 23 | name = "tvNumber", 24 | type = TextView.class 25 | ), 26 | @ViewField( 27 | id = R.id.tvClub, 28 | name = "tvClub", 29 | type = TextView.class 30 | ) 31 | }) 32 | public class FootballerRenderer 33 | extends 34 | Renderer { 35 | 36 | public final FootballerModel footballerModel; 37 | 38 | public FootballerRenderer(final FootballerModel footballerModel) { 39 | this.footballerModel = footballerModel; 40 | } 41 | 42 | @Override 43 | public void apply(final FootballerRendererViewHolder vh) { 44 | final Context context = vh.getContext(); 45 | vh.tvName.setText(footballerModel.getName()); 46 | vh.tvClub.setText(footballerModel.getClub()); 47 | vh.tvNumber.setText(context.getString(R.string.footballer_number_template, 48 | footballerModel.getNumber())); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/AutoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter; 2 | 3 | import android.support.annotation.NonNull; 4 | import com.zuluft.autoadapter.factories.AutoViewHolderFactory; 5 | import com.zuluft.autoadapter.renderables.IRenderer; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | public class AutoAdapter 12 | extends 13 | BaseAutoAdapter { 14 | 15 | private final List mRenderers = new ArrayList<>(); 16 | 17 | public AutoAdapter(final @NonNull AutoViewHolderFactory autoViewHolderFactory) { 18 | super(autoViewHolderFactory); 19 | } 20 | 21 | @Override 22 | public int getItemCount() { 23 | return mRenderers.size(); 24 | } 25 | 26 | @Override 27 | public void add(@NonNull final IRenderer item) { 28 | mRenderers.add(item); 29 | } 30 | 31 | @Override 32 | public void addAll(@NonNull final IRenderer[] items) { 33 | mRenderers.addAll(Arrays.asList(items)); 34 | } 35 | 36 | @Override 37 | public void addAll(@NonNull final List items) { 38 | mRenderers.addAll(items); 39 | } 40 | 41 | @Override 42 | public void remove(final int position) { 43 | mRenderers.remove(position); 44 | } 45 | 46 | @Override 47 | public void remove(@NonNull final IRenderer item) { 48 | mRenderers.remove(item); 49 | } 50 | 51 | @Override 52 | public void removeAll() { 53 | mRenderers.clear(); 54 | } 55 | 56 | @Override 57 | public void update(final int position, @NonNull final IRenderer newItem) { 58 | mRenderers.remove(position); 59 | mRenderers.add(position, newItem); 60 | } 61 | 62 | @Override 63 | public int indexOf(@NonNull IRenderer item) { 64 | return mRenderers.indexOf(item); 65 | } 66 | 67 | @Override 68 | public IRenderer getItem(final int position) { 69 | return mRenderers.get(position); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/renderables/AutoViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.renderables; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.IdRes; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.View; 7 | 8 | import com.jakewharton.rxbinding2.view.RxView; 9 | 10 | import io.reactivex.Observable; 11 | import io.reactivex.functions.Function; 12 | 13 | 14 | @SuppressWarnings({"unused", "WeakerAccess"}) 15 | public class AutoViewHolder extends RecyclerView.ViewHolder { 16 | 17 | 18 | public AutoViewHolder(View itemView) { 19 | super(itemView); 20 | 21 | } 22 | 23 | public Observable getViewHolderOnClickObservable() { 24 | return RxView.clicks(itemView).map(new Function() { 25 | @Override 26 | public AutoViewHolder apply(Object view) { 27 | return AutoViewHolder.this; 28 | } 29 | }); 30 | } 31 | 32 | public Observable getViewHolderOnLongClickObservable() { 33 | return RxView.longClicks(itemView).map(new Function() { 34 | @Override 35 | public AutoViewHolder apply(Object o) { 36 | return AutoViewHolder.this; 37 | } 38 | }); 39 | } 40 | 41 | public Observable getViewHolderOnChildLongClickObservable(@IdRes int viewId) { 42 | return RxView.longClicks(itemView.findViewById(viewId)) 43 | .map(new Function() { 44 | @Override 45 | public AutoViewHolder apply(Object o) { 46 | return AutoViewHolder.this; 47 | } 48 | }); 49 | } 50 | 51 | public final Observable getViewHolderOnChildClickObservable(@IdRes int viewId) { 52 | return RxView.clicks(itemView.findViewById(viewId)) 53 | .map(new Function() { 54 | @Override 55 | public AutoViewHolder apply(Object o) { 56 | return AutoViewHolder.this; 57 | } 58 | }); 59 | 60 | } 61 | 62 | public Context getContext() { 63 | return itemView.getContext(); 64 | } 65 | 66 | public View findViewById(int id) { 67 | return itemView.findViewById(id); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/zuluft/giodz/autorendereradaptersample/sortedAutoAdapterSample/renderers/FootballerOrderableRenderer.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.giodz.autorendereradaptersample.sortedAutoAdapterSample.renderers; 2 | 3 | 4 | import android.content.Context; 5 | import android.support.annotation.NonNull; 6 | import android.widget.TextView; 7 | 8 | import com.zuluft.autoadapter.renderables.OrderableRenderer; 9 | import com.zuluft.autoadapterannotations.Render; 10 | import com.zuluft.autoadapterannotations.ViewField; 11 | import com.zuluft.generated.FootballerOrderableRendererViewHolder; 12 | import com.zuluft.giodz.autorendereradaptersample.R; 13 | import com.zuluft.giodz.autorendereradaptersample.models.FootballerModel; 14 | 15 | @Render(layout = R.layout.item_footballer, 16 | views = { 17 | @ViewField( 18 | id = R.id.tvName, 19 | name = "tvName", 20 | type = TextView.class 21 | ), 22 | @ViewField( 23 | id = R.id.tvNumber, 24 | name = "tvNumber", 25 | type = TextView.class 26 | ), 27 | @ViewField( 28 | id = R.id.tvClub, 29 | name = "tvClub", 30 | type = TextView.class 31 | ) 32 | }) 33 | public class FootballerOrderableRenderer 34 | extends 35 | OrderableRenderer { 36 | 37 | public final FootballerModel footballerModel; 38 | 39 | public FootballerOrderableRenderer(final FootballerModel footballerModel) { 40 | this.footballerModel = footballerModel; 41 | } 42 | 43 | @Override 44 | public void apply(final FootballerOrderableRendererViewHolder vh) { 45 | final Context context = vh.getContext(); 46 | vh.tvName.setText(footballerModel.getName()); 47 | vh.tvClub.setText(footballerModel.getClub()); 48 | vh.tvNumber.setText(context.getString(R.string.footballer_number_template, 49 | footballerModel.getNumber())); 50 | } 51 | 52 | @Override 53 | public int compareTo(@NonNull OrderableRenderer item) { 54 | return Integer.valueOf(footballerModel.getNumber()) 55 | .compareTo(getFootballerModel(item).getNumber()); 56 | } 57 | 58 | private FootballerModel getFootballerModel(@NonNull final OrderableRenderer orderableRenderer) { 59 | return ((FootballerOrderableRenderer) orderableRenderer).footballerModel; 60 | } 61 | 62 | @Override 63 | public boolean areContentsTheSame(@NonNull OrderableRenderer item) { 64 | final FootballerModel otherFootballer = getFootballerModel(item); 65 | return footballerModel.getClub().equals(otherFootballer.getClub()) 66 | && footballerModel.getNumber() == otherFootballer.getNumber(); 67 | } 68 | 69 | @Override 70 | public boolean areItemsTheSame(@NonNull OrderableRenderer item) { 71 | return footballerModel.getName().equals(getFootballerModel(item).getName()); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/zuluft/giodz/autorendereradaptersample/simpleSample/SimpleSampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.giodz.autorendereradaptersample.simpleSample; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.DividerItemDecoration; 8 | import android.support.v7.widget.LinearLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.widget.Toast; 11 | 12 | import com.annimon.stream.Collectors; 13 | import com.annimon.stream.Stream; 14 | import com.zuluft.autoadapter.AutoAdapter; 15 | import com.zuluft.generated.AutoAdapterFactory; 16 | import com.zuluft.giodz.autorendereradaptersample.R; 17 | import com.zuluft.giodz.autorendereradaptersample.models.FootballerModel; 18 | import com.zuluft.giodz.autorendereradaptersample.simpleSample.renderers.FootballerRenderer; 19 | 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | @SuppressWarnings("FieldCanBeLocal") 24 | public class SimpleSampleActivity 25 | extends 26 | AppCompatActivity { 27 | 28 | private RecyclerView mRecyclerView; 29 | private AutoAdapter mAutoAdapter; 30 | 31 | @Override 32 | protected void onCreate(@Nullable Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_main); 35 | mRecyclerView = findViewById(R.id.recyclerView); 36 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 37 | mRecyclerView.addItemDecoration(new DividerItemDecoration(this, 38 | LinearLayoutManager.VERTICAL)); 39 | mAutoAdapter = AutoAdapterFactory.createAutoAdapter(); 40 | mAutoAdapter.longClicks(FootballerRenderer.class) 41 | .map(itemInfo -> itemInfo.renderer) 42 | .map(renderer -> renderer.footballerModel) 43 | .subscribe(footballerModel -> 44 | Toast.makeText(this, 45 | footballerModel.getName(), Toast.LENGTH_LONG) 46 | .show()); 47 | mAutoAdapter.longClicks(FootballerRenderer.class, R.id.ivDelete) 48 | .map(itemInfo -> itemInfo.position) 49 | .subscribe(position -> { 50 | mAutoAdapter.remove(position); 51 | mAutoAdapter.notifyItemRemoved(position); 52 | }); 53 | mAutoAdapter.addAll(Stream.of(getFootballers()).map(FootballerRenderer::new) 54 | .collect(Collectors.toList())); 55 | mRecyclerView.setAdapter(mAutoAdapter); 56 | } 57 | 58 | 59 | private List getFootballers() { 60 | return Arrays.asList( 61 | new FootballerModel("Luis Suarez", 9, "Barcelona"), 62 | new FootballerModel("Leo Messi", 10, "Barcelona"), 63 | new FootballerModel("Ousmane Dembele", 11, "FC Barcelona"), 64 | new FootballerModel("Harry Kane", 9, "Tottenham Hotspur"), 65 | new FootballerModel("Dele Alli", 20, "Tottenham Hotspur"), 66 | new FootballerModel("Alexis Sanchez", 7, "Arsenal") 67 | ); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/zuluft/giodz/autorendereradaptersample/sortedAutoAdapterSample/SortedAutoAdapterSampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.giodz.autorendereradaptersample.sortedAutoAdapterSample; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.DividerItemDecoration; 8 | import android.support.v7.widget.LinearLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.widget.Toast; 11 | 12 | import com.annimon.stream.Collectors; 13 | import com.annimon.stream.Stream; 14 | import com.zuluft.autoadapter.SortedAutoAdapter; 15 | import com.zuluft.generated.AutoAdapterFactory; 16 | import com.zuluft.giodz.autorendereradaptersample.R; 17 | import com.zuluft.giodz.autorendereradaptersample.models.FootballerModel; 18 | import com.zuluft.giodz.autorendereradaptersample.sortedAutoAdapterSample.renderers.FootballerOrderableRenderer; 19 | 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | @SuppressWarnings("FieldCanBeLocal") 24 | public class SortedAutoAdapterSampleActivity 25 | extends 26 | AppCompatActivity { 27 | 28 | private RecyclerView mRecyclerView; 29 | private SortedAutoAdapter mSortedAutoAdapter; 30 | 31 | @Override 32 | protected void onCreate(@Nullable Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_main); 35 | mRecyclerView = findViewById(R.id.recyclerView); 36 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 37 | mRecyclerView.addItemDecoration(new DividerItemDecoration(this, 38 | LinearLayoutManager.VERTICAL)); 39 | mSortedAutoAdapter = AutoAdapterFactory.createSortedAutoAdapter(); 40 | mSortedAutoAdapter.clicks(FootballerOrderableRenderer.class) 41 | .map(itemInfo -> itemInfo.renderer) 42 | .map(renderer -> renderer.footballerModel) 43 | .subscribe(footballerModel -> 44 | Toast.makeText(this, 45 | footballerModel.getName(), Toast.LENGTH_LONG) 46 | .show()); 47 | mSortedAutoAdapter.clicks(FootballerOrderableRenderer.class, R.id.ivDelete) 48 | .map(itemInfo -> itemInfo.position) 49 | .subscribe(position -> 50 | mSortedAutoAdapter.remove(position)); 51 | mSortedAutoAdapter.updateAll(Stream.of(getFootballers()) 52 | .map(FootballerOrderableRenderer::new) 53 | .collect(Collectors.toList())); 54 | mRecyclerView.setAdapter(mSortedAutoAdapter); 55 | } 56 | 57 | private List getFootballers() { 58 | return Arrays.asList( 59 | new FootballerModel("Luis Suarez", 9, "Barcelona"), 60 | new FootballerModel("Leo Messi", 10, "Barcelona"), 61 | new FootballerModel("Ousmane Dembele", 11, "FC Barcelona"), 62 | new FootballerModel("Harry Kane", 9, "Tottenham Hotspur"), 63 | new FootballerModel("Dele Alli", 20, "Tottenham Hotspur"), 64 | new FootballerModel("Alexis Sanchez", 7, "Arsenal") 65 | ); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/SortedAutoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.annotation.Nullable; 5 | 6 | import com.zuluft.autoadapter.factories.AutoViewHolderFactory; 7 | import com.zuluft.autoadapter.renderables.OrderableRenderer; 8 | import com.zuluft.autoadapter.structures.ISortedAdapter; 9 | import com.zuluft.autoadapter.structures.SortedAdapterDataStructure; 10 | 11 | import java.util.Arrays; 12 | import java.util.List; 13 | 14 | public class SortedAutoAdapter 15 | extends 16 | BaseAutoAdapter 17 | implements 18 | ISortedAdapter { 19 | 20 | private final SortedAdapterDataStructure mSortedAdapterDataStructure; 21 | 22 | public SortedAutoAdapter(@NonNull final AutoViewHolderFactory autoViewHolderFactory) { 23 | super(autoViewHolderFactory); 24 | mSortedAdapterDataStructure = new SortedAdapterDataStructure(this); 25 | } 26 | 27 | @Override 28 | public int getItemCount() { 29 | return mSortedAdapterDataStructure.size(); 30 | } 31 | 32 | @Override 33 | public int compare(@NonNull final OrderableRenderer item1, 34 | @NonNull final OrderableRenderer item2) { 35 | return item1.compareOrderIds(item2); 36 | } 37 | 38 | @Override 39 | public boolean areContentsTheSame(@NonNull final OrderableRenderer item1, 40 | @NonNull final OrderableRenderer item2) { 41 | return item1.areContentsTheSame(item2); 42 | } 43 | 44 | @Override 45 | public boolean areItemsTheSame(@NonNull final OrderableRenderer item1, 46 | @NonNull final OrderableRenderer item2) { 47 | return item1.hasSameOrderIds(item2); 48 | } 49 | 50 | @Override 51 | public void beginUpdate() { 52 | mSortedAdapterDataStructure.beginBatchedUpdates(); 53 | } 54 | 55 | @Override 56 | public void commitUpdate() { 57 | mSortedAdapterDataStructure.endBatchedUpdates(); 58 | } 59 | 60 | @Override 61 | public void updateAll(@NonNull final List list) { 62 | mSortedAdapterDataStructure.updateAll(list); 63 | } 64 | 65 | @Override 66 | public void add(@NonNull final OrderableRenderer item) { 67 | mSortedAdapterDataStructure.add(item); 68 | } 69 | 70 | @Override 71 | public void addAll(@NonNull final OrderableRenderer[] items) { 72 | mSortedAdapterDataStructure.addAll(Arrays.asList(items)); 73 | } 74 | 75 | @Override 76 | public void addAll(@NonNull final List items) { 77 | OrderableRenderer[] renderables = new OrderableRenderer[items.size()]; 78 | mSortedAdapterDataStructure.addAll(items.toArray(renderables)); 79 | } 80 | 81 | @Override 82 | public void remove(final int position) { 83 | mSortedAdapterDataStructure.removeItemAt(position); 84 | } 85 | 86 | @Override 87 | public void remove(@NonNull final OrderableRenderer item) { 88 | mSortedAdapterDataStructure.remove(item); 89 | } 90 | 91 | @Override 92 | public void removeAll() { 93 | mSortedAdapterDataStructure.clear(); 94 | } 95 | 96 | @Override 97 | public void update(final int position, @NonNull final OrderableRenderer newItem) { 98 | mSortedAdapterDataStructure.updateItemAt(position, newItem); 99 | } 100 | 101 | @Override 102 | public int indexOf(@NonNull final OrderableRenderer item) { 103 | return mSortedAdapterDataStructure.indexOf(item); 104 | } 105 | 106 | @Nullable 107 | @Override 108 | public OrderableRenderer getItem(final int position) { 109 | return mSortedAdapterDataStructure.get(position); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/structures/SortedAdapterDataStructure.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter.structures; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.util.SortedList; 5 | import android.support.v7.widget.util.SortedListAdapterCallback; 6 | 7 | import com.zuluft.autoadapter.SortedAutoAdapter; 8 | import com.zuluft.autoadapter.renderables.OrderableRenderer; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Collections; 12 | import java.util.Comparator; 13 | import java.util.List; 14 | import java.util.Stack; 15 | 16 | 17 | public class SortedAdapterDataStructure extends SortedList { 18 | 19 | 20 | public SortedAdapterDataStructure(@NonNull final SortedAutoAdapter autoAdapter) { 21 | super(OrderableRenderer.class, new SortedListAdapterCallback(autoAdapter) { 22 | @Override 23 | public int compare(OrderableRenderer o1, OrderableRenderer o2) { 24 | return autoAdapter.compare(o1, o2); 25 | } 26 | 27 | @Override 28 | public boolean areContentsTheSame(OrderableRenderer oldItem, OrderableRenderer newItem) { 29 | return autoAdapter.areContentsTheSame(oldItem, newItem); 30 | } 31 | 32 | @Override 33 | public boolean areItemsTheSame(OrderableRenderer item1, OrderableRenderer item2) { 34 | return autoAdapter.areItemsTheSame(item1, item2); 35 | } 36 | }); 37 | } 38 | 39 | public void updateAll(List list) { 40 | Stack itemsToRemove = new Stack<>(); 41 | SortedAdapterDataStructure oldData = this; 42 | int oldSize = this.size(); 43 | int newSize = list.size(); 44 | ArrayList newData = new ArrayList<>(list); 45 | Collections.sort(newData, new Comparator() { 46 | @Override 47 | public int compare(OrderableRenderer o1, OrderableRenderer o2) { 48 | return o1.compareTo(o2); 49 | } 50 | }); 51 | if (oldSize > 0) { 52 | OrderableRenderer oldItem; 53 | OrderableRenderer newItem; 54 | for (int i = 0; i < oldSize; i++) { 55 | oldItem = oldData.get(i); 56 | boolean needRemove = true; 57 | for (int j = 0; j < newSize; j++) { 58 | newItem = newData.get(j); 59 | if (oldItem.areItemsTheSame(newItem)) { 60 | needRemove = false; 61 | break; 62 | } 63 | } 64 | if (needRemove) { 65 | itemsToRemove.push(oldItem); 66 | } 67 | } 68 | } 69 | oldData.beginBatchedUpdates(); 70 | while (!itemsToRemove.empty()) { 71 | oldData.remove(itemsToRemove.pop()); 72 | } 73 | OrderableRenderer item; 74 | int oldIndex, newIndex; 75 | for (int i = 0; i < newSize; i++) { 76 | item = newData.get(i); 77 | newIndex = i; 78 | oldIndex = findSameItem(item); 79 | if (oldIndex > SortedList.INVALID_POSITION) { 80 | oldData.updateItemAt(oldIndex, item); 81 | if (oldIndex != newIndex) { 82 | oldData.recalculatePositionOfItemAt(oldIndex); 83 | } 84 | } else { 85 | oldData.add(item); 86 | } 87 | } 88 | oldData.endBatchedUpdates(); 89 | } 90 | 91 | private int findSameItem(OrderableRenderer item) { 92 | for (int pos = 0, size = this.size(); pos < size; pos++) { 93 | if (this.get(pos).areItemsTheSame(item)) { 94 | return pos; 95 | } 96 | } 97 | return INVALID_POSITION; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save ( ) { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 2016 George Dzotsenidze 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 | -------------------------------------------------------------------------------- /processor/src/main/java/com/zuluft/autoAdapterAnnotationsProcessor/Processor.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoAdapterAnnotationsProcessor; 2 | 3 | import com.squareup.javapoet.ClassName; 4 | import com.squareup.javapoet.FieldSpec; 5 | import com.squareup.javapoet.JavaFile; 6 | import com.squareup.javapoet.MethodSpec; 7 | import com.squareup.javapoet.ParameterSpec; 8 | import com.squareup.javapoet.TypeName; 9 | import com.squareup.javapoet.TypeSpec; 10 | import com.zuluft.autoadapterannotations.Render; 11 | 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | import java.util.Arrays; 15 | import java.util.HashMap; 16 | import java.util.HashSet; 17 | import java.util.List; 18 | import java.util.Map; 19 | import java.util.Set; 20 | 21 | import javax.annotation.processing.AbstractProcessor; 22 | import javax.annotation.processing.Filer; 23 | import javax.annotation.processing.Messager; 24 | import javax.annotation.processing.ProcessingEnvironment; 25 | import javax.annotation.processing.RoundEnvironment; 26 | import javax.lang.model.SourceVersion; 27 | import javax.lang.model.element.Element; 28 | import javax.lang.model.element.ElementKind; 29 | import javax.lang.model.element.Modifier; 30 | import javax.lang.model.element.TypeElement; 31 | import javax.tools.Diagnostic; 32 | 33 | @SuppressWarnings("UnusedReturnValue") 34 | public class Processor extends AbstractProcessor { 35 | 36 | private static final String VIEW_HOLDER_SUFFIX = "ViewHolder"; 37 | private static final String GENERATED_CLASSES_PACKAGE_NAME = "com.zuluft.generated"; 38 | private static final String GENERATED_VIEW_HOLDER_FACTORY_CLASS_NAME = "ViewHolderFactoryImpl"; 39 | 40 | private static final ClassName sViewHolderSuperClassName = 41 | ClassName.get("com.zuluft.autoadapter.renderables", 42 | "AutoViewHolder"); 43 | private static final ClassName sViewHolderFactorySuperClassName = 44 | ClassName.get("com.zuluft.autoadapter.factories", 45 | "AutoViewHolderFactory"); 46 | private static final ClassName sViewClassName = 47 | ClassName.get("android.view", "View"); 48 | private static final ClassName sAutoAdapterClassName = 49 | ClassName.get("com.zuluft.autoadapter", "AutoAdapter"); 50 | private static final ClassName sSortedAutoAdapterClassName = 51 | ClassName.get("com.zuluft.autoadapter", "SortedAutoAdapter"); 52 | private static final ClassName sViewHolderFactoryImplClassName = 53 | ClassName.get(GENERATED_CLASSES_PACKAGE_NAME, GENERATED_VIEW_HOLDER_FACTORY_CLASS_NAME); 54 | 55 | 56 | private Messager mMessager; 57 | private Filer mFiler; 58 | private List mViewHolderInfos; 59 | private Map mRendererLayoutIdMapping; 60 | private Map mLayoutIdViewHolderMapping; 61 | 62 | 63 | @Override 64 | public SourceVersion getSupportedSourceVersion() { 65 | return SourceVersion.latestSupported(); 66 | } 67 | 68 | @Override 69 | public Set getSupportedAnnotationTypes() { 70 | return new HashSet<>(Arrays.asList("com.zuluft.autoadapterannotations.ViewHolderFactory", 71 | "com.zuluft.autoadapterannotations.Render")); 72 | } 73 | 74 | 75 | @Override 76 | public synchronized void init(ProcessingEnvironment processingEnvironment) { 77 | super.init(processingEnvironment); 78 | mMessager = processingEnvironment.getMessager(); 79 | mFiler = processingEnvironment.getFiler(); 80 | mViewHolderInfos = new ArrayList<>(); 81 | mRendererLayoutIdMapping = new HashMap<>(); 82 | mLayoutIdViewHolderMapping = new HashMap<>(); 83 | } 84 | 85 | @Override 86 | public boolean process(Set set, RoundEnvironment roundEnvironment) { 87 | checkAnnotatedElements(roundEnvironment); 88 | generateViewHolderClasses(); 89 | generateViewHolderFactoryClass(); 90 | generateAutoAdapterFactoryClass(); 91 | return false; 92 | } 93 | 94 | private boolean generateAutoAdapterFactoryClass() { 95 | try { 96 | JavaFile.builder(GENERATED_CLASSES_PACKAGE_NAME, 97 | createAutoAdapterFactoryClass()) 98 | .build().writeTo(mFiler); 99 | } catch (IOException e) { 100 | return false; 101 | } 102 | return true; 103 | } 104 | 105 | private TypeSpec createAutoAdapterFactoryClass() { 106 | return TypeSpec.classBuilder("AutoAdapterFactory") 107 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 108 | .addMethod(createGetAutoAdapterStaticMethod()) 109 | .addMethod(createGetSortedAutoAdapterStaticMethod()) 110 | .addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build()) 111 | .build(); 112 | } 113 | 114 | private MethodSpec createGetSortedAutoAdapterStaticMethod() { 115 | return MethodSpec.methodBuilder("createSortedAutoAdapter") 116 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC) 117 | .returns(sSortedAutoAdapterClassName) 118 | .addStatement("return new $L(new $T())", "SortedAutoAdapter", 119 | sViewHolderFactoryImplClassName) 120 | .build(); 121 | } 122 | 123 | private MethodSpec createGetAutoAdapterStaticMethod() { 124 | return MethodSpec.methodBuilder("createAutoAdapter") 125 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC) 126 | .returns(sAutoAdapterClassName) 127 | .addStatement("return new $L(new $T())", "AutoAdapter", 128 | sViewHolderFactoryImplClassName) 129 | .build(); 130 | } 131 | 132 | private boolean generateViewHolderFactoryClass() { 133 | try { 134 | JavaFile.builder(GENERATED_CLASSES_PACKAGE_NAME, 135 | createViewHolderFactoryClass()) 136 | .build().writeTo(mFiler); 137 | } catch (IOException e) { 138 | return false; 139 | } 140 | 141 | return true; 142 | } 143 | 144 | private TypeSpec createViewHolderFactoryClass() { 145 | return TypeSpec.classBuilder(GENERATED_VIEW_HOLDER_FACTORY_CLASS_NAME) 146 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 147 | .superclass(sViewHolderFactorySuperClassName) 148 | .addMethod(createGetLayoutIdMethod()) 149 | .addMethod(createGetViewHolderMethod()) 150 | .addMethod(createViewHolderFactoryConstructor()) 151 | .build(); 152 | } 153 | 154 | private MethodSpec createViewHolderFactoryConstructor() { 155 | return MethodSpec.constructorBuilder() 156 | .addModifiers(Modifier.PUBLIC) 157 | .build(); 158 | } 159 | 160 | private MethodSpec createGetViewHolderMethod() { 161 | MethodSpec.Builder builder = 162 | MethodSpec.methodBuilder("createViewHolder") 163 | .addModifiers(Modifier.PUBLIC) 164 | .addParameter(ParameterSpec.builder(ClassName 165 | .get("android.view", "ViewGroup"), 166 | "parent", Modifier.FINAL).build()) 167 | .addParameter(TypeName.INT, "layoutId", Modifier.FINAL) 168 | .returns(sViewHolderSuperClassName) 169 | .addStatement("$T view=createView($L,$L)", 170 | sViewClassName, "parent", "layoutId"); 171 | for (Map.Entry entry : 172 | mLayoutIdViewHolderMapping.entrySet()) { 173 | builder.beginControlFlow("if($L == $L)", "layoutId", entry.getKey()) 174 | .addStatement("return new $T($L)", entry.getValue(), "view") 175 | .endControlFlow(); 176 | } 177 | builder.addStatement("return null"); 178 | return builder.build(); 179 | } 180 | 181 | private MethodSpec createGetLayoutIdMethod() { 182 | MethodSpec.Builder builder = MethodSpec.methodBuilder("getLayoutId") 183 | .addModifiers(Modifier.PUBLIC) 184 | .addParameter(ParameterSpec.builder( 185 | ClassName.get("com.zuluft.autoadapter.renderables", "IRenderer"), "renderer", Modifier.FINAL) 186 | .build()) 187 | .returns(TypeName.INT); 188 | for (Map.Entry entry : mRendererLayoutIdMapping.entrySet()) { 189 | builder.beginControlFlow("if($L instanceof $T)", 190 | "renderer", entry.getKey()) 191 | .addStatement("return $L", entry.getValue()) 192 | .endControlFlow(); 193 | } 194 | builder.addStatement("return -1"); 195 | return builder.build(); 196 | } 197 | 198 | private TypeSpec createViewHolderClass(ViewHolderInfo viewHolderInfo) { 199 | return TypeSpec 200 | .classBuilder(viewHolderInfo.name) 201 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 202 | .addFields(createViewHolderFields(viewHolderInfo.viewInfos)) 203 | .superclass(sViewHolderSuperClassName) 204 | .addMethod(createConstructorForViewHolder(viewHolderInfo.viewInfos)) 205 | .build(); 206 | } 207 | 208 | private boolean generateViewHolderClasses() { 209 | for (ViewHolderInfo viewHolderInfo : mViewHolderInfos) { 210 | try { 211 | JavaFile.builder(GENERATED_CLASSES_PACKAGE_NAME, 212 | createViewHolderClass(viewHolderInfo)) 213 | .build().writeTo(mFiler); 214 | } catch (IOException e) { 215 | return false; 216 | } 217 | } 218 | return true; 219 | } 220 | 221 | 222 | private Iterable createViewHolderFields(ViewInfo[] viewInfos) { 223 | List fieldSpecs = new ArrayList<>(); 224 | for (ViewInfo viewInfo : viewInfos) { 225 | fieldSpecs.add(FieldSpec.builder(ClassName.get(viewInfo.canonicalName), 226 | viewInfo.name) 227 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 228 | .build()); 229 | } 230 | return fieldSpecs; 231 | } 232 | 233 | 234 | private MethodSpec createConstructorForViewHolder(ViewInfo[] viewInfos) { 235 | MethodSpec.Builder builder = MethodSpec.constructorBuilder() 236 | .addModifiers(Modifier.PUBLIC) 237 | .addParameter(ParameterSpec.builder(sViewClassName, 238 | "itemView", 239 | Modifier.FINAL) 240 | .build()) 241 | .addStatement("super(itemView)"); 242 | 243 | for (ViewInfo viewInfo : viewInfos) { 244 | builder.addStatement("$L=($T)findViewById($L)", viewInfo.name, 245 | ClassName.get(viewInfo.canonicalName), viewInfo.id); 246 | } 247 | return builder.build(); 248 | } 249 | 250 | private boolean checkAnnotatedElements(RoundEnvironment roundEnvironment) { 251 | for (Element element : roundEnvironment.getElementsAnnotatedWith(Render.class)) { 252 | if (element.getKind() != ElementKind.CLASS) { 253 | mMessager.printMessage(Diagnostic.Kind.ERROR, 254 | "Only classes can be annotated with @Render annotation"); 255 | return false; 256 | } 257 | 258 | Render renderAnnotation = element.getAnnotation(Render.class); 259 | String viewHolderClassName = element.getSimpleName().toString() + VIEW_HOLDER_SUFFIX; 260 | mViewHolderInfos.add(new 261 | ViewHolderInfo(viewHolderClassName, 262 | renderAnnotation.views())); 263 | mRendererLayoutIdMapping.put(ClassName.get(element.asType()), 264 | renderAnnotation.layout()); 265 | mLayoutIdViewHolderMapping.put(renderAnnotation.layout(), 266 | ClassName.get(GENERATED_CLASSES_PACKAGE_NAME, viewHolderClassName)); 267 | } 268 | return true; 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /autoAdapter/src/main/java/com/zuluft/autoadapter/BaseAutoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zuluft.autoadapter; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.support.annotation.IdRes; 5 | import android.support.annotation.LayoutRes; 6 | import android.support.annotation.NonNull; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.util.SparseArray; 9 | import android.view.ViewGroup; 10 | 11 | import com.zuluft.autoadapter.factories.AutoViewHolderFactory; 12 | import com.zuluft.autoadapter.listeners.ItemInfo; 13 | import com.zuluft.autoadapter.listeners.ViewHolderCreationListener; 14 | import com.zuluft.autoadapter.renderables.AutoViewHolder; 15 | import com.zuluft.autoadapter.renderables.IRenderer; 16 | import com.zuluft.autoadapter.structures.IAdapter; 17 | 18 | import java.util.ArrayList; 19 | import java.util.HashMap; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | import io.reactivex.Observer; 24 | import io.reactivex.disposables.Disposable; 25 | import io.reactivex.subjects.PublishSubject; 26 | 27 | @SuppressWarnings({"WeakerAccess"}) 28 | public abstract class BaseAutoAdapter 29 | extends 30 | RecyclerView.Adapter 31 | implements 32 | IAdapter { 33 | 34 | private final AutoViewHolderFactory mAutoViewHolderFactory; 35 | 36 | private final Map, PublishSubject> 37 | mItemViewClickBinding = new HashMap<>(); 38 | private final Map, Map> 39 | mChildViewsClickBinding = new HashMap<>(); 40 | 41 | private final Map, PublishSubject> 42 | mItemViewLongClickBinding = new HashMap<>(); 43 | private final Map, Map> 44 | mChildViewsLongClickBinding = new HashMap<>(); 45 | 46 | private final SparseArray> 47 | mLayoutRendererMapping = new SparseArray<>(); 48 | 49 | private final List mViewHolderCreationListeners = new ArrayList<>(); 50 | 51 | public BaseAutoAdapter(final AutoViewHolderFactory autoViewHolderFactory) { 52 | this.mAutoViewHolderFactory = autoViewHolderFactory; 53 | } 54 | 55 | @SuppressWarnings("unused") 56 | public final void addViewHolderCreationListener(@NonNull final ViewHolderCreationListener 57 | viewHolderCreationListener) { 58 | mViewHolderCreationListeners.add(viewHolderCreationListener); 59 | } 60 | 61 | @NonNull 62 | @Override 63 | public AutoViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int layoutId) { 64 | AutoViewHolder viewHolder = mAutoViewHolderFactory.createViewHolder(parent, layoutId); 65 | Class rendererClass = mLayoutRendererMapping.get(layoutId); 66 | final PublishSubject itemLongClickPublishSubject = 67 | mItemViewLongClickBinding.get(rendererClass); 68 | if (itemLongClickPublishSubject != null) { 69 | viewHolder.getViewHolderOnLongClickObservable() 70 | .subscribe(new Observer() { 71 | @Override 72 | public void onSubscribe(Disposable d) { 73 | itemLongClickPublishSubject.onSubscribe(d); 74 | } 75 | 76 | @Override 77 | @SuppressWarnings("unchecked") 78 | public void onNext(AutoViewHolder autoViewHolder) { 79 | int position = autoViewHolder.getAdapterPosition(); 80 | ItemInfo info = 81 | new ItemInfo(position, getItem(position), autoViewHolder); 82 | itemLongClickPublishSubject.onNext(info); 83 | } 84 | 85 | @SuppressWarnings("unchecked") 86 | @Override 87 | public void onError(Throwable e) { 88 | itemLongClickPublishSubject.onNext(e); 89 | } 90 | 91 | @Override 92 | public void onComplete() { 93 | itemLongClickPublishSubject.onComplete(); 94 | } 95 | }); 96 | } 97 | final PublishSubject itemClickPublishSubject = mItemViewClickBinding.get(rendererClass); 98 | if (itemClickPublishSubject != null) { 99 | viewHolder.getViewHolderOnClickObservable() 100 | .subscribe(new Observer() { 101 | @Override 102 | public void onSubscribe(Disposable d) { 103 | itemClickPublishSubject.onSubscribe(d); 104 | } 105 | 106 | @SuppressWarnings("unchecked") 107 | @Override 108 | public void onNext(AutoViewHolder autoViewHolder) { 109 | int position = autoViewHolder.getAdapterPosition(); 110 | ItemInfo info = 111 | new ItemInfo(position, getItem(position), autoViewHolder); 112 | itemClickPublishSubject.onNext(info); 113 | } 114 | 115 | @Override 116 | public void onError(Throwable e) { 117 | itemClickPublishSubject.onError(e); 118 | } 119 | 120 | @Override 121 | public void onComplete() { 122 | itemClickPublishSubject.onComplete(); 123 | } 124 | }); 125 | } 126 | 127 | final Map childViewsLongClickMap = 128 | mChildViewsLongClickBinding.get(rendererClass); 129 | if (childViewsLongClickMap != null) { 130 | for (Map.Entry entry : 131 | childViewsLongClickMap.entrySet()) { 132 | final PublishSubject publishSubject = entry.getValue(); 133 | viewHolder.getViewHolderOnChildLongClickObservable(entry.getKey()) 134 | .subscribe(new Observer() { 135 | @Override 136 | public void onSubscribe(Disposable d) { 137 | publishSubject.onSubscribe(d); 138 | } 139 | 140 | @SuppressWarnings("unchecked") 141 | @Override 142 | public void onNext(AutoViewHolder autoViewHolder) { 143 | int position = autoViewHolder.getAdapterPosition(); 144 | ItemInfo info = 145 | new ItemInfo(position, getItem(position), autoViewHolder); 146 | publishSubject.onNext(info); 147 | } 148 | 149 | @Override 150 | public void onError(Throwable e) { 151 | publishSubject.onError(e); 152 | } 153 | 154 | @Override 155 | public void onComplete() { 156 | publishSubject.onComplete(); 157 | } 158 | }); 159 | } 160 | } 161 | 162 | final Map childViewsClickMap 163 | = mChildViewsClickBinding.get(rendererClass); 164 | if (childViewsClickMap != null) { 165 | for (Map.Entry entry : 166 | childViewsClickMap.entrySet()) { 167 | final PublishSubject publishSubject = entry.getValue(); 168 | viewHolder.getViewHolderOnChildClickObservable(entry.getKey()) 169 | .subscribe(new Observer() { 170 | @Override 171 | public void onSubscribe(Disposable d) { 172 | publishSubject.onSubscribe(d); 173 | } 174 | 175 | @SuppressWarnings("unchecked") 176 | @Override 177 | public void onNext(AutoViewHolder autoViewHolder) { 178 | int position = autoViewHolder.getAdapterPosition(); 179 | ItemInfo info = 180 | new ItemInfo(position, getItem(position), autoViewHolder); 181 | publishSubject.onNext(info); 182 | } 183 | 184 | @Override 185 | public void onError(Throwable e) { 186 | publishSubject.onError(e); 187 | } 188 | 189 | @Override 190 | public void onComplete() { 191 | publishSubject.onComplete(); 192 | } 193 | }); 194 | } 195 | } 196 | for (ViewHolderCreationListener mViewHolderCreationListener : 197 | mViewHolderCreationListeners) { 198 | mViewHolderCreationListener.onViewHolderCreated(viewHolder); 199 | } 200 | return viewHolder; 201 | } 202 | 203 | public final > PublishSubject> 204 | clicks(@NonNull final Class clazz) { 205 | PublishSubject> publishSubject = PublishSubject.create(); 206 | mItemViewClickBinding.put(clazz, publishSubject); 207 | return publishSubject; 208 | } 209 | 210 | @SuppressLint("UseSparseArrays") 211 | public final > PublishSubject> 212 | clicks(@NonNull final Class clazz, @IdRes final int viewId) { 213 | PublishSubject> publishSubject = PublishSubject.create(); 214 | Map map = mChildViewsClickBinding.get(clazz); 215 | if (map == null) { 216 | map = new HashMap<>(); 217 | mChildViewsClickBinding.put(clazz, map); 218 | } 219 | map.put(viewId, publishSubject); 220 | return publishSubject; 221 | } 222 | 223 | @SuppressWarnings("unused") 224 | public final > PublishSubject> 225 | longClicks(@NonNull final Class clazz) { 226 | PublishSubject> publishSubject = PublishSubject.create(); 227 | mItemViewLongClickBinding.put(clazz, publishSubject); 228 | return publishSubject; 229 | } 230 | 231 | 232 | @SuppressWarnings("unused") 233 | @SuppressLint("UseSparseArrays") 234 | public final > PublishSubject> 235 | longClicks(@NonNull final Class clazz, @IdRes final int viewId) { 236 | PublishSubject> publishSubject = PublishSubject.create(); 237 | Map map = mChildViewsLongClickBinding.get(clazz); 238 | if (map == null) { 239 | map = new HashMap<>(); 240 | mChildViewsLongClickBinding.put(clazz, map); 241 | } 242 | map.put(viewId, publishSubject); 243 | return publishSubject; 244 | } 245 | 246 | 247 | @SuppressWarnings("unchecked") 248 | @Override 249 | public void onBindViewHolder(@NonNull final AutoViewHolder holder, 250 | final int position) { 251 | getItem(position).apply(holder); 252 | } 253 | 254 | @Override 255 | @LayoutRes 256 | public int getItemViewType(final int position) { 257 | final T item = getItem(position); 258 | final int layoutId = mAutoViewHolderFactory.getLayoutId(getItem(position)); 259 | if (mLayoutRendererMapping.indexOfKey(layoutId) < 0) { 260 | mLayoutRendererMapping.put(layoutId, item.getClass()); 261 | } 262 | return layoutId; 263 | } 264 | } 265 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoAdapter 2 | 3 | [![Release](https://jitpack.io/v/Zuluft/AutoAdapter.svg)](https://jitpack.io/#Zuluft/AutoAdapter) 4 | [![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-AutoAdapter-green.svg?style=flat )]( https://android-arsenal.com/details/1/6470 ) 5 | [![](https://jitpack.io/v/Zuluft/AutoAdapter/month.svg)](https://jitpack.io/#Zuluft/AutoAdapter) 6 | 7 | 8 | This Repository simplifies working with RecyclerView Adapter 9 | 10 | ## Gradle: 11 | Add it in your root build.gradle at the end of repositories: 12 | ```Groovy 13 | allprojects { 14 | repositories { 15 | ... 16 | maven { url 'https://jitpack.io' } 17 | } 18 | } 19 | ``` 20 | Add dependency to app gradle: 21 | ```Groovy 22 | implementation 'com.github.Zuluft:AutoAdapter:v2.4.1' 23 | annotationProcessor 'com.github.Zuluft:AutoAdapter:v2.4.1' 24 | ``` 25 | 26 | ## Simple Sample: 27 | 28 | ### Step 1: 29 | 30 | Create layout xml file, for example ```item_footballer.xml``` which contains ```TextView```s with ids ```tvName```, ```tvNumber```, ```tvClub``` and ```ImageView``` with id ```ivDelete``` 31 | 32 | ### Step 2 (Optional): 33 | 34 | create model, that you want to be drawn on above created layout: 35 | ```Java 36 | public final class FootballerModel { 37 | private final String name; 38 | private final int number; 39 | private final String club; 40 | 41 | public FootballerModel(final String name, 42 | final int number, 43 | final String club) { 44 | this.name = name; 45 | this.number = number; 46 | this.club = club; 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | 53 | public int getNumber() { 54 | return number; 55 | } 56 | 57 | public String getClub() { 58 | return club; 59 | } 60 | } 61 | ``` 62 | ### Step 3: 63 | 64 | Create 'Renderer' class in the following way: 65 | 66 | ```Java 67 | @Render(layout = R.layout.item_footballer, 68 | views = { 69 | @ViewField( 70 | id = R.id.tvName, 71 | name = "tvName", 72 | type = TextView.class 73 | ), 74 | @ViewField( 75 | id = R.id.tvNumber, 76 | name = "tvNumber", 77 | type = TextView.class 78 | ), 79 | @ViewField( 80 | id = R.id.tvClub, 81 | name = "tvClub", 82 | type = TextView.class 83 | ) 84 | }) 85 | public class FootballerRenderer{ 86 | 87 | } 88 | ``` 89 | ```@Render``` annotation is needed to generate ```ViewHolder``` for this ```Renderer``` by annotation processor. 90 | Inside ```@Render``` annotation ```layout``` value is an itemView layout id and ```@ViewField```s are containing information about the views in this layout. Name of the generated ```ViewHolder``` will be RendererClassName+'ViewHolder', in this case ```FootballerRendererViewHolder``` 91 | 92 | ### Step 4: 93 | 94 | Rebuild the project. Rebuilding generates ViewHolder class (```FootballerRendererViewHolder```), that we use in ```Step 5```. 95 | 96 | ### Step 5: 97 | 98 | Extend your ```FootballerRenderer``` by ```Renderer``` and pass newly generated ```FootballerRendererViewHolder``` as a generic type: 99 | 100 | ```Java 101 | @Render(layout = R.layout.item_footballer, 102 | views = { 103 | @ViewField( 104 | id = R.id.tvName, 105 | name = "tvName", 106 | type = TextView.class 107 | ), 108 | @ViewField( 109 | id = R.id.tvNumber, 110 | name = "tvNumber", 111 | type = TextView.class 112 | ), 113 | @ViewField( 114 | id = R.id.tvClub, 115 | name = "tvClub", 116 | type = TextView.class 117 | ) 118 | }) 119 | public class FootballerRenderer 120 | extends 121 | Renderer { 122 | 123 | public final FootballerModel footballerModel; 124 | 125 | public FootballerRenderer(final FootballerModel footballerModel) { 126 | this.footballerModel = footballerModel; 127 | } 128 | 129 | @Override 130 | public void apply(@NonNull final FootballerRendererViewHolder vh) { 131 | final Context context = vh.getContext(); 132 | vh.tvName.setText(footballerModel.getName()); 133 | vh.tvClub.setText(footballerModel.getClub()); 134 | vh.tvNumber.setText(context.getString(R.string.footballer_number_template, 135 | footballerModel.getNumber())); 136 | } 137 | } 138 | ``` 139 | 140 | As you see generated ```FootballerRendererViewHolder``` has ```tvName```, ```tvClub```, ```tvNumber``` fields. 141 | 142 | ### Step 6: 143 | 144 | ```Java 145 | ... 146 | @Override 147 | protected void onCreate(@Nullable Bundle savedInstanceState) { 148 | ... 149 | mAutoAdapter = AutoAdapterFactory.createAutoAdapter(); 150 | mAutoAdapter.addAll(Stream.of(getFootballers()).map(FootballerRenderer::new) 151 | .collect(Collectors.toList())); 152 | mRecyclerView.setAdapter(mAutoAdapter); 153 | } 154 | 155 | 156 | private List getFootballers() { 157 | return Arrays.asList( 158 | new FootballerModel("Luis Suarez", 9, "Barcelona"), 159 | new FootballerModel("Leo Messi", 10, "Barcelona"), 160 | new FootballerModel("Ousmane Dembele", 11, "Barcelona"), 161 | new FootballerModel("Harry Kane", 9, "Tottenham Hotspur"), 162 | new FootballerModel("Dele Alli", 20, "Tottenham Hotspur"), 163 | new FootballerModel("Alexis Sanchez", 7, "Arsenal") 164 | ); 165 | } 166 | ``` 167 | This line ```Stream.of(getFootballers()).map(FootballerRenderer::new).collect(Collectors.toList())``` converts ```FootballerModel``` to 168 | ```FootballerRenderer``` using [Stream](https://github.com/aNNiMON/Lightweight-Stream-API) 169 | 170 | ### Mmm... What If I want to have heterogeneous items and layouts inside ```RecyclerView``` ? 171 | AutoAdapter's working perfectly with heterogeneous items. 172 | You can add any descedent of ```Renderer``` to ```AutoAdapter```, for example it's not a problem to write the following: 173 | ```Java 174 | ... 175 | mAutoAdapter.add(new FootballerRenderer()); 176 | mAutoAdapter.add(new BasketballerRenderer()); 177 | mAutoAdapter.add(new BoxerRenderer()); 178 | .... 179 | ``` 180 | They all will draw their own layout. 181 | 182 | ### How to add OnClickListener to itemView ? 183 | 184 | ```AutoAdapter``` has ```clicks``` method, it has one required argument ```Renderer class```, one optional argument ```child view id``` and returns ```Rx2 Observable``` with ```ItemInfo``` as generic type. ```ItemInfo``` has 3 public final fields: ```position```, ```renderer```, ```viewHolder```. 185 | 186 | ```Java 187 | ... 188 | mAutoAdapter.clicks(FootballerRenderer.class) 189 | .map(itemInfo -> itemInfo.renderer) 190 | .map(renderer -> renderer.footballerModel) 191 | .subscribe(footballerModel -> 192 | Toast.makeText(this, 193 | footballerModel.getName(), Toast.LENGTH_LONG) 194 | .show()); 195 | ... 196 | ``` 197 | ```Java 198 | ... 199 | mAutoAdapter.clicks(FootballerRenderer.class, R.id.ivDelete) 200 | .map(itemInfo -> itemInfo.position) 201 | .subscribe(position -> { 202 | mAutoAdapter.remove(position); 203 | mAutoAdapter.notifyItemRemoved(position); 204 | }); 205 | ... 206 | ``` 207 | 208 | ## SortedAutoAdapter Sample: 209 | 210 | ```Java 211 | @Render(layout = R.layout.item_footballer, 212 | views = { 213 | @ViewField( 214 | id = R.id.tvName, 215 | name = "tvName", 216 | type = TextView.class 217 | ), 218 | @ViewField( 219 | id = R.id.tvNumber, 220 | name = "tvNumber", 221 | type = TextView.class 222 | ), 223 | @ViewField( 224 | id = R.id.tvClub, 225 | name = "tvClub", 226 | type = TextView.class 227 | ) 228 | }) 229 | public class FootballerOrderableRenderer 230 | extends 231 | OrderableRenderer { 232 | 233 | public final FootballerModel footballerModel; 234 | 235 | public FootballerOrderableRenderer(final FootballerModel footballerModel) { 236 | this.footballerModel = footballerModel; 237 | } 238 | 239 | @Override 240 | public void apply(final FootballerOrderableRendererViewHolder vh) { 241 | final Context context = vh.getContext(); 242 | vh.tvName.setText(footballerModel.getName()); 243 | vh.tvClub.setText(footballerModel.getClub()); 244 | vh.tvNumber.setText(context.getString(R.string.footballer_number_template, 245 | footballerModel.getNumber())); 246 | } 247 | 248 | @Override 249 | public int compareTo(@NonNull OrderableRenderer item) { 250 | return Integer.valueOf(footballerModel.getNumber()) 251 | .compareTo(getFootballerModel(item).getNumber()); 252 | } 253 | 254 | private FootballerModel getFootballerModel(@NonNull final OrderableRenderer orderableRenderer) { 255 | return ((FootballerOrderableRenderer) orderableRenderer).footballerModel; 256 | } 257 | 258 | @Override 259 | public boolean areContentsTheSame(@NonNull OrderableRenderer item) { 260 | final FootballerModel otherFootballer = getFootballerModel(item); 261 | return footballerModel.getClub().equals(otherFootballer.getClub()) 262 | && footballerModel.getNumber() == otherFootballer.getNumber(); 263 | } 264 | 265 | @Override 266 | public boolean areItemsTheSame(@NonNull OrderableRenderer item) { 267 | return footballerModel.getName().equals(getFootballerModel(item).getName()); 268 | } 269 | } 270 | ``` 271 | 272 | ```Java 273 | public class SortedAutoAdapterSampleActivity 274 | extends 275 | AppCompatActivity { 276 | 277 | private RecyclerView mRecyclerView; 278 | private SortedAutoAdapter mSortedAutoAdapter; 279 | 280 | @Override 281 | protected void onCreate(@Nullable Bundle savedInstanceState) { 282 | super.onCreate(savedInstanceState); 283 | setContentView(R.layout.activity_main); 284 | mRecyclerView = findViewById(R.id.recyclerView); 285 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 286 | mRecyclerView.addItemDecoration(new DividerItemDecoration(this, 287 | LinearLayoutManager.VERTICAL)); 288 | mSortedAutoAdapter = AutoAdapterFactory.createSortedAutoAdapter(); 289 | mSortedAutoAdapter.clicks(FootballerOrderableRenderer.class) 290 | .map(itemInfo -> itemInfo.renderer) 291 | .map(renderer -> renderer.footballerModel) 292 | .subscribe(footballerModel -> 293 | Toast.makeText(this, 294 | footballerModel.getName(), Toast.LENGTH_LONG) 295 | .show()); 296 | mSortedAutoAdapter.clicks(FootballerOrderableRenderer.class, R.id.ivDelete) 297 | .map(itemInfo -> itemInfo.position) 298 | .subscribe(position -> 299 | mSortedAutoAdapter.remove(position)); 300 | mSortedAutoAdapter.updateAll(Stream.of(getFootballers()) 301 | .map(FootballerOrderableRenderer::new) 302 | .collect(Collectors.toList())); 303 | mRecyclerView.setAdapter(mSortedAutoAdapter); 304 | } 305 | 306 | private List getFootballers() { 307 | return Arrays.asList( 308 | new FootballerModel("Luis Suarez", 9, "Barcelona"), 309 | new FootballerModel("Leo Messi", 10, "Barcelona"), 310 | new FootballerModel("Ousmane Dembele", 11, "FC Barcelona"), 311 | new FootballerModel("Harry Kane", 9, "Tottenham Hotspur"), 312 | new FootballerModel("Dele Alli", 20, "Tottenham Hotspur"), 313 | new FootballerModel("Alexis Sanchez", 7, "Arsenal") 314 | ); 315 | } 316 | } 317 | ``` 318 | --------------------------------------------------------------------------------