├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── layout
│ │ │ ├── activity_main.xml
│ │ │ ├── layout_bar.xml
│ │ │ └── layout_foo.xml
│ │ ├── java
│ │ └── com
│ │ │ └── zhihu
│ │ │ └── android
│ │ │ └── sugaradapterdemo
│ │ │ ├── holder
│ │ │ ├── BaseHolder.java
│ │ │ ├── FooHolder2.java
│ │ │ ├── FooHolder.java
│ │ │ └── BarHolder.java
│ │ │ ├── item
│ │ │ ├── Bar.java
│ │ │ └── Foo.java
│ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── res
│ │ ├── values
│ │ │ └── strings.xml
│ │ └── layout
│ │ │ └── layout_library.xml
│ │ └── java
│ │ └── com
│ │ └── zhihu
│ │ └── android
│ │ └── sugaradapterlibrary
│ │ ├── item
│ │ └── LibraryItem.java
│ │ └── holder
│ │ └── LibraryHolder.kt
├── proguard-rules.pro
└── build.gradle
├── sugaradapter
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ └── values
│ │ │ └── strings.xml
│ │ ├── java
│ │ └── com
│ │ │ └── zhihu
│ │ │ └── android
│ │ │ └── sugaradapter
│ │ │ ├── InjectDelegate.java
│ │ │ ├── ContainerDelegate.java
│ │ │ ├── Container.java
│ │ │ ├── Sugar.java
│ │ │ ├── PreInflateThread.java
│ │ │ ├── SugarHolder.java
│ │ │ └── SugarAdapter.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── sugaradapter-annotation
├── .gitignore
├── build.gradle
└── src
│ └── main
│ └── java
│ └── com
│ └── zhihu
│ └── android
│ └── sugaradapter
│ ├── Id.java
│ └── Layout.java
├── sugaradapter-processor
├── .gitignore
├── src
│ └── main
│ │ ├── resources
│ │ └── META-INF
│ │ │ └── services
│ │ │ └── javax.annotation.processing.Processor
│ │ └── java
│ │ └── com
│ │ └── zhihu
│ │ └── android
│ │ └── sugaradapter
│ │ ├── Pair.java
│ │ ├── InjectInfo.java
│ │ └── SugarProcessor.java
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle
├── gradle.properties
├── gradlew.bat
├── gradlew
├── README_zh_CN.md
├── README.md
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sugaradapter/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sugaradapter-annotation/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sugaradapter-processor/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sugaradapter-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | com.zhihu.android.sugaradapter.SugarProcessor
2 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mthli/SugarAdapter/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/java/com/zhihu/android/sugaradapterdemo/holder/BaseHolder.java:
--------------------------------------------------------------------------------
1 | package com.zhihu.android.sugaradapterdemo.holder;
2 |
3 | import android.view.View;
4 | import androidx.annotation.NonNull;
5 | import com.zhihu.android.sugaradapter.SugarHolder;
6 |
7 | // Do something in BaseHolder, but don't use @Layout and @Id
8 | abstract class BaseHolder extends SugarHolder {
9 | BaseHolder(@NonNull View view) {
10 | super(view);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # Log Files
26 | *.log
27 |
28 | # Android Studio Navigation editor temp files
29 | .navigation/
30 |
31 | # Android Studio captures folder
32 | captures/
33 |
34 | # Others
35 | .idea/
36 | *.DS_Store
37 | *.directory
38 | *.iml
39 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | include ':app', ':library', ':sugaradapter', ':sugaradapter-annotation', ':sugaradapter-processor'
18 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 | SugarAdapterDemo
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 | SugarAdapter
20 |
21 |
22 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020 Matthew Lee
3 | # Copyright 2018 Zhihu Inc.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | #Sun Jul 19 16:09:28 CST 2020
19 | distributionBase=GRADLE_USER_HOME
20 | distributionPath=wrapper/dists
21 | zipStoreBase=GRADLE_USER_HOME
22 | zipStorePath=wrapper/dists
23 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
24 |
--------------------------------------------------------------------------------
/sugaradapter-annotation/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'java'
18 |
19 | // for plugin 'java'
20 | targetCompatibility = '1.8'
21 | sourceCompatibility = '1.8'
22 |
23 | dependencies {
24 | implementation fileTree(include: ['*.jar'], dir: 'libs')
25 | compileOnly "androidx.annotation:annotation:$projectAndroidX"
26 | }
27 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/java/com/zhihu/android/sugaradapter/InjectDelegate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import android.view.View;
20 | import androidx.annotation.NonNull;
21 |
22 | public interface InjectDelegate {
23 | void injectView(@NonNull SH sh, @NonNull View view);
24 | }
25 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
19 |
20 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/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 /Users/matthew/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhihu/android/sugaradapterdemo/item/Bar.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapterdemo.item;
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | public final class Bar {
22 | private String mText;
23 |
24 | public Bar(@NonNull String text) {
25 | mText = text;
26 | }
27 |
28 | @NonNull
29 | public String getText() {
30 | return mText;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhihu/android/sugaradapterdemo/item/Foo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapterdemo.item;
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | public final class Foo {
22 | private String mText;
23 |
24 | public Foo(@NonNull String text) {
25 | mText = text;
26 | }
27 |
28 | @NonNull
29 | public String getText() {
30 | return mText;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/sugaradapter-processor/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'java'
18 |
19 | // for plugin 'java'
20 | targetCompatibility = '1.8'
21 | sourceCompatibility = '1.8'
22 |
23 | dependencies {
24 | implementation fileTree(include: ['*.jar'], dir: 'libs')
25 | implementation project(':sugaradapter-annotation')
26 | implementation 'com.hendraanggrian:r-parser:0.2'
27 | compileOnly "androidx.annotation:annotation:$projectAndroidX"
28 | }
29 |
--------------------------------------------------------------------------------
/library/src/main/java/com/zhihu/android/sugaradapterlibrary/item/LibraryItem.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapterlibrary.item;
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | public final class LibraryItem {
22 | private String mText;
23 |
24 | public LibraryItem(@NonNull String text) {
25 | mText = text;
26 | }
27 |
28 | @NonNull
29 | public String getText() {
30 | return mText;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/sugaradapter-annotation/src/main/java/com/zhihu/android/sugaradapter/Id.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.IdRes;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | @Retention(RetentionPolicy.CLASS)
27 | @Target(ElementType.FIELD)
28 | public @interface Id {
29 | @IdRes
30 | int value() default 0;
31 | }
32 |
--------------------------------------------------------------------------------
/sugaradapter-annotation/src/main/java/com/zhihu/android/sugaradapter/Layout.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.LayoutRes;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Inherited;
23 | import java.lang.annotation.Retention;
24 | import java.lang.annotation.RetentionPolicy;
25 | import java.lang.annotation.Target;
26 |
27 | @Retention(RetentionPolicy.CLASS)
28 | @Target(ElementType.TYPE)
29 | @Inherited
30 | public @interface Layout {
31 | @LayoutRes
32 | int value() default 0;
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
22 |
23 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhihu/android/sugaradapterdemo/holder/FooHolder2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapterdemo.holder;
18 |
19 | import android.view.View;
20 | import androidx.annotation.NonNull;
21 | import com.zhihu.android.sugaradapterdemo.item.Foo;
22 |
23 | // Just extends FooHolder with same data,
24 | // so you need to use SugarAdapter#addDispatcher
25 | public final class FooHolder2 extends FooHolder {
26 | public FooHolder2(@NonNull View view) {
27 | super(view);
28 | }
29 |
30 | @Override
31 | protected void onBindData(@NonNull Foo foo) {
32 | String text = foo.getText() + " - FooHolder2";
33 | mTextView.setText(text);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/sugaradapter/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 /Users/matthew/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
27 | -keep class * extends com.zhihu.android.sugaradapter.SugarHolder
28 | -keepclassmembers class * extends com.zhihu.android.sugaradapter.SugarHolder {
29 | public (android.view.View);
30 | }
31 |
32 | -keep class * implements com.zhihu.android.sugaradapter.ContainerDelegate
33 | -keep class * implements com.zhihu.android.sugaradapter.InjectDelegate
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_bar.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
26 |
27 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_foo.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
26 |
27 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/library/src/main/res/layout/layout_library.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
26 |
27 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/library/src/main/java/com/zhihu/android/sugaradapterlibrary/holder/LibraryHolder.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 Matthew Lee
3 | * Copyright 2018 Zhihu Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.zhihu.android.sugaradapterlibrary.holder
19 |
20 | import android.view.View
21 | import androidx.appcompat.widget.AppCompatTextView
22 | import com.zhihu.android.sugaradapter.Id
23 | import com.zhihu.android.sugaradapter.Layout
24 | import com.zhihu.android.sugaradapter.SugarHolder
25 | import com.zhihu.android.sugaradapterlibrary.R2
26 | import com.zhihu.android.sugaradapterlibrary.item.LibraryItem
27 |
28 | @Layout(R2.layout.layout_library)
29 | class LibraryHolder(view: View) : SugarHolder(view) {
30 | @Id(R2.id.text)
31 | lateinit var textView: AppCompatTextView
32 |
33 | override fun onBindData(item: LibraryItem) {
34 | textView.text = item.text
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/java/com/zhihu/android/sugaradapter/ContainerDelegate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.LayoutRes;
20 | import androidx.annotation.NonNull;
21 |
22 | import java.util.Collections;
23 | import java.util.Map;
24 |
25 | public interface ContainerDelegate {
26 | @NonNull
27 | default Map, Integer> getLayoutResMap() {
28 | return Collections.emptyMap();
29 | }
30 |
31 | @NonNull
32 | default Map, Class> getDataClassMap() {
33 | return Collections.emptyMap();
34 | }
35 |
36 | @LayoutRes
37 | int getLayoutRes(@NonNull Class extends SugarHolder> holderClass);
38 |
39 | @NonNull
40 | Class getDataClass(@NonNull Class extends SugarHolder> holderClass);
41 | }
42 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2018 Zhihu Inc.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | # Project-wide Gradle settings.
18 |
19 | # IDE (e.g. Android Studio) users:
20 | # Gradle settings configured through the IDE *will override*
21 | # any settings specified in this file.
22 |
23 | # For more details on how to configure your build environment visit
24 | # http://www.gradle.org/docs/current/userguide/build_environment.html
25 |
26 | # Specifies the JVM arguments used for the daemon process.
27 | # The setting is particularly useful for tweaking memory settings.
28 | org.gradle.jvmargs=-Xmx1536m
29 |
30 | # When configured, Gradle will run in incubating parallel mode.
31 | # This option should only be used with decoupled projects. More details, visit
32 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
33 | # org.gradle.parallel=true
34 |
35 | # https://developer.android.com/jetpack/androidx/migrate
36 | android.useAndroidX=true
37 | android.enableJetifier=true
38 |
--------------------------------------------------------------------------------
/sugaradapter/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | apply plugin: 'com.android.library'
18 |
19 | android {
20 | buildToolsVersion projectBuildToolsVersion
21 | compileSdkVersion projectCompileSdkVersion
22 |
23 | defaultConfig {
24 | minSdkVersion projectMinSdkVersion
25 | targetSdkVersion projectTargetSdkVersion
26 | consumerProguardFiles 'proguard-rules.pro'
27 | }
28 |
29 | buildTypes {
30 | release {
31 | minifyEnabled false
32 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
33 | }
34 | }
35 |
36 | compileOptions {
37 | sourceCompatibility JavaVersion.VERSION_1_8
38 | targetCompatibility JavaVersion.VERSION_1_8
39 | }
40 | }
41 |
42 | dependencies {
43 | implementation fileTree(include: ['*.jar'], dir: 'libs')
44 | api project(':sugaradapter-annotation')
45 | api "androidx.recyclerview:recyclerview:$projectAndroidX"
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
22 |
23 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/sugaradapter-processor/src/main/java/com/zhihu/android/sugaradapter/Pair.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.NonNull;
20 | import androidx.annotation.Nullable;
21 |
22 | class Pair {
23 | private String mFirst;
24 | private String mSecond;
25 |
26 | Pair(@NonNull String first, @NonNull String second) {
27 | mFirst = first;
28 | mSecond = second;
29 | }
30 |
31 | @NonNull
32 | String getFirst() {
33 | return mFirst;
34 | }
35 |
36 | @NonNull
37 | String getSecond() {
38 | return mSecond;
39 | }
40 |
41 | @Override
42 | public boolean equals(@Nullable Object o) {
43 | if (this == o) return true;
44 | if (o == null || getClass() != o.getClass()) return false;
45 |
46 | Pair pair = (Pair) o;
47 |
48 | // noinspection SimplifiableIfStatement
49 | if (!mFirst.equals(pair.mFirst)) return false;
50 | return mSecond.equals(pair.mSecond);
51 | }
52 |
53 | @Override
54 | public int hashCode() {
55 | int result = mFirst.hashCode();
56 | result = 31 * result + mSecond.hashCode();
57 | return result;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhihu/android/sugaradapterdemo/holder/FooHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapterdemo.holder;
18 |
19 | import android.view.View;
20 | import androidx.annotation.NonNull;
21 | import androidx.appcompat.widget.AppCompatTextView;
22 | import com.zhihu.android.sugaradapter.Id;
23 | import com.zhihu.android.sugaradapter.Layout;
24 | import com.zhihu.android.sugaradapterdemo.R;
25 | import com.zhihu.android.sugaradapterdemo.item.Foo;
26 |
27 | @Layout(R.layout.layout_foo)
28 | public class FooHolder extends BaseHolder {
29 | @Id(R.id.text)
30 | public AppCompatTextView mTextView;
31 |
32 | @SuppressWarnings("WeakerAccess")
33 | public FooHolder(@NonNull View view) {
34 | super(view);
35 | }
36 |
37 | @Override
38 | protected void onBindData(@NonNull Foo foo) {
39 | mTextView.setText(foo.getText());
40 | }
41 |
42 | @Override
43 | protected void onViewAttachedToWindow() {
44 | // DO SOMETHING
45 | }
46 |
47 | @Override
48 | protected void onViewDetachedFromWindow() {
49 | // DO SOMETHING
50 | }
51 |
52 | @Override
53 | protected void onViewRecycled() {
54 | // DO SOMETHING
55 | }
56 |
57 | @Override
58 | protected boolean onFailedToRecycleView() {
59 | // DO SOMETHING
60 | return super.onFailedToRecycleView();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/java/com/zhihu/android/sugaradapter/Container.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.LayoutRes;
20 | import androidx.annotation.NonNull;
21 | import androidx.annotation.Nullable;
22 |
23 | class Container {
24 | private Class extends SugarHolder> mHolderClass;
25 | private Class> mDataClass;
26 | private int mLayoutRes;
27 | private SugarHolder.OnCreatedCallback mCallback;
28 | private Object mData;
29 |
30 | Container(@NonNull Class extends SugarHolder> holderClass,
31 | @NonNull Class> dataClass, @LayoutRes int layoutRes,
32 | @Nullable SugarHolder.OnCreatedCallback callback) {
33 | mHolderClass = holderClass;
34 | mDataClass = dataClass;
35 | mLayoutRes = layoutRes;
36 | mCallback = callback;
37 | }
38 |
39 | @NonNull
40 | Class extends SugarHolder> getHolderClass() {
41 | return mHolderClass;
42 | }
43 |
44 | @NonNull
45 | Class> getDataClass() {
46 | return mDataClass;
47 | }
48 |
49 | @LayoutRes
50 | int getLayoutRes() {
51 | return mLayoutRes;
52 | }
53 |
54 | @Nullable
55 | SugarHolder.OnCreatedCallback getCallback() {
56 | return mCallback;
57 | }
58 |
59 | @NonNull
60 | Object getData() {
61 | return mData;
62 | }
63 |
64 | void setData(@NonNull Object data) {
65 | mData = data;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 Matthew Lee
3 | * Copyright 2018 Zhihu Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | apply plugin: 'com.android.library'
19 | apply plugin: 'com.jakewharton.butterknife'
20 | apply plugin: 'kotlin-android'
21 | apply plugin: 'kotlin-android-extensions'
22 | apply plugin: 'kotlin-kapt'
23 |
24 | android {
25 | buildToolsVersion projectBuildToolsVersion
26 | compileSdkVersion projectCompileSdkVersion
27 |
28 | defaultConfig {
29 | minSdkVersion projectMinSdkVersion
30 | targetSdkVersion projectTargetSdkVersion
31 |
32 | javaCompileOptions {
33 | annotationProcessorOptions {
34 | arguments = [moduleNameOfSugarAdapter: 'library']
35 | }
36 | }
37 | }
38 |
39 | buildTypes {
40 | release {
41 | minifyEnabled false
42 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
43 | }
44 | }
45 |
46 | compileOptions {
47 | sourceCompatibility JavaVersion.VERSION_1_8
48 | targetCompatibility JavaVersion.VERSION_1_8
49 | }
50 | }
51 |
52 | dependencies {
53 | api fileTree(dir: 'libs', include: ['*.jar'])
54 | api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$projectKotlinVersion"
55 |
56 | // noinspection GradleDependency
57 | api "androidx.appcompat:appcompat:$projectAndroidX"
58 |
59 | // SugarAdapter
60 | // api "com.zhihu.android:sugaradapter:${ext.sugaradapter}"
61 | // annotationProcessor "com.zhihu.android:sugaradapter-processor:${ext.sugaradapter}"
62 | api project(':sugaradapter')
63 | kapt project(':sugaradapter-processor')
64 | }
65 |
--------------------------------------------------------------------------------
/sugaradapter-processor/src/main/java/com/zhihu/android/sugaradapter/InjectInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.NonNull;
20 | import androidx.annotation.Nullable;
21 |
22 | class InjectInfo {
23 | private String mViewName;
24 | private String mViewType;
25 | private String mViewIdStr;
26 |
27 | InjectInfo(@NonNull String viewName, @NonNull String viewType, @NonNull String viewIdStr) {
28 | mViewName = viewName;
29 | mViewType = viewType;
30 | mViewIdStr = viewIdStr;
31 | }
32 |
33 | @NonNull
34 | String getViewName() {
35 | return mViewName;
36 | }
37 |
38 | @NonNull
39 | String getViewType() {
40 | return mViewType;
41 | }
42 |
43 | @NonNull
44 | String getViewIdStr() {
45 | return mViewIdStr;
46 | }
47 |
48 | @Override
49 | public boolean equals(@Nullable Object obj) {
50 | if (this == obj) {
51 | return true;
52 | }
53 |
54 | if (obj == null || getClass() != obj.getClass()) {
55 | return false;
56 | }
57 |
58 | InjectInfo that = (InjectInfo) obj;
59 | if (!mViewName.equals(that.getViewName())) {
60 | return false;
61 | }
62 |
63 | if (!mViewType.equals(that.getViewType())) {
64 | return false;
65 | }
66 |
67 | return mViewIdStr.equals(that.getViewIdStr());
68 | }
69 |
70 | @Override
71 | public int hashCode() {
72 | int result = mViewName.hashCode();
73 | result = 31 * result + mViewType.hashCode();
74 | result = 31 * result + mViewIdStr.hashCode();
75 | return result;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 Matthew Lee
3 | * Copyright 2018 Zhihu Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | apply plugin: 'com.android.application'
19 | apply plugin: 'kotlin-android'
20 | apply plugin: 'kotlin-android-extensions'
21 | apply plugin: 'kotlin-kapt'
22 |
23 | android {
24 | buildToolsVersion projectBuildToolsVersion
25 | compileSdkVersion projectCompileSdkVersion
26 |
27 | defaultConfig {
28 | applicationId 'com.zhihu.android.sugaradapterdemo'
29 | minSdkVersion projectMinSdkVersion
30 | targetSdkVersion projectTargetSdkVersion
31 | versionCode 1
32 | versionName '1.0.0'
33 |
34 | javaCompileOptions {
35 | annotationProcessorOptions {
36 | arguments = [subModulesOfSugarAdapter: 'library']
37 | }
38 | }
39 | }
40 |
41 | buildTypes {
42 | release {
43 | minifyEnabled true
44 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
45 | }
46 | }
47 |
48 | compileOptions {
49 | sourceCompatibility JavaVersion.VERSION_1_8
50 | targetCompatibility JavaVersion.VERSION_1_8
51 | }
52 | }
53 |
54 | dependencies {
55 | implementation fileTree(include: ['*.jar'], dir: 'libs')
56 | implementation project(':library')
57 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$projectKotlinVersion"
58 |
59 | // noinspection GradleDependency
60 | implementation "androidx.appcompat:appcompat:$projectAndroidX"
61 |
62 | // SugarAdapter
63 | // implementation "com.zhihu.android:sugaradapter:${ext.sugaradapter}"
64 | // annotationProcessor "com.zhihu.android:sugaradapter-processor:${ext.sugaradapter}"
65 | implementation project(':sugaradapter')
66 | kapt project(':sugaradapter-processor')
67 | }
68 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/java/com/zhihu/android/sugaradapter/Sugar.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapter;
18 |
19 | import androidx.annotation.NonNull;
20 | import androidx.annotation.Nullable;
21 |
22 | import java.util.HashMap;
23 | import java.util.Map;
24 |
25 | public enum Sugar {
26 | INSTANCE;
27 |
28 | private ContainerDelegate mContainerDelegate;
29 | private Map, InjectDelegate> mInjectMap;
30 |
31 | @NonNull
32 | public ContainerDelegate getContainerDelegate() {
33 | if (mContainerDelegate == null) {
34 | try {
35 | Class delegateClass = Class.forName("com.zhihu.android.sugaradapter.ContainerDelegateImpl");
36 | mContainerDelegate = (ContainerDelegate) delegateClass.newInstance();
37 | } catch (Exception e) {
38 | throw new RuntimeException(e);
39 | }
40 | }
41 |
42 | return mContainerDelegate;
43 | }
44 |
45 | @Nullable
46 | public InjectDelegate getInjectDelegate(@NonNull T t) {
47 | if (mInjectMap == null) {
48 | mInjectMap = new HashMap<>();
49 | }
50 |
51 | if (mInjectMap.containsKey(t.getClass())) {
52 | return mInjectMap.get(t.getClass());
53 | }
54 |
55 | try {
56 | Class delegateClass = Class.forName(t.getClass().getCanonicalName() + "$InjectDelegateImpl");
57 | InjectDelegate delegate = (InjectDelegate) delegateClass.newInstance();
58 | mInjectMap.put(t.getClass(), delegate);
59 | return delegate;
60 | } catch (Exception e) {
61 | // e.printStackTrace();
62 | // noinspection ConstantConditions
63 | mInjectMap.put(t.getClass(), null);
64 | return null;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zhihu/android/sugaradapterdemo/holder/BarHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Zhihu Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.zhihu.android.sugaradapterdemo.holder;
18 |
19 | import android.view.View;
20 | import androidx.annotation.NonNull;
21 | import androidx.annotation.Nullable;
22 | import androidx.appcompat.widget.AppCompatTextView;
23 | import com.zhihu.android.sugaradapter.Id;
24 | import com.zhihu.android.sugaradapter.Layout;
25 | import com.zhihu.android.sugaradapter.SugarHolder;
26 | import com.zhihu.android.sugaradapterdemo.R;
27 | import com.zhihu.android.sugaradapterdemo.item.Bar;
28 |
29 | @Layout(R.layout.layout_bar)
30 | public final class BarHolder extends SugarHolder {
31 | public interface BarHolderListener {
32 | void onBarHolderViewAttachedToWindow(@NonNull BarHolder barHolder);
33 | void onBarHolderViewDetachedFromWindow(@NonNull BarHolder barHolder);
34 | }
35 |
36 | @Id(R.id.text)
37 | public AppCompatTextView mTextView;
38 |
39 | private BarHolderListener mListener;
40 |
41 | public BarHolder(@NonNull View view) {
42 | super(view);
43 | }
44 |
45 | public void setBarHolderListener(@Nullable BarHolderListener listener) {
46 | mListener = listener;
47 | }
48 |
49 | @Override
50 | protected void onBindData(@NonNull Bar bar) {
51 | mTextView.setText(bar.getText());
52 | }
53 |
54 | @Override
55 | protected void onViewAttachedToWindow() {
56 | if (mListener != null) {
57 | mListener.onBarHolderViewAttachedToWindow(this);
58 | }
59 | }
60 |
61 | @Override
62 | protected void onViewDetachedFromWindow() {
63 | if (mListener != null) {
64 | mListener.onBarHolderViewDetachedFromWindow(this);
65 | }
66 | }
67 |
68 | @Override
69 | protected void onViewRecycled() {
70 | // DO SOMETHING
71 | }
72 |
73 | @Override
74 | protected boolean onFailedToRecycleView() {
75 | // DO SOMETHING
76 | return super.onFailedToRecycleView();
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/sugaradapter/src/main/java/com/zhihu/android/sugaradapter/PreInflateThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 Matthew Lee
3 | * Copyright 2018 Zhihu Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.zhihu.android.sugaradapter;
19 |
20 | import android.os.Handler;
21 | import android.os.Looper;
22 | import android.os.Message;
23 | import android.util.SparseArray;
24 | import android.view.LayoutInflater;
25 | import android.view.View;
26 | import android.view.ViewGroup;
27 |
28 | import androidx.annotation.LayoutRes;
29 | import androidx.annotation.NonNull;
30 |
31 | import java.util.List;
32 | import java.util.concurrent.atomic.AtomicReference;
33 |
34 | class PreInflateThread extends Thread {
35 | private static final int MESSAGE_INTERRUPT = 0x00;
36 | private static final int MESSAGE_INFLATE = 0x01;
37 | private static final int MESSAGE_CLEAR = 0x02;
38 |
39 | private SparseArray> mViewRefArray;
40 | private List mListenerList;
41 | private ViewGroup mParent;
42 | private Handler mHandler;
43 | private LayoutInflater mInflater;
44 |
45 | public PreInflateThread(
46 | @NonNull ViewGroup parent,
47 | @NonNull SparseArray> viewRefArray,
48 | @NonNull List listenerList
49 | ) {
50 | mViewRefArray = viewRefArray;
51 | mListenerList = listenerList;
52 | mParent = parent;
53 | mInflater = LayoutInflater.from(parent.getContext());
54 | }
55 |
56 | @Override
57 | public void run() {
58 | Looper.prepare();
59 |
60 | Thread thread = Thread.currentThread();
61 | for (int i = 0; i < mViewRefArray.size() && !thread.isInterrupted(); i++) {
62 | inflateView(mViewRefArray.keyAt(i));
63 | }
64 |
65 | if (thread.isInterrupted()) return;
66 | mHandler = new Handler(msg -> {
67 | // msg.what = MESSAGE_INTERRUPT
68 | if (thread.isInterrupted()) {
69 | Looper looper = Looper.myLooper();
70 | if (looper != null) {
71 | looper.quit();
72 | }
73 | return true;
74 | }
75 |
76 | if (msg.what == MESSAGE_INFLATE) {
77 | inflateView((int) msg.obj);
78 | } else if (msg.what == MESSAGE_CLEAR) {
79 | mViewRefArray.clear();
80 | }
81 |
82 | return true;
83 | });
84 |
85 | Looper.loop();
86 | }
87 |
88 | public void clear() {
89 | if (mHandler != null) {
90 | mHandler.sendEmptyMessage(MESSAGE_CLEAR);
91 | }
92 | }
93 |
94 | public void inflate(@LayoutRes int layoutRes) {
95 | if (mHandler != null) {
96 | Message message = mHandler.obtainMessage(MESSAGE_INFLATE, layoutRes);
97 | mHandler.sendMessage(message);
98 | }
99 | }
100 |
101 | @Override
102 | public void interrupt() {
103 | super.interrupt();
104 | if (mHandler != null) {
105 | mHandler.sendEmptyMessage(MESSAGE_INTERRUPT);
106 | }
107 | }
108 |
109 | private void inflateView(@LayoutRes int layoutRes) {
110 | View view = mInflater.inflate(layoutRes, mParent, false);
111 | AtomicReference viewRef = mViewRefArray.get(layoutRes);
112 | if (viewRef != null) {
113 | viewRef.set(view);
114 | } else {
115 | mViewRefArray.put(layoutRes, new AtomicReference<>(view));
116 | }
117 |
118 | for (SugarAdapter.PreInflateListener listener : mListenerList) {
119 | if (listener != null) {
120 | listener.onPreInflateExecuted(layoutRes);
121 | }
122 | }
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/README_zh_CN.md:
--------------------------------------------------------------------------------
1 | SugarAdapter
2 | ===
3 |
4 | Make [RecyclerView.Adapter](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html "RecyclerView.Adapter") Great Again!
5 |
6 | 支持 **API v14+**
7 |
8 | ## 介绍
9 |
10 | 使用 RecyclerView 时,我们通常都要继承并复写 Adapter 的一些方法,感觉浪费时间又无聊:
11 |
12 | - `getItemCount()`
13 | - `getItemViewType()`
14 | - `onCreateViewHolder()`
15 | - `onBindViewHolder()`
16 | - `onViewAttachedToWindow()`
17 | - `onViewDetachedFromWindow()`
18 | - `onViewRecycled()`
19 | - `onAttachedToRecyclerView()`
20 | - `onDetachedFromRecyclerView()`
21 | - `onFailedToRecycleView()`
22 |
23 | 此外,我们通常需要 View **生命周期**相关的回调,
24 |
25 | 例如 `onViewAttachedToWindow()` 和 `onViewDetachedFromWindow()` 。
26 |
27 | 现在,我们使用 **annotationProcessor** 在编译时生成那些无聊的 Adapter 代码,并且给 ViewHolder 加上了一些特性。
28 |
29 | 完整的使用示例可以参考[这个链接](https://github.com/zhihu/SugarAdapter/tree/master/app "zhihu/SugarAdapter/app/")。
30 |
31 | ### SugarAdapter
32 |
33 | 你不需要继承并复写 Adapter ,只需要写几行 Builder 模式的代码即可:
34 |
35 | ```java
36 | mAdapter = SugarAdapter.Builder.with(mList) // eg. List