├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── layotu_button_2.xml
│ │ │ │ ├── layotu_button_1.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── xk
│ │ │ │ └── simplenestrecyclerview
│ │ │ │ ├── bean
│ │ │ │ ├── BrandBean.java
│ │ │ │ └── CategoryBean.java
│ │ │ │ ├── BrandAdapter.java
│ │ │ │ ├── CategoryAdapter.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── xk
│ │ │ └── simplenestrecyclerview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xk
│ │ └── simplenestrecyclerview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── simplenestlist
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── xk
│ │ │ └── simplenestlist
│ │ │ ├── BaseViewHolder.java
│ │ │ ├── layouthelper
│ │ │ ├── LayoutHelper.java
│ │ │ ├── LinearLayoutHelper.java
│ │ │ ├── SingleLayoutHelper.java
│ │ │ ├── GridLayoutHelper.java
│ │ │ ├── TitleGridLayoutHelper.java
│ │ │ └── CrossGridLayoutHelper.java
│ │ │ ├── SimpleNestListException.java
│ │ │ ├── SimpleNestLayoutManager.java
│ │ │ ├── adapter
│ │ │ ├── DefaultDiffCallback.java
│ │ │ ├── SingleAdapter.java
│ │ │ └── AbsSubAdapter.java
│ │ │ ├── Cantor.java
│ │ │ └── DelegateAdapter.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── xk
│ │ │ └── lib
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── xk
│ │ └── lib
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── image
├── demo.jpeg
├── demo.jpg
├── grid.png
├── list.png
├── single.png
├── jdhome.jpeg
├── jdcategory.jpeg
├── jdfilter.jpeg
├── taobaohome.jpeg
├── titlegrid.png
├── taobaofilter.jpeg
└── crossgridlayout.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── encodings.xml
├── compiler.xml
├── vcs.xml
├── jd_smartfox
│ └── smartfox_info.xml
├── runConfigurations.xml
├── gradle.xml
├── codeStyles
│ └── Project.xml
├── misc.xml
└── inspectionProfiles
│ └── Project_Default.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/simplenestlist/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':simplenestlist'
2 |
--------------------------------------------------------------------------------
/image/demo.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/demo.jpeg
--------------------------------------------------------------------------------
/image/demo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/demo.jpg
--------------------------------------------------------------------------------
/image/grid.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/grid.png
--------------------------------------------------------------------------------
/image/list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/list.png
--------------------------------------------------------------------------------
/image/single.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/single.png
--------------------------------------------------------------------------------
/image/jdhome.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/jdhome.jpeg
--------------------------------------------------------------------------------
/image/jdcategory.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/jdcategory.jpeg
--------------------------------------------------------------------------------
/image/jdfilter.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/jdfilter.jpeg
--------------------------------------------------------------------------------
/image/taobaohome.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/taobaohome.jpeg
--------------------------------------------------------------------------------
/image/titlegrid.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/titlegrid.png
--------------------------------------------------------------------------------
/image/taobaofilter.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/taobaofilter.jpeg
--------------------------------------------------------------------------------
/image/crossgridlayout.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/image/crossgridlayout.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SimpleNestRecyclerView
3 |
4 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | simplenestlist
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/94kai/SimpleNestList/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/simplenestlist/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 24 10:48:24 CST 2019
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.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/.idea/jd_smartfox/smartfox_info.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xk/simplenestrecyclerview/bean/BrandBean.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview.bean;
2 |
3 | /**
4 | * @author xuekai1
5 | * @date 2019/4/24
6 | */
7 | public class BrandBean {
8 | public String name;
9 |
10 | public BrandBean(String name) {
11 | this.name = name;
12 | }
13 |
14 | public BrandBean() {
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xk/simplenestrecyclerview/bean/CategoryBean.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview.bean;
2 |
3 | /**
4 | * @author xuekai1
5 | * @date 2019/4/24
6 | */
7 | public class CategoryBean {
8 | public String name = "";
9 |
10 | public CategoryBean(String name) {
11 | this.name = name;
12 | }
13 |
14 | public CategoryBean() {
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layotu_button_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layotu_button_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/BaseViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.View;
6 |
7 | /**
8 | * @author xuekai1
9 | * @date 2019/4/24
10 | */
11 | public class BaseViewHolder extends RecyclerView.ViewHolder {
12 | public BaseViewHolder(@NonNull View itemView) {
13 | super(itemView);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/simplenestlist/src/test/java/com/xk/lib/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/xk/simplenestrecyclerview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/LayoutHelper.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.layouthelper;
2 |
3 | /**
4 | * 自定义布局继承他
5 | * @author xuekai1
6 | * @date 2019/4/24
7 | */
8 | public abstract class LayoutHelper {
9 |
10 | /**
11 | * 需要支持的span。如果有多行需要支持3、4、5、6列,就用3 4 5 6
12 | */
13 | protected int[] needSpan = {1};
14 |
15 | public abstract int getSpanForPosition(int position, int maxSpanCount);
16 |
17 | public int[] getNeedSpan() {
18 | return needSpan;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/SimpleNestListException.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | /**
4 | * @author xuekai1
5 | * @date 2019/4/24
6 | */
7 | public class SimpleNestListException extends RuntimeException{
8 | public SimpleNestListException() {
9 | }
10 |
11 | public SimpleNestListException(String message) {
12 | super(message);
13 | }
14 |
15 | public SimpleNestListException(String message, Throwable cause) {
16 | super(message, cause);
17 | }
18 |
19 | public SimpleNestListException(Throwable cause) {
20 | super(cause);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/LinearLayoutHelper.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.layouthelper;
2 |
3 | import com.xk.simplenestlist.SimpleNestListException;
4 |
5 | /**
6 | * 线性的
7 | * | - |
8 | * | - |
9 | * | - |
10 | * | - |
11 | * | - |
12 | * | - |
13 | *
14 | * @author xuekai1
15 | * @date 2019/4/24
16 | */
17 | public class LinearLayoutHelper extends LayoutHelper {
18 |
19 | public LinearLayoutHelper() {
20 | this.needSpan = new int[]{1};
21 | }
22 |
23 | @Override
24 | public int getSpanForPosition(int position, int maxSpanCount) {
25 | if ((maxSpanCount<1)) {
26 | throw new SimpleNestListException("spanCount要大于1");
27 | }
28 | return maxSpanCount;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/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 |
--------------------------------------------------------------------------------
/simplenestlist/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 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/SimpleNestLayoutManager.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.GridLayoutManager;
5 | import android.util.AttributeSet;
6 |
7 | /**
8 | * @author xuekai1
9 | * @date 2019/4/24
10 | */
11 | public class SimpleNestLayoutManager extends GridLayoutManager {
12 | public SimpleNestLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
13 | super(context, attrs, defStyleAttr, defStyleRes);
14 | }
15 |
16 | public SimpleNestLayoutManager(Context context) {
17 | super(context, 1);
18 | }
19 |
20 | public SimpleNestLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
21 | super(context, spanCount, orientation, reverseLayout);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/simplenestlist/src/androidTest/java/com/xk/lib/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.xk.simplenestlist.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/xk/simplenestrecyclerview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.xk.simplenestrecyclerview", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/SingleLayoutHelper.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.layouthelper;
2 |
3 | import com.xk.simplenestlist.SimpleNestListException;
4 |
5 | /**
6 | * 只显示一个组件
7 | *
8 | * 还可以用来解决如下问题
9 | *
10 | * 三列grid+四列grid
11 | * | - - - |
12 | * | - - - |
13 | * | - - - |
14 | * | - - - - - |
15 | * | - - - - - |
16 | * | - - - - - |
17 | * 当三列gird最后一行不全时,四列gird的第一个item会顶上去,如下
18 | * | - - - |
19 | * | - - - |
20 | * | - - - | 该行最后一个元素是下面四列grid的模块
21 | * | - - - - - |
22 | * | - - - - - |
23 | * | - - - - |
24 | * 此时可以给两个模块中插入一个singleLayoutHelper
25 | * @author xuekai1
26 | * @date 2019/4/24
27 | */
28 | public class SingleLayoutHelper extends LayoutHelper {
29 | @Override
30 | public int getSpanForPosition(int position, int maxSpanCount) {
31 | if (position>0) {
32 | throw new SimpleNestListException("singleLayoutHelper只支持一个item");
33 | }
34 | return maxSpanCount;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/adapter/DefaultDiffCallback.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.adapter;
2 |
3 | import android.support.v7.util.DiffUtil;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * @author xuekai1
9 | * @date 2019/4/25
10 | */
11 | public class DefaultDiffCallback extends DiffUtil.Callback {
12 | private List oldData, newData;
13 |
14 | public DefaultDiffCallback() {
15 |
16 | }
17 |
18 | public void setDatas(List oldData, List newData) {
19 | this.oldData = oldData;
20 | this.newData = newData;
21 | }
22 |
23 | @Override
24 | public int getOldListSize() {
25 | return oldData == null ? 0 : oldData.size();
26 | }
27 |
28 | @Override
29 | public int getNewListSize() {
30 | return newData == null ? 0 : newData.size();
31 | }
32 |
33 | @Override
34 | public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
35 | return false;
36 | }
37 |
38 | @Override
39 | public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
40 | return false;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.xk.simplenestrecyclerview"
7 | minSdkVersion 21
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:28.0.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25 | implementation 'com.android.support:recyclerview-v7:28.0.0'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29 | implementation project(':simplenestlist')
30 | }
31 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/GridLayoutHelper.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.layouthelper;
2 |
3 | import com.xk.simplenestlist.SimpleNestListException;
4 |
5 | /**
6 | * 网格型的
7 | * | - - - |
8 | * | - - - |
9 | * | - - - |
10 | * | - - - |
11 | * | - - - |
12 | *
13 | * @author xuekai1
14 | * @date 2019/4/24
15 | */
16 | public class GridLayoutHelper extends LayoutHelper {
17 |
18 | private final int mColumn;
19 |
20 | public GridLayoutHelper(int column) {
21 | this.mColumn = column;
22 | this.needSpan = new int[]{column};
23 | }
24 |
25 | @Override
26 | public int getSpanForPosition(int position, int maxSpanCount) {
27 | //有个问题,每个adapter之间可能会在一行。可以通过插入一个0px的singleLayoutHelper实现
28 | if (mColumn > maxSpanCount) {
29 | throw new SimpleNestListException("gridLayoutHelper不能指定大于maxSpanCount的column");
30 | }
31 | int span = maxSpanCount / mColumn;
32 | if (span * mColumn != maxSpanCount) {
33 | //校验一下是否整除了
34 | throw new SimpleNestListException("maxSpanCount设置有误。请设置所有可能存在的列数的最小公倍数");
35 | }
36 | return span;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/Cantor.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | /**
4 | * Created by longerian on 2017/12/10.
5 | *
6 | * @author longerian
7 | * @date 2017/12/10
8 | */
9 |
10 | public class Cantor {
11 |
12 | /**
13 | * @param k1
14 | * @param k2
15 | * @return cantor pair for k1 and k2
16 | */
17 | public static long getCantor(long k1, long k2) {
18 | return (k1 + k2) * (k1 + k2 + 1) / 2 + k2;
19 | }
20 |
21 | /**
22 | * reverse cantor pair to origin number k1 and k2, k1 is stored in result[0], and k2 is stored in result[1]
23 | *
24 | * @param cantor a computed cantor number
25 | * @param result the array to store output values
26 | */
27 | public static void reverseCantor(long cantor, long[] result) {
28 | if (result == null || result.length < 2) {
29 | result = new long[2];
30 | }
31 | // reverse Cantor Function
32 | long w = (long) (Math.floor(Math.sqrt(8 * cantor + 1) - 1) / 2);
33 | long t = (w * w + w) / 2;
34 |
35 | long k2 = cantor - t;
36 | long k1 = w - k2;
37 | result[0] = k1;
38 | result[1] = k2;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/TitleGridLayoutHelper.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.layouthelper;
2 |
3 | import com.xk.simplenestlist.SimpleNestListException;
4 |
5 | /**
6 | * 带有title的网格型的
7 | * | ----- |
8 | * | - - - |
9 | * | - - - |
10 | * | - - - |
11 | * | - - - |
12 | *
13 | * @author xuekai1
14 | * @date 2019/4/24
15 | */
16 | public class TitleGridLayoutHelper extends LayoutHelper {
17 |
18 | private final int mColumn;
19 |
20 | public TitleGridLayoutHelper(int column) {
21 | this.mColumn = column;
22 | this.needSpan = new int[]{column};
23 | }
24 |
25 | @Override
26 | public int getSpanForPosition(int position, int maxSpanCount) {
27 | // TODO: by xk 2019/4/24 下午1:24 有个问题,每个adapter之间可能会在一行,或许可以通过重写getSpanIndex实现
28 | if (mColumn > maxSpanCount) {
29 | throw new SimpleNestListException("TitleGridLayoutHelper不能指定大于maxSpanCount" + maxSpanCount + "的column" + mColumn);
30 | }
31 | if (position == 0) {
32 | return maxSpanCount;
33 | }
34 | int span = maxSpanCount / mColumn;
35 | if (span * mColumn != maxSpanCount) {
36 | //校验一下是否整除了
37 | throw new SimpleNestListException("maxSpanCount设置有误。请设置所有可能存在的列数的最小公倍数");
38 | }
39 | return span;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/CrossGridLayoutHelper.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.layouthelper;
2 |
3 | /**
4 | * 一个交叉型的网格布局的实例。也可以模仿实现其他自定义各种交叉规则,或者实现一个通用的helper。
5 | * 由于该类型不定性太大,完全取决于
6 | * 业务需求,所以我不会实现一个通用的,毕竟越通用,使用门槛越高。大家自己跟着定制自己的规则即可。
7 | * | -|-|- |
8 | * | -|--- |
9 | * | ---|- |
10 | * | --|-- |
11 | * | - - - |
12 | * | - - - |
13 | *
14 | * @author xuekai1
15 | * @date 2019/4/24
16 | */
17 | public class CrossGridLayoutHelper extends LayoutHelper {
18 |
19 |
20 | public CrossGridLayoutHelper() {
21 | //第一行3列,第二行可以认为是4列。所以是3 4
22 | this.needSpan = new int[]{3, 4};
23 | }
24 |
25 | @Override
26 | public int getSpanForPosition(int position, int maxSpanCount) {
27 | //一行需要分成几列,就用maxSpanCount除以几。
28 | if (position < 3) {
29 | return maxSpanCount / 3;
30 | } else if (position == 3) {
31 | return maxSpanCount / 4;
32 | } else if (position == 4) {
33 | return maxSpanCount / 4 * 3;
34 | } else if (position == 5) {
35 | return maxSpanCount / 4 * 3;
36 | } else if (position == 6) {
37 | return maxSpanCount / 4;
38 | } else if (position < 9) {
39 | return maxSpanCount / 2;
40 | } else {
41 | return maxSpanCount / 3;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/adapter/SingleAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.adapter;
2 |
3 | import android.graphics.Color;
4 | import android.support.annotation.NonNull;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.TextView;
8 |
9 | import com.xk.simplenestlist.BaseViewHolder;
10 | import com.xk.simplenestlist.layouthelper.LayoutHelper;
11 |
12 | /**
13 | * @author xuekai1
14 | * @date 2019/4/24
15 | */
16 | public class SingleAdapter extends AbsSubAdapter {
17 | public SingleAdapter(LayoutHelper layoutHelper) {
18 | super(layoutHelper);
19 | }
20 |
21 | @Override
22 | public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
23 | View itemView;
24 | itemView = new TextView(viewGroup.getContext());
25 | ((TextView) itemView).setText("我是一条分割线,你可以把我高度设为0.");
26 | itemView.setBackgroundColor(Color.RED);
27 |
28 | itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
29 | return new BaseViewHolder(itemView);
30 | }
31 |
32 | @Override
33 | public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
34 | }
35 |
36 | @Override
37 | public final int getItemCount() {
38 | return 1;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xk/simplenestrecyclerview/BrandAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.Button;
8 | import android.widget.TextView;
9 |
10 | import com.xk.simplenestlist.BaseViewHolder;
11 | import com.xk.simplenestlist.adapter.AbsSubAdapter;
12 | import com.xk.simplenestlist.layouthelper.LayoutHelper;
13 | import com.xk.simplenestlist.layouthelper.TitleGridLayoutHelper;
14 | import com.xk.simplenestrecyclerview.bean.BrandBean;
15 |
16 | /**
17 | * @author xuekai1
18 | * @date 2019/4/24
19 | */
20 | public class BrandAdapter extends AbsSubAdapter {
21 |
22 | public BrandAdapter(LayoutHelper layoutHelper) {
23 | super(layoutHelper);
24 | }
25 |
26 | public BrandAdapter(LayoutHelper layoutHelper, RecyclerView.RecycledViewPool pool) {
27 | super(layoutHelper,pool);
28 | //设置回收池大小
29 | // pool.setMaxRecycledViews(1,10);
30 | // pool.setMaxRecycledViews(1,10);
31 | // pool.setMaxRecycledViews(1,10);
32 | // pool.setMaxRecycledViews(1,10);
33 | // pool.setMaxRecycledViews(1,10);
34 | }
35 | @NonNull
36 | @Override
37 | public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
38 | View itemView;
39 | itemView = new Button(viewGroup.getContext());
40 | itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
41 | return new BaseViewHolder(itemView);
42 | }
43 |
44 | @Override
45 | public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
46 | if (mLayoutHelper instanceof TitleGridLayoutHelper) {
47 | if (i != 0) {
48 | Button button = (Button) baseViewHolder.itemView;
49 | button.setText("brand:" + i + " ");
50 | } else {
51 | ((TextView) baseViewHolder.itemView).setText("我是brand的title");
52 | }
53 | } else {
54 | Button button = (Button) baseViewHolder.itemView;
55 | button.setText("brand:" + i + " ");
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xk/simplenestrecyclerview/CategoryAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.widget.Button;
7 | import android.widget.TextView;
8 |
9 | import com.xk.simplenestlist.BaseViewHolder;
10 | import com.xk.simplenestlist.adapter.AbsSubAdapter;
11 | import com.xk.simplenestlist.layouthelper.LayoutHelper;
12 | import com.xk.simplenestlist.layouthelper.TitleGridLayoutHelper;
13 | import com.xk.simplenestrecyclerview.bean.CategoryBean;
14 |
15 | /**
16 | * @author xuekai1
17 | * @date 2019/4/24
18 | */
19 | public class CategoryAdapter extends AbsSubAdapter {
20 |
21 | /**
22 | * 实现类似二级菜单展开、收起的功能
23 | */
24 | boolean isClose = false;
25 |
26 | public CategoryAdapter(LayoutHelper layoutHelper) {
27 | super(layoutHelper);
28 | }
29 |
30 | @NonNull
31 | @Override
32 | public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
33 | View itemView;
34 | itemView = new Button(viewGroup.getContext());
35 | itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
36 | return new BaseViewHolder(itemView);
37 | }
38 |
39 | @Override
40 | public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
41 | if (mLayoutHelper instanceof TitleGridLayoutHelper) {
42 | if (i != 0) {
43 | Button button = (Button) baseViewHolder.itemView;
44 | button.setText(mData.get(i).name + "category:" + i + " ");
45 | } else {
46 | ((TextView) baseViewHolder.itemView).setText("我是category的title");
47 | }
48 | } else {
49 | Button button = (Button) baseViewHolder.itemView;
50 | button.setText(mData.get(i).name + "category:" + i);
51 | }
52 | }
53 |
54 | public void setCloseState(boolean isClose) {
55 | this.isClose = isClose;
56 | }
57 |
58 |
59 | @Override
60 | public int getItemCount() {
61 | if (isClose) {
62 | //关闭后只显示3个item。这块注意,数字至少也要大于mData.size(),否则onbindviewholder里会越界
63 | return super.getItemCount() / 2;
64 | } else {
65 | //打开后显示全部
66 | return super.getItemCount();
67 | }
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 |
--------------------------------------------------------------------------------
/simplenestlist/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 | version = "1.0.0"
5 | group = "com.xk.library"
6 | def siteUrl = 'https://github.com/XSation/SimpleNestList' // project homepage
7 | def gitUrl = 'https://github.com/XSation/SimpleNestList.git' // project git
8 | android {
9 | compileSdkVersion 28
10 |
11 |
12 |
13 | defaultConfig {
14 | minSdkVersion 21
15 | targetSdkVersion 28
16 | versionCode 1
17 | versionName "1.0"
18 |
19 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
20 |
21 | }
22 |
23 | buildTypes {
24 | release {
25 | minifyEnabled false
26 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
27 | }
28 | }
29 |
30 | }
31 |
32 | dependencies {
33 | implementation fileTree(dir: 'libs', include: ['*.jar'])
34 |
35 | implementation 'com.android.support:appcompat-v7:28.0.0'
36 | implementation 'com.android.support:recyclerview-v7:27.0.0'
37 | testImplementation 'junit:junit:4.12'
38 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
39 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
40 | }
41 |
42 | task sourcesJar(type: Jar) {
43 | from android.sourceSets.main.java.srcDirs
44 | classifier = 'sources'
45 | }
46 | task javadoc(type: Javadoc) {
47 | source = android.sourceSets.main.java.srcDirs
48 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
49 | }
50 | task javadocJar(type: Jar, dependsOn: javadoc) {
51 | classifier = 'javadoc'
52 | from javadoc.destinationDir
53 | }
54 | artifacts {
55 | archives javadocJar
56 | archives sourcesJar
57 | }
58 |
59 | install {
60 | repositories.mavenInstaller {
61 | // This generates POM.xml with proper parameters
62 | pom {
63 | project {
64 | packaging 'aar'
65 | name '嵌套recyclerview'
66 | url siteUrl
67 | // Set your license
68 | licenses {
69 | license {
70 | name 'The Apache Software License, Version 2.0'
71 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
72 | }
73 | }
74 | developers {
75 | developer { //填写的一些基本信息
76 | id 'xuekai'
77 | name 'xuekai'
78 | email '3440395@qq.com'
79 | }
80 | }
81 | scm {
82 | connection gitUrl
83 | developerConnection gitUrl
84 | url siteUrl
85 | }
86 | }
87 | }
88 | }
89 | }
90 |
91 |
92 | //配置bintray参数
93 | Properties properties = new Properties()
94 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
95 | bintray {
96 | user = properties.getProperty("bintrayUser")
97 | key = properties.getProperty("bintrayApiKey")
98 | configurations = ['archives']
99 | pkg {
100 | repo = "maven" //Maven仓库的名字
101 | name = "SimpleNestList" //com.xk.library:LimitApp:1.0.0
102 | websiteUrl = siteUrl
103 | vcsUrl = gitUrl
104 | licenses = ["Apache-2.0"]
105 | publish = true
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 一款仿VLayout的可嵌套RecyclerView框架
2 |
3 | # 鸣谢 ^_^
4 | https://github.com/alibaba/vlayout
5 |
6 | ## 使用
7 |
8 | - 项目级build.gradle添加
9 | ```
10 | allprojects {
11 | repositories {
12 | ...
13 | maven{
14 | url "https://dl.bintray.com/xk3440395/maven"
15 | }
16 | }
17 | }
18 | ```
19 | - module级build.gradle添加
20 | ```
21 |
22 | ```
23 |
24 | ## 已实现的效果图
25 |
26 | > 以下只是暂时已经实现的几个布局,他们支持任意组合,搭配。相同类型的view之间支持互相复用。更重要的是扩展出新的布局也很简单,可以直接参考已经实现的效果去做。
27 |
28 | 列表
29 |
30 | 
31 |
32 | 网格
33 |
34 | 
35 |
36 | 带title的网格
37 |
38 | 
39 |
40 |
41 | 横跨view的独立控件
42 |
43 | 
44 |
45 | 交错的网格(一个示例,具体可以根据自己产品需求自定义)
46 |
47 | 
48 |
49 | 搭配起来的效果
50 |
51 | 
52 |
53 |
54 | ## 什么地方需要用到该框架(当然用VLayout是首选,不过重复造轮子也没什么不好,重要的是学习知识点 ^_^...)
55 |
56 | 
57 | 
58 | 
59 | 
60 | 
61 |
62 | ## 优缺点
63 | - 对外暴露的api模仿VLayout实现,使用方式与VLayout一样简单。简化了开发步骤。
64 | - 对于布局的实现,使用gridLayoutManager控制span实现。
65 | - 相较于VLayout:
66 | - 实现方式更low ^_^
67 | - 不支持流式布局
68 | - 但也有好处:扩展容易,只需要继承LayoutHelper,实现几个方法即可,具体可参考layouthelper包下的几个类。
69 | - 目前已经实现的Layout:
70 | - LinearLayoutHelper 线性的,即类似于listView那样
71 | - GridLayoutHelper 网格型的,类似于GridView
72 | - TitleGridLayoutHelper title拖grid,可以实现类似于京东、淘宝、天猫搜索页面的筛选面板那样的布局。
73 | - SingleLayoutHelper 通栏型。 即放置一个控件,横向铺满屏幕。
74 | - 本来不打算实现该布局,因为他其实就是一个长度的LinearLayoutHelper。后来遇到一个问题。如下:
75 | 三列grid+四列grid组合时,当三列gird最后一行不全时,四列gird的第一个item会顶上去,此时可以给两个模块中插入一个singleLayoutHelper
76 | - 参考https://github.com/XSation/SimpleNestList/blob/master/simplenestlist/src/main/java/com/xk/simplenestlist/layouthelper/SingleLayoutHelper.java
77 | - 所以干脆把他写出来了,使用也稍微方便了一点点。
78 | - 等等,具体以上面效果图为准
79 | - 关于itemShareType
80 | - 类似于VLayout的mHasConsistItemType变量。
81 | - 最开始支持了不同模块之间viewtype共享,后来发现这样的话使用起来成本高,而且意义不是太大,所以直接删了。各模块之间的type互不干扰。
82 | - 每个subAdapter随意实现自己的getItemType即可
83 |
84 | ## 数据操作、ui刷新
85 |
86 | 目前支持:
87 | - 整个recyclerView一次性刷。简单粗暴。
88 | - 针对一个subAdapter刷。由于subAdapter的模块化开发。所以也很简单。同时效率也更好。
89 | - 不要使用Adapter官方提供的方法!!!不要使用Adapter官方提供的方法!!!不要使用Adapter官方提供的方法!!!
90 | - 构建AbsSubAdapter时传入RecycledViewPool,然后通过调用setMaxRecycledViews方法设置各种viewType的回收池大小
91 | ### 如下:
92 |
93 | - 简单粗暴法。刷整个recyclerview
94 | - 修改任意一个subAdapter的数据源之后,可以调用delegateAdapter.simpleNotifyDataSetChanged来刷新,注意不是系统提供的notifyDataSetChanged方法
95 | - delegateAdapter.simpleNotifyDataSetChanged();
96 | - 略微粗暴法。
97 | - 刷一个subAdapter的小区域,目前只想到这种方法,并且必须要传入新旧数据源,没有好的思路保存这两数据源。大家有其他思路的可以提一提。
98 | - 使用默认的DiffCallback,即认为调用该方法,先移除,后新增,效率略低
99 | - categoryAdapter.simpleNotifyDataSetChanged(oldDatas, categoryBeans);
100 | - 使用自定义的DiffCallback,会根据数据源的变化情况来刷,效率略高
101 | - categoryAdapter.simpleNotifyDataSetChanged(oldDatas, categoryBeans,new DefaultDiffCallback(){// TODO: by xk 2019/4/25 下午3:56 实现各种自己的实现});
102 | - itemChanged
103 | - categoryAdapter.simpleNotifyItemChanged(1);
104 | - itemRemove
105 | - categoryAdapter.simpleNotifyItemRemoved(0);
106 | - itemMove
107 | - categoryAdapter.simpleNotifyItemMoved(2, 10);
108 | - itemInserted
109 | - categoryAdapter.simpleNotifyItemInserted(4);
110 | - itemRangeChanged
111 | - categoryAdapter.simpleNotifyItemRangeChanged(1,4);
112 | - itemRangeRemoved
113 | - categoryAdapter.simpleNotifyItemRangeRemoved(1,4);
114 | - itemRangeInserted
115 | - categoryAdapter.simpleNotifyItemRangeInserted(1,4);
116 |
117 |
118 | # TODO
119 | - 可以利用ItemDecoration实现类似阿里的fix类型的布局。开发者同样使用adapter的形式使用,delegateadapter中把这种helper的subadapter过滤掉,单独处理。
120 | - 吸顶效果
121 | - 针对subAdapter刷所有数据时,不需要传入新旧数据源(没有想到较好的办法去保存修改前后数据源)
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/adapter/AbsSubAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist.adapter;
2 |
3 |
4 | import android.support.v7.util.DiffUtil;
5 | import android.support.v7.util.ListUpdateCallback;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.view.ViewGroup;
8 |
9 | import com.xk.simplenestlist.BaseViewHolder;
10 | import com.xk.simplenestlist.Cantor;
11 | import com.xk.simplenestlist.DelegateAdapter;
12 | import com.xk.simplenestlist.layouthelper.LayoutHelper;
13 |
14 | import java.util.HashMap;
15 | import java.util.List;
16 |
17 | /**
18 | * @author xuekai1
19 | * @date 2019/4/24
20 | */
21 | public abstract class AbsSubAdapter extends RecyclerView.Adapter {
22 | private RecyclerView.RecycledViewPool pool;
23 | /**
24 | * 子adapter第一个元素在整个recyclerview中的起始position
25 | */
26 | public int mStartPosition;
27 |
28 | protected List mData;
29 | //位于adapter的集合第几位
30 | private int index = -1;
31 |
32 | protected LayoutHelper mLayoutHelper;
33 | protected DelegateAdapter mDelegateAdapter;
34 |
35 | @Override
36 | public abstract BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int i);
37 |
38 | @Override
39 | public abstract void onBindViewHolder(BaseViewHolder baseViewHolder, int i);
40 |
41 | public AbsSubAdapter(LayoutHelper layoutHelper) {
42 | this.mLayoutHelper = layoutHelper;
43 | }
44 |
45 | public AbsSubAdapter(LayoutHelper layoutHelper, RecyclerView.RecycledViewPool pool) {
46 | this.mLayoutHelper = layoutHelper;
47 | this.pool = pool;
48 | }
49 |
50 | public void setDelegateAdapter(DelegateAdapter delegateAdapter) {
51 | this.mDelegateAdapter = delegateAdapter;
52 | mDelegateAdapter.setSpanCount(mLayoutHelper.getNeedSpan());
53 | }
54 |
55 |
56 | @Override
57 | public int getItemCount() {
58 | return mData == null ? 0 : mData.size();
59 | }
60 |
61 | public void setData(List data) {
62 | mData = data;
63 | notifyDataSetChanged();
64 | }
65 |
66 | public int getSpanByPosition(int position, int maxSpanCount) {
67 | return mLayoutHelper.getSpanForPosition(position, maxSpanCount);
68 | }
69 |
70 | HashMap recycledViewTypeCache = new HashMap<>();
71 |
72 | /**
73 | * 设置viewtype的缓存
74 | *
75 | * @param viewType
76 | * @param max
77 | */
78 | public void setMaxRecycledViews(int viewType, int max) {
79 | if (index != -1) {
80 | int index = getIndex();
81 | //在总的adapter中的真实的viewType
82 | long realViewType = Cantor.getCantor(viewType, index);
83 | if (pool != null) {
84 | pool.setMaxRecycledViews((int) realViewType, max);
85 | }
86 | } else {
87 | //缓存起来,在setIndex的时候使用
88 | recycledViewTypeCache.put(viewType, max);
89 | }
90 |
91 | }
92 |
93 | public int getIndex() {
94 | return index;
95 | }
96 |
97 | public void setIndex(int index) {
98 | this.index = index;
99 | for (Integer viewType : recycledViewTypeCache.keySet()) {
100 | //在总的adapter中的真实的viewType
101 | long realViewType = Cantor.getCantor(viewType, index);
102 | if (pool != null) {
103 | pool.setMaxRecycledViews((int) realViewType, recycledViewTypeCache.get(viewType));
104 | }
105 | }
106 | recycledViewTypeCache.clear();
107 | }
108 |
109 | public List getData() {
110 | return mData;
111 | }
112 |
113 | public void simpleNotifyItemChanged(int position) {
114 | mDelegateAdapter.notifyItemChanged(mStartPosition + position);
115 | }
116 |
117 | public void simpleNotifyItemRemoved(int position) {
118 | mDelegateAdapter.updateIndex();
119 | mDelegateAdapter.notifyItemRemoved(mStartPosition + position);
120 | }
121 |
122 | public void simpleNotifyItemMoved(int fromPosition, int toPosition) {
123 | mDelegateAdapter.notifyItemMoved(mStartPosition + fromPosition, mStartPosition + toPosition);
124 | }
125 |
126 | public void simpleNotifyItemInserted(int position) {
127 | mDelegateAdapter.updateIndex();
128 | mDelegateAdapter.notifyItemInserted(mStartPosition + position);
129 | }
130 |
131 | public void simpleNotifyItemRangeChanged(int positionStart, int count) {
132 | mDelegateAdapter.notifyItemRangeChanged(mStartPosition + positionStart, count);
133 | }
134 |
135 | public void simpleNotifyItemRangeRemoved(int positionStart, int count) {
136 | mDelegateAdapter.updateIndex();
137 | mDelegateAdapter.notifyItemRangeRemoved(mStartPosition + positionStart, count);
138 | }
139 |
140 | public void simpleNotifyItemRangeInserted(int positionStart, int count) {
141 | mDelegateAdapter.updateIndex();
142 | mDelegateAdapter.notifyItemRangeInserted(mStartPosition + positionStart, count);
143 | }
144 |
145 | public void simpleNotifyDataSetChanged(List oldData, List newData, DefaultDiffCallback callback) {
146 | callback.setDatas(oldData, newData);
147 | //虽然是刷subAdapter的所有,但在整个recyclerview中还是一小部分,所以只能根据数据源的变化来调用delegateAdapter的各种notify方法
148 | DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, true);
149 | diffResult.dispatchUpdatesTo(new ListUpdateCallback() {
150 | @Override
151 | public void onInserted(int position, int count) {
152 | simpleNotifyItemRangeInserted(position, count);
153 | }
154 |
155 | @Override
156 | public void onRemoved(int position, int count) {
157 | simpleNotifyItemRangeRemoved(position, count);
158 | }
159 |
160 | @Override
161 | public void onMoved(int fromPosition, int toPosition) {
162 | simpleNotifyItemMoved(fromPosition, toPosition);
163 | }
164 |
165 | @Override
166 | public void onChanged(int position, int count, Object payload) {
167 | simpleNotifyItemRangeChanged(position, count);
168 | }
169 | });
170 |
171 | }
172 |
173 | public void simpleNotifyDataSetChanged(List oldData, List newData) {
174 | DefaultDiffCallback callback = new DefaultDiffCallback<>();
175 | simpleNotifyDataSetChanged(oldData, newData, callback);
176 | }
177 |
178 |
179 | }
180 |
--------------------------------------------------------------------------------
/app/src/main/java/com/xk/simplenestrecyclerview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestrecyclerview;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.support.v7.widget.Toolbar;
7 | import android.view.View;
8 |
9 | import com.xk.simplenestlist.DelegateAdapter;
10 | import com.xk.simplenestlist.SimpleNestLayoutManager;
11 | import com.xk.simplenestlist.adapter.AbsSubAdapter;
12 | import com.xk.simplenestlist.adapter.SingleAdapter;
13 | import com.xk.simplenestlist.layouthelper.CrossGridLayoutHelper;
14 | import com.xk.simplenestlist.layouthelper.GridLayoutHelper;
15 | import com.xk.simplenestlist.layouthelper.LinearLayoutHelper;
16 | import com.xk.simplenestlist.layouthelper.SingleLayoutHelper;
17 | import com.xk.simplenestlist.layouthelper.TitleGridLayoutHelper;
18 | import com.xk.simplenestrecyclerview.bean.BrandBean;
19 | import com.xk.simplenestrecyclerview.bean.CategoryBean;
20 |
21 | import java.lang.reflect.InvocationTargetException;
22 | import java.util.ArrayList;
23 | import java.util.LinkedList;
24 | import java.util.List;
25 |
26 | public class MainActivity extends AppCompatActivity {
27 |
28 | private RecyclerView recyclerView;
29 |
30 | LinkedList absSubAdapters = new LinkedList<>();
31 | private CategoryAdapter categoryAdapter;
32 | private List categoryBeans;
33 | private DelegateAdapter delegateAdapter;
34 | private BrandAdapter brandAdapter;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.activity_main);
40 | recyclerView = findViewById(R.id.recyclerview);
41 | Toolbar toolBar = findViewById(R.id.action_bar);
42 | toolBar.setSubtitle("点击操作数据源");
43 | toolBar.setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View v) {
46 | help();
47 | }
48 | });
49 |
50 | SimpleNestLayoutManager layoutManager = new SimpleNestLayoutManager(this);
51 | delegateAdapter = new DelegateAdapter(layoutManager);
52 | recyclerView.setLayoutManager(layoutManager);
53 | RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();
54 | recyclerView.setRecycledViewPool(recycledViewPool);
55 | List brandBeans = createDatas(BrandBean.class);
56 | categoryBeans = createDatas(CategoryBean.class);
57 |
58 |
59 | BrandAdapter service = new BrandAdapter(new TitleGridLayoutHelper(3));
60 | service.setData(brandBeans);
61 | categoryAdapter = new CategoryAdapter(new GridLayoutHelper(4));
62 | categoryAdapter.setData(categoryBeans);
63 |
64 |
65 | BrandAdapter serviceList = new BrandAdapter(new LinearLayoutHelper(), recycledViewPool);
66 | serviceList.setData(brandBeans);
67 |
68 | BrandAdapter serviceList2 = new BrandAdapter(new TitleGridLayoutHelper(2));
69 | serviceList2.setData(brandBeans);
70 |
71 | absSubAdapters.add(service);
72 | absSubAdapters.add(new SingleAdapter(new SingleLayoutHelper()));
73 | absSubAdapters.add(categoryAdapter);
74 | absSubAdapters.add(serviceList);
75 | absSubAdapters.add(serviceList2);
76 | absSubAdapters.add(new SingleAdapter(new SingleLayoutHelper()));
77 |
78 | brandAdapter = new BrandAdapter(new CrossGridLayoutHelper());
79 | brandAdapter.setData(createDatas(BrandBean.class, 25));
80 | absSubAdapters.add(brandAdapter);
81 | delegateAdapter.setAdapters(absSubAdapters);
82 | recyclerView.setAdapter(delegateAdapter);
83 | }
84 |
85 |
86 | private List createDatas(Class clazz, int count) {
87 | List beans = new ArrayList<>();
88 | try {
89 | for (int i = 0; i < count; i++) {
90 | if (clazz == CategoryBean.class) {
91 | beans.add(clazz.getConstructor(String.class).newInstance("" + i));
92 | } else {
93 | beans.add(clazz.getConstructor().newInstance());
94 | }
95 | }
96 | } catch (InstantiationException e) {
97 | e.printStackTrace();
98 | } catch (IllegalAccessException e) {
99 | e.printStackTrace();
100 | } catch (InvocationTargetException e) {
101 | e.printStackTrace();
102 | } catch (NoSuchMethodException e) {
103 | e.printStackTrace();
104 | }
105 |
106 | return beans;
107 | }
108 |
109 | private List createDatas(Class clazz) {
110 | return createDatas(clazz, 100);
111 | }
112 |
113 | public void help() {
114 |
115 | //==================数据操作使用说明
116 | //1.1修改subAdapter数据源
117 | // categoryBeans.remove(0);
118 | // categoryBeans.set(1,new CategoryBean("修改后的"));
119 | // final List oldDatas = new ArrayList(categoryBeans);
120 | // categoryBeans.add(4, new CategoryBean("新增第四个"));
121 | // categoryBeans.clear();
122 | // CategoryBean temp = categoryBeans.remove(2);
123 | // categoryBeans.add(10, temp);
124 | //1.2修改subAdapter的getItemCount方法,实现类似于二级菜单打开关闭的功能
125 | // categoryAdapter.setCloseState(true);
126 |
127 | //==================recyclerView刷新说明
128 | //2.1简单粗暴法。刷整个recyclerview
129 | // 修改任意一个subAdapter的数据源之后,可以调用delegateAdapter.simpleNotifyDataSetChanged来刷新,注意不是系统提供的notifyDataSetChanged方法
130 | // delegateAdapter.simpleNotifyDataSetChanged();
131 | //2.2略微粗暴法。刷一个subAdapter的小区域,目前只想到这种方法。大家有其他思路的可以提一提
132 | //2.2.1使用默认的DiffCallback,即认为调用该方法,先移除,后新增,效率略低
133 | // categoryAdapter.simpleNotifyDataSetChanged(oldDatas, categoryBeans);
134 | //2.2.3使用自定义的DiffCallback,会根据数据源的变化情况来刷,效率略高
135 | // categoryAdapter.simpleNotifyDataSetChanged(oldDatas, categoryBeans,new DefaultDiffCallback(){
136 | // TODO: by xk 2019/4/25 下午3:56 实现各种自己的实现
137 | // });
138 | //2.3itemChanged
139 | // categoryAdapter.simpleNotifyItemChanged(1);
140 | //2.4itemRemove
141 | // categoryAdapter.simpleNotifyItemRemoved(0);
142 | //2.5itemMove
143 | // categoryAdapter.simpleNotifyItemMoved(2, 10);
144 | //2.6itemInserted
145 | // categoryAdapter.simpleNotifyItemInserted(4);
146 | //2.7itemRangeChanged
147 | // categoryAdapter.simpleNotifyItemRangeChanged(1,4);
148 | //2.8itemRangeRemoved
149 | // categoryAdapter.simpleNotifyItemRangeRemoved(1,4);
150 | //2.8itemRangeInserted
151 | // categoryAdapter.simpleNotifyItemRangeInserted(1,4);
152 |
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/simplenestlist/src/main/java/com/xk/simplenestlist/DelegateAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xk.simplenestlist;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.v7.widget.GridLayoutManager;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.util.Log;
7 | import android.view.ViewGroup;
8 |
9 | import com.xk.simplenestlist.adapter.AbsSubAdapter;
10 |
11 | import java.util.LinkedList;
12 |
13 | /**
14 | * subadapter之间的item的type不共享
15 | *
16 | * @author xuekai1
17 | * @date 2019/4/24
18 | */
19 | public class DelegateAdapter extends RecyclerView.Adapter {
20 |
21 | private final SimpleNestLayoutManager mLayoutManager;
22 |
23 | /**
24 | * subadapter的集合
25 | */
26 | private LinkedList mAdapters = new LinkedList<>();
27 |
28 | /**
29 | * itemCount
30 | */
31 | private int total;
32 |
33 | /**
34 | * 构造
35 | * 共享type
36 | *
37 | * @param layoutManager
38 | */
39 | public DelegateAdapter(SimpleNestLayoutManager layoutManager) {
40 | this.mLayoutManager = layoutManager;
41 | layoutManager.setSpanSizeLookup(getSpanSizeLookUp());
42 | }
43 |
44 |
45 | @NonNull
46 | @Override
47 | public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
48 | AbsSubAdapter absSubAdapter = resolveViewType(viewType);
49 | if (absSubAdapter != null) {
50 | Log.i("DelegateAdapter", "onCreateViewHolder-->" + viewType + " adapterIndex:" + absSubAdapter.getIndex());
51 | return absSubAdapter.onCreateViewHolder(parent, viewType);
52 | }
53 | throw new SimpleNestListException();
54 | }
55 |
56 | /**
57 | * 根据viewType解析出所属的SubAdapter
58 | */
59 | private AbsSubAdapter resolveViewType(int viewType) {
60 | long[] result = new long[2];
61 | Cantor.reverseCantor(viewType, result);
62 | long index = result[1];
63 | return mAdapters.get((int) index);
64 | }
65 |
66 | @Override
67 | public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
68 | AbsSubAdapter absSubAdapterByPosition = findSubAdapterByPosition(position);
69 | if (absSubAdapterByPosition == null) {
70 | throw new SimpleNestListException();
71 | }
72 | absSubAdapterByPosition.onBindViewHolder(holder, position - absSubAdapterByPosition.mStartPosition);
73 | }
74 |
75 | @Override
76 | public int getItemCount() {
77 | return total;
78 | }
79 |
80 | @Override
81 | public int getItemViewType(int position) {
82 | AbsSubAdapter absSubAdapter = findSubAdapterByPosition(position);
83 | if (absSubAdapter == null) {
84 | throw new SimpleNestListException();
85 | }
86 | return makeViewType(absSubAdapter, position);
87 | //通过absSubAdapter和position构造出一个viewType
88 | }
89 |
90 | private int makeViewType(AbsSubAdapter absSubAdapter, int position) {
91 | int subItemType = absSubAdapter.getItemViewType(position - absSubAdapter.mStartPosition);
92 | long cantor = Cantor.getCantor(subItemType, absSubAdapter.getIndex());
93 | return (int) cantor;
94 | }
95 |
96 | /**
97 | * 通过position寻找他所属的SubAdapter
98 | *
99 | * @return AbsSubAdapter
100 | */
101 | private AbsSubAdapter findSubAdapterByPosition(int position) {
102 | final int count = mAdapters.size();
103 | if (count == 0) {
104 | return null;
105 | }
106 | int s = 0, e = count - 1, m;
107 | AbsSubAdapter result = null;
108 | while (s <= e) {
109 | m = (s + e) / 2;
110 | result = mAdapters.get(m);
111 | int endPosition = result.mStartPosition + result.getItemCount() - 1;
112 | if (result.mStartPosition > position) {
113 | e = m - 1;
114 | } else if (endPosition < position) {
115 | s = m + 1;
116 | } else if (result.mStartPosition <= position && endPosition >= position) {
117 | break;
118 | }
119 | result = null;
120 | }
121 |
122 | return result;
123 | }
124 |
125 | /**
126 | * 设置adapters
127 | *
128 | * @param mAdapters
129 | */
130 | public void setAdapters(LinkedList mAdapters) {
131 | this.mAdapters = mAdapters;
132 | simpleNotifyDataSetChanged();
133 | }
134 |
135 | /**
136 | * 清空状态
137 | */
138 | private void clear() {
139 | total = 0;
140 | // TODO: by xk 2019/4/24 上午10:36 任意一个adapter的itemCount改变,都需要更新total
141 | // TODO: by xk 2019-05-31 14:59 如何可以类似于调用RecyclerView.setAdapter 方法,全部重置,防止出现type和上次的type一样的情况。情况复用池应该可以
142 | }
143 |
144 | /**
145 | * 任意一个subAdapter的数据源改变,都可以通过调用该方法来刷新。类似于notifyDataSetChanged
146 | */
147 | public void simpleNotifyDataSetChanged() {
148 | updateIndex();
149 | notifyDataSetChanged();
150 | // TODO: by xk 2019/4/24 上午10:39 更新position和subadapter的索引
151 | }
152 |
153 | public void updateIndex() {
154 | clear();
155 | int startPosition = 0;
156 | int index = 0;
157 | for (AbsSubAdapter mAdapter : mAdapters) {
158 | mAdapter.setDelegateAdapter(this);
159 | mAdapter.mStartPosition = startPosition;
160 | startPosition += mAdapter.getItemCount();
161 | mAdapter.setIndex(index);
162 | index++;
163 | }
164 | for (AbsSubAdapter adapter : mAdapters) {
165 | total += adapter.getItemCount();
166 | }
167 | }
168 |
169 | public GridLayoutManager.SpanSizeLookup getSpanSizeLookUp() {
170 | return new GridLayoutManager.SpanSizeLookup() {
171 | @Override
172 | public int getSpanSize(int position) {
173 | AbsSubAdapter subAdapter = findSubAdapterByPosition(position);
174 | if (subAdapter == null) {
175 | throw new SimpleNestListException(position + "的adapter没找到");
176 | }
177 | return subAdapter.getSpanByPosition(position - subAdapter.mStartPosition, mLayoutManager.getSpanCount());
178 | }
179 | };
180 | }
181 |
182 | /**
183 | * 每增加一个layouthelper,就要刷新一下spancount
184 | *
185 | * @param supportSpanCount 需要支持的列数
186 | */
187 | public void setSpanCount(int[] supportSpanCount) {
188 | if (mLayoutManager == null) {
189 | throw new SimpleNestListException();
190 | }
191 | int spanCount = mLayoutManager.getSpanCount();
192 | int spanCountTemp = spanCount;
193 | for (int i : supportSpanCount) {
194 | spanCountTemp = leastCommonMultiple(spanCountTemp, i);
195 | }
196 | if (spanCount != spanCountTemp) {
197 | mLayoutManager.setSpanCount(spanCountTemp);
198 | }
199 | }
200 |
201 | /**
202 | * 求最小公倍数
203 | */
204 | public int leastCommonMultiple(int a, int b) {
205 | int c = a * b;
206 | if (a < b) {
207 | int r;
208 | r = a;
209 | a = b;
210 | b = r;
211 | }
212 | while (true) {
213 | int r = a % b;
214 | if (r == 0) {
215 | return c / b;
216 | } else {
217 | a = b;
218 | b = r;
219 | }
220 | }
221 | }
222 | }
223 |
224 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
--------------------------------------------------------------------------------