├── 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
│ │ │ │ ├── item_rv_grid.xml
│ │ │ │ ├── item_rv_linear.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── zeropercenthappy
│ │ │ └── zphrvitemdecoration
│ │ │ ├── GridRVAdapter.java
│ │ │ ├── LinearRVAdapter.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── zeropercenthappy
│ │ │ └── zphrvitemdecoration
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── zeropercenthappy
│ │ └── zphrvitemdecoration
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── zeropercenthappy
│ │ │ └── divider
│ │ │ ├── LinearLayoutManagerDivider.kt
│ │ │ └── GridLayoutManagerDivider.kt
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── zeropercenthappy
│ │ │ └── decorationlibrary
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── zeropercenthappy
│ │ └── decorationlibrary
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── screenshots
├── GridLayoutManagerDivider.png
├── LinearLayoutManagerDivider.png
├── GridLayoutManagerDividerFullWrap.png
└── LinearLayoutManagerDividerFullWrap.png
├── gradle.properties
├── gradlew.bat
├── README_CN.md
├── README.md
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /.idea
3 | /build
4 | local.properties
5 | *.iml
6 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Library
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ZPHRVItemDecoration
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/screenshots/GridLayoutManagerDivider.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/screenshots/GridLayoutManagerDivider.png
--------------------------------------------------------------------------------
/screenshots/LinearLayoutManagerDivider.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/screenshots/LinearLayoutManagerDivider.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/screenshots/GridLayoutManagerDividerFullWrap.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/screenshots/GridLayoutManagerDividerFullWrap.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/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/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/screenshots/LinearLayoutManagerDividerFullWrap.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/HEAD/screenshots/LinearLayoutManagerDividerFullWrap.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeropercenthappy/ZPHRVItemDecoration/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/zeropercenthappy/ZPHRVItemDecoration/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/zeropercenthappy/ZPHRVItemDecoration/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Feb 29 13:18:25 CST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
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/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/zeropercenthappy/zphrvitemdecoration/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.zphrvitemdecoration;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/library/src/test/java/com/zeropercenthappy/decorationlibrary/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.decorationlibrary;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_rv_grid.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_rv_linear.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
17 |
--------------------------------------------------------------------------------
/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.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | android.enableJetifier=true
13 | android.useAndroidX=true
14 | org.gradle.jvmargs=-Xmx1536m
15 |
16 | # When configured, Gradle will run in incubating parallel mode.
17 | # This option should only be used with decoupled projects. More details, visit
18 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
19 | # org.gradle.parallel=true
20 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/zeropercenthappy/decorationlibrary/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.decorationlibrary;
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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.zeropercenthappy.library.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/zeropercenthappy/zphrvitemdecoration/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.zphrvitemdecoration;
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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.zeropercenthappy.zphrvitemdecoration", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 29
7 | defaultConfig {
8 | applicationId "com.zeropercenthappy.zphrvitemdecoration"
9 | minSdkVersion 16
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(include: ['*.jar'], dir: 'libs')
25 |
26 | implementation 'com.google.android.material:material:1.2.1'
27 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
28 |
29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
30 |
31 | implementation project(':library')
32 | }
33 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'kotlin-android'
4 | apply plugin: 'kotlin-android-extensions'
5 | group = 'com.github.zeropercenthappy'
6 |
7 | android {
8 | compileSdkVersion 29
9 |
10 | defaultConfig {
11 | minSdkVersion 14
12 | targetSdkVersion 29
13 | versionCode 110
14 | versionName "1.1.0"
15 |
16 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
17 |
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 |
27 | }
28 |
29 | dependencies {
30 | implementation fileTree(include: ['*.jar'], dir: 'libs')
31 |
32 | implementation 'com.google.android.material:material:1.2.1'
33 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
34 | }
35 |
36 | task sourcesJar(type: Jar) {
37 | from android.sourceSets.main.java.srcDirs
38 | getArchiveClassifier().set("sources")
39 | }
40 |
41 | artifacts {
42 | archives sourcesJar
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zeropercenthappy/zphrvitemdecoration/GridRVAdapter.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.zphrvitemdecoration;
2 |
3 | import androidx.recyclerview.widget.RecyclerView;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.ImageView;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * @author ybq
13 | * @date 2017/12/26
14 | */
15 |
16 | public class GridRVAdapter extends RecyclerView.Adapter {
17 | private List entityList;
18 |
19 | public List getEntityList() {
20 | return entityList;
21 | }
22 |
23 | public void setEntityList(List entityList) {
24 | this.entityList = entityList;
25 | }
26 |
27 | @Override
28 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
29 | View rootView = LayoutInflater.from(parent.getContext())
30 | .inflate(R.layout.item_rv_grid, parent, false);
31 | return new ViewHolder(rootView);
32 | }
33 |
34 | @Override
35 | public void onBindViewHolder(ViewHolder holder, int position) {
36 | ImageView iv = (ImageView) holder.getView(R.id.iv);
37 | iv.setImageResource(R.color.colorPrimary);
38 | }
39 |
40 | @Override
41 | public int getItemCount() {
42 | return entityList == null ? 0 : entityList.size();
43 | }
44 |
45 | public class ViewHolder extends RecyclerView.ViewHolder {
46 |
47 | public ViewHolder(View itemView) {
48 | super(itemView);
49 | }
50 |
51 | public View getView(int viewId) {
52 | return itemView.findViewById(viewId);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zeropercenthappy/zphrvitemdecoration/LinearRVAdapter.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.zphrvitemdecoration;
2 |
3 | import androidx.recyclerview.widget.RecyclerView;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.ImageView;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * @author ybq
13 | * @date 2017/12/26
14 | */
15 |
16 | public class LinearRVAdapter extends RecyclerView.Adapter {
17 | private List entityList;
18 |
19 | public List getEntityList() {
20 | return entityList;
21 | }
22 |
23 | public void setEntityList(List entityList) {
24 | this.entityList = entityList;
25 | }
26 |
27 | @Override
28 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
29 | View rootView = LayoutInflater.from(parent.getContext())
30 | .inflate(R.layout.item_rv_linear, parent, false);
31 | return new ViewHolder(rootView);
32 | }
33 |
34 | @Override
35 | public void onBindViewHolder(ViewHolder holder, int position) {
36 | ImageView iv = (ImageView) holder.getView(R.id.iv);
37 | iv.setImageResource(R.color.colorPrimary);
38 | }
39 |
40 | @Override
41 | public int getItemCount() {
42 | return entityList == null ? 0 : entityList.size();
43 | }
44 |
45 | public class ViewHolder extends RecyclerView.ViewHolder {
46 |
47 | public ViewHolder(View itemView) {
48 | super(itemView);
49 | }
50 |
51 | public View getView(int viewId) {
52 | return itemView.findViewById(viewId);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
26 |
27 |
35 |
36 |
44 |
45 |
53 |
54 |
62 |
63 |
72 |
73 |
--------------------------------------------------------------------------------
/README_CN.md:
--------------------------------------------------------------------------------
1 | # ZPHRVItemDecoration
2 |
3 | ## 分割线
4 |
5 | ### LinearLayoutManager
6 |
7 | #### 用法
8 |
9 | - 全包裹:
10 |
11 | ```java
12 | LinearLayoutManagerDivider divider = new LinearLayoutManagerDivider(color, dividerWidth);
13 | // 如果RecyclerView有HeaderView或FooterView(不需要绘制分割线)
14 | // 按下面的方法添加进Divider中(不会影响UI,仅用于计算)
15 | divider.addHeaderView(headerView);
16 | divider.addFooterView(footerView);
17 | //
18 | rv.addItemDecoration(divider);
19 | ```
20 |
21 | 
22 |
23 | - 非全包裹:
24 |
25 | ```java
26 | LinearLayoutManagerDivider divider = new LinearLayoutManagerDivider(color, dividerWidth, false);
27 | // 如果RecyclerView有HeaderView或FooterView(不需要绘制分割线)
28 | // 按下面的方法添加进Divider中(不会影响UI,仅用于计算)
29 | divider.addHeaderView(headerView);
30 | divider.addFooterView(footerView);
31 | //
32 | rv.addItemDecoration(divider);
33 | ```
34 |
35 | 
36 |
37 | ### GridLayoutManager
38 |
39 | #### 用法 (当前仅支持`Vertical`方向的`GridLayoutManager`绘制分割线)
40 |
41 | - Full Wrap:
42 |
43 | ```java
44 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, dividerWidth);
45 | // 如果你想为横向和竖向的分割线设置不同的size:
46 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, horizontalDividerHeight, horizontalDividerHeight, true);
47 | // 如果RecyclerView有HeaderView或FooterView(不需要绘制分割线)
48 | // 按下面的方法添加进Divider中(不会影响UI,仅用于计算)
49 | divider.addHeaderView(headerView);
50 | divider.addFooterView(footerView);
51 | //
52 | rv.addItemDecoration(divider);
53 | ```
54 |
55 | 
56 |
57 | - Not Full Wrap:
58 |
59 | ```java
60 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, dividerWidth, false);
61 | // 如果你想为横向和竖向的分割线设置不同的size:
62 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, horizontalDividerHeight, verticalDividerWidth, false);
63 | // 如果RecyclerView有HeaderView或FooterView(不需要绘制分割线)
64 | // 按下面的方法添加进Divider中(不会影响UI,仅用于计算)
65 | divider.addHeaderView(headerView);
66 | divider.addFooterView(footerView);
67 | //
68 | rv.addItemDecoration(divider);
69 | ```
70 |
71 | 
72 |
73 | ## 下载
74 |
75 | ### 步骤 1.
76 |
77 | 添加以下配置到项目根目录位置的build.gradle:
78 |
79 | ```groovy
80 | allprojects {
81 | repositories {
82 | ...
83 | maven { url 'https://jitpack.io' }
84 | }
85 | }
86 | ```
87 |
88 | ### 步骤 2.
89 |
90 | 在Module目录下的build.gradle文件内添加依赖:
91 |
92 | ```groovy
93 | dependencies {
94 | // 对于android compat版本
95 | implementation 'com.github.zeropercenthappy:ZPHRVItemDecoration:1.0.9'
96 | // 对于androidX版本
97 | implementation 'com.github.zeropercenthappy:ZPHRVItemDecoration:1.1.0'
98 | }
99 | ```
100 |
101 | ## 更新日志
102 |
103 | - 1.1.0
104 |
105 | 迁移至AndroidX。
106 |
107 | - 1.0.9
108 |
109 | Support different size of horizontal and vertical divider for GridLayoutManager.
110 |
111 | - 1.0.8
112 |
113 | fix draw way of not full wrap style.
114 |
115 | - 1.0.7
116 |
117 | Use kotlin to rewrite, rewrite all calculate logic, support to set header view and footer view.
118 |
119 | - 1.0.6
120 |
121 | Downgrade minSdkVersion to 14
122 |
123 | - 1.0.4
124 |
125 | Add sources jar
126 |
127 | - 1.0.3
128 |
129 | Fix build error
130 |
131 | - 1.0.2
132 |
133 | Support RecycleView set padding now.
134 |
135 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ZPHRVItemDecoration [简体中文](https://github.com/zeropercenthappy/ZPHRVItemDecoration/blob/master/README_CN.md)
2 |
3 | ## Divider
4 |
5 | ### LinearLayoutManager
6 |
7 | #### Usage
8 |
9 | - Full Wrap:
10 |
11 | ```java
12 | LinearLayoutManagerDivider divider = new LinearLayoutManagerDivider(color, dividerWidth);
13 | // if you have header view or footer view.
14 | // it will not influence layout, only use for calculate.
15 | divider.addHeaderView(headerView);
16 | divider.addFooterView(footerView);
17 | //
18 | rv.addItemDecoration(divider);
19 | ```
20 |
21 | 
22 |
23 | - Not Full Wrap:
24 |
25 | ```java
26 | LinearLayoutManagerDivider divider = new LinearLayoutManagerDivider(color, dividerWidth, false);
27 | // if you have header view or footer view.
28 | // it will not influence layout, only use for calculate.
29 | divider.addHeaderView(headerView);
30 | divider.addFooterView(footerView);
31 | //
32 | rv.addItemDecoration(divider);
33 | ```
34 |
35 | 
36 |
37 | ### GridLayoutManager
38 |
39 | #### Usage (Current version only support `Vertical` `GridLayoutManager`)
40 |
41 | - Full Wrap:
42 |
43 | ```java
44 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, dividerWidth, true);
45 | // if you want to set a different size of divider:
46 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, horizontalDividerHeight, horizontalDividerHeight, true);
47 | // if you have header view or footer view.
48 | // it will not influence layout, only use for calculate.
49 | divider.addHeaderView(headerView);
50 | divider.addFooterView(footerView);
51 | //
52 | rv.addItemDecoration(divider);
53 | ```
54 |
55 | 
56 |
57 | - Not Full Wrap:
58 |
59 | ```java
60 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, dividerWidth, false);
61 | // if you want to set a different size of divider:
62 | GridLayoutManagerDivider divider = new GridLayoutManagerDivider(color, horizontalDividerHeight, verticalDividerWidth, false);
63 | // if you have header view or footer view.
64 | // it will not influence layout, only use for calculate.
65 | divider.addHeaderView(headerView);
66 | divider.addFooterView(footerView);
67 | //
68 | rv.addItemDecoration(divider);
69 | ```
70 |
71 | 
72 |
73 | ## Download
74 |
75 | ### Step 1.
76 |
77 | Add it in your root build.gradle at the end of repositories:
78 |
79 | ```groovy
80 | allprojects {
81 | repositories {
82 | ...
83 | maven { url 'https://jitpack.io' }
84 | }
85 | }
86 | ```
87 |
88 | ### Step 2.
89 |
90 | Add the dependency in your module's build.gradle:
91 |
92 | ```groovy
93 | dependencies {
94 | // For android compat version
95 | implementation 'com.github.zeropercenthappy:ZPHRVItemDecoration:1.0.9'
96 | // For androidX version
97 | implementation 'com.github.zeropercenthappy:ZPHRVItemDecoration:1.1.0'
98 | }
99 | ```
100 |
101 | ## Change log
102 |
103 | - 1.1.0
104 |
105 | Migrate to AndroidX.
106 |
107 | - 1.0.9
108 |
109 | Support different size of horizontal and vertical divider for GridLayoutManager.
110 |
111 | - 1.0.8
112 |
113 | fix draw way of not full wrap style.
114 |
115 | - 1.0.7
116 |
117 | Use kotlin to rewrite, rewrite all calculate logic, support to set header view and footer view.
118 |
119 | - 1.0.6
120 |
121 | Downgrade minSdkVersion to 14
122 |
123 | - 1.0.4
124 |
125 | Add sources jar
126 |
127 | - 1.0.3
128 |
129 | Fix build error
130 |
131 | - 1.0.2
132 |
133 | Support RecycleView set padding now.
134 |
135 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zeropercenthappy/zphrvitemdecoration/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.zphrvitemdecoration;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import androidx.appcompat.app.AppCompatActivity;
6 | import androidx.recyclerview.widget.GridLayoutManager;
7 | import androidx.recyclerview.widget.LinearLayoutManager;
8 | import androidx.recyclerview.widget.RecyclerView;
9 | import android.view.View;
10 | import android.widget.Button;
11 |
12 | import com.zeropercenthappy.divider.GridLayoutManagerDivider;
13 | import com.zeropercenthappy.divider.LinearLayoutManagerDivider;
14 |
15 | import java.util.ArrayList;
16 | import java.util.List;
17 |
18 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
19 |
20 | private Button btnLinear;
21 | private Button btnLinearFullWrap;
22 | private Button btnGrid;
23 | private Button btnGridFullWrap;
24 | private Button btnMinus;
25 | private Button btnAdd;
26 | private RecyclerView rv;
27 |
28 | private GridRVAdapter gridRVAdapter;
29 | private LinearRVAdapter linearRVAdapter;
30 | private List entityList;
31 |
32 | @Override
33 | protected void onCreate(Bundle savedInstanceState) {
34 | super.onCreate(savedInstanceState);
35 | setContentView(R.layout.activity_main);
36 | initData();
37 | initView();
38 | rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
39 | rv.setAdapter(linearRVAdapter);
40 | }
41 |
42 | private void initData() {
43 | entityList = new ArrayList<>();
44 | entityList.add("");
45 | gridRVAdapter = new GridRVAdapter();
46 | gridRVAdapter.setEntityList(entityList);
47 | linearRVAdapter = new LinearRVAdapter();
48 | linearRVAdapter.setEntityList(entityList);
49 | }
50 |
51 | private void initView() {
52 | rv = findViewById(R.id.rv);
53 | btnMinus = findViewById(R.id.btn_minus);
54 | btnMinus.setOnClickListener(this);
55 | btnAdd = findViewById(R.id.btn_add);
56 | btnAdd.setOnClickListener(this);
57 | btnLinear = findViewById(R.id.btn_linear);
58 | btnLinear.setOnClickListener(this);
59 | btnLinearFullWrap = findViewById(R.id.btn_linear_full_wrap);
60 | btnLinearFullWrap.setOnClickListener(this);
61 | btnGrid = findViewById(R.id.btn_grid);
62 | btnGrid.setOnClickListener(this);
63 | btnGridFullWrap = findViewById(R.id.btn_grid_full_wrap);
64 | btnGridFullWrap.setOnClickListener(this);
65 | }
66 |
67 | @Override
68 | public void onClick(View v) {
69 | if (v == btnMinus) {
70 | if (entityList.size() > 0) {
71 | entityList.remove(entityList.size() - 1);
72 | }
73 | if (rv.getAdapter() != null) {
74 | rv.getAdapter().notifyDataSetChanged();
75 | }
76 | } else if (v == btnAdd) {
77 | entityList.add("");
78 | if (rv.getAdapter() != null) {
79 | rv.getAdapter().notifyDataSetChanged();
80 | }
81 | } else if (v == btnLinear) {
82 | rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
83 | if (rv.getItemDecorationCount() > 0) {
84 | rv.removeItemDecoration(rv.getItemDecorationAt(0));
85 | }
86 | rv.addItemDecoration(new LinearLayoutManagerDivider(Color.parseColor("#e69310"), 10, false));
87 | rv.setAdapter(linearRVAdapter);
88 | } else if (v == btnLinearFullWrap) {
89 | rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
90 | if (rv.getItemDecorationCount() > 0) {
91 | rv.removeItemDecoration(rv.getItemDecorationAt(0));
92 | }
93 | rv.addItemDecoration(new LinearLayoutManagerDivider(Color.parseColor("#e69310"), 10));
94 | rv.setAdapter(linearRVAdapter);
95 | } else if (v == btnGrid) {
96 | rv.setLayoutManager(new GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false));
97 | if (rv.getItemDecorationCount() > 0) {
98 | rv.removeItemDecoration(rv.getItemDecorationAt(0));
99 | }
100 | rv.addItemDecoration(new GridLayoutManagerDivider(Color.parseColor("#e69310"), 10, false));
101 | rv.setAdapter(gridRVAdapter);
102 | } else if (v == btnGridFullWrap) {
103 | rv.setLayoutManager(new GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false));
104 | if (rv.getItemDecorationCount() > 0) {
105 | rv.removeItemDecoration(rv.getItemDecorationAt(0));
106 | }
107 | rv.addItemDecoration(new GridLayoutManagerDivider(Color.parseColor("#e69310"), 10, true));
108 | rv.setAdapter(gridRVAdapter);
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/library/src/main/java/com/zeropercenthappy/divider/LinearLayoutManagerDivider.kt:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.divider
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Paint
5 | import android.graphics.Rect
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import androidx.annotation.ColorInt
9 | import androidx.recyclerview.widget.RecyclerView
10 | import kotlin.math.roundToInt
11 |
12 | class LinearLayoutManagerDivider(
13 | @ColorInt dividerColor: Int,
14 | private val dividerWidth: Int
15 | ) : RecyclerView.ItemDecoration() {
16 |
17 | constructor(
18 | @ColorInt dividerColor: Int,
19 | dividerWidth: Int,
20 | fullWrap: Boolean
21 | ) : this(dividerColor, dividerWidth) {
22 | this.fullWrap = fullWrap
23 | }
24 |
25 | private val paint: Paint = Paint()
26 | private val headerViewList = arrayListOf()
27 | private val footerViewList = arrayListOf()
28 | private var fullWrap = true
29 | private var orientation: Int = androidx.recyclerview.widget.LinearLayoutManager.VERTICAL
30 |
31 | init {
32 | paint.isAntiAlias = true
33 | paint.style = Paint.Style.FILL
34 | paint.color = dividerColor
35 | }
36 |
37 | override fun onDraw(
38 | canvas: Canvas,
39 | parent: RecyclerView,
40 | state: RecyclerView.State
41 | ) {
42 | if (fullWrap) {
43 | drawFullWrap(canvas, parent)
44 | } else {
45 | drawNotFullWrap(canvas, parent, state)
46 | }
47 | }
48 |
49 | /**
50 | * 每个item都画满宽度的线
51 | * 以item的offset来决定最终显示出来的分割线
52 | */
53 | private fun drawFullWrap(canvas: Canvas, parent: RecyclerView) {
54 | for (i in 0 until parent.childCount) {
55 | val childView = parent.getChildAt(i)
56 | // HeaderView和FooterView不处理
57 | if (isHeader(childView) || isFooter(childView)) {
58 | continue
59 | }
60 | // 绘制分割线
61 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
62 | // 上边
63 | canvas.drawRect(
64 | childView.left.toFloat(),
65 | childView.top.toFloat() - dividerWidth,
66 | childView.right.toFloat(),
67 | childView.top.toFloat(), paint
68 | )
69 | // 下边
70 | canvas.drawRect(
71 | childView.left.toFloat(),
72 | childView.bottom.toFloat(),
73 | childView.right.toFloat(),
74 | childView.bottom.toFloat() + dividerWidth,
75 | paint
76 | )
77 | } else {
78 | // 左边
79 | canvas.drawRect(
80 | childView.left.toFloat() - dividerWidth,
81 | childView.top.toFloat(),
82 | childView.left.toFloat(),
83 | childView.bottom.toFloat(),
84 | paint
85 | )
86 | // 右边
87 | canvas.drawRect(
88 | childView.right.toFloat(),
89 | childView.top.toFloat(),
90 | childView.right.toFloat() + dividerWidth,
91 | childView.bottom.toFloat(),
92 | paint
93 | )
94 | }
95 | }
96 | }
97 |
98 | /**
99 | * 根据item的偏移量绘制分割线
100 | */
101 | private fun drawNotFullWrap(
102 | canvas: Canvas,
103 | parent: RecyclerView,
104 | state: RecyclerView.State
105 | ) {
106 | for (i in 0 until parent.childCount) {
107 | val childView = parent.getChildAt(i)
108 | // HeaderView和FooterView不处理
109 | if (isHeader(childView) || isFooter(childView)) {
110 | continue
111 | }
112 | // 排除HeaderView和FooterView后,计算真实列表中的ChildView数量
113 | val realItemCount = state.itemCount - headerViewList.size - footerViewList.size
114 | // 找到当前ChildView在真实列表中的position(从1数起)
115 | val positionInLinear =
116 | parent.getChildLayoutPosition(childView) + 1 - headerViewList.size
117 | // 根据偏移量绘制分割线
118 | val offsetRect = getNotFullWrapOffsets(positionInLinear, realItemCount)
119 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
120 | // 上边
121 | canvas.drawRect(
122 | childView.left.toFloat(),
123 | (childView.top - offsetRect.top).toFloat(),
124 | childView.right.toFloat(),
125 | childView.top.toFloat(),
126 | paint
127 | )
128 | // 下边
129 | canvas.drawRect(
130 | childView.left.toFloat(),
131 | childView.bottom.toFloat(),
132 | childView.right.toFloat(),
133 | (childView.bottom + offsetRect.bottom).toFloat(),
134 | paint
135 | )
136 | } else {
137 | // 左边
138 | canvas.drawRect(
139 | (childView.left - offsetRect.left).toFloat(),
140 | childView.top.toFloat(),
141 | childView.left.toFloat(),
142 | childView.bottom.toFloat(),
143 | paint
144 | )
145 | // 右边
146 | canvas.drawRect(
147 | childView.right.toFloat(),
148 | childView.top.toFloat(),
149 | (childView.right + offsetRect.right).toFloat(),
150 | childView.bottom.toFloat(),
151 | paint
152 | )
153 | }
154 | }
155 | }
156 |
157 | override fun getItemOffsets(
158 | outRect: Rect,
159 | view: View,
160 | parent: RecyclerView,
161 | state: RecyclerView.State
162 | ) {
163 | val layoutManager = parent.layoutManager
164 | require(layoutManager is androidx.recyclerview.widget.LinearLayoutManager) {
165 | "LinearLayoutManagerDivider can only use with LinearLayoutManager"
166 | }
167 | orientation = layoutManager.orientation
168 | // HeaderView和FooterView不设置偏移量
169 | if (isHeader(view) || isFooter(view)) {
170 | outRect.set(0, 0, 0, 0)
171 | return
172 | }
173 | // 排除HeaderView和FooterView后,计算真实列表中的ChildView数量
174 | val realItemCount = state.itemCount - headerViewList.size - footerViewList.size
175 | // 找到当前ChildView在真实列表中的position(从1数起)
176 | val positionInLinear = parent.getChildLayoutPosition(view) + 1 - headerViewList.size
177 | // 计算偏移量
178 | if (fullWrap) {
179 | outRect.set(getFullWrapOffsets(positionInLinear, realItemCount))
180 | } else {
181 | outRect.set(getNotFullWrapOffsets(positionInLinear, realItemCount))
182 | }
183 | // 第一次第二行item的偏移量后,重新计算第一行的item的偏移量
184 | // 因为第一行的item在第一次计算时,是当作同时是第一行和最后一行计算的
185 | // 所以开始出现第二行时,要重新计算一次第一行
186 | if (realItemCount == 2) {
187 | parent.postDelayed({
188 | for (i in headerViewList.size + 0 until headerViewList.size + 1) {
189 | parent.adapter?.notifyItemChanged(i)
190 | }
191 | }, 50)
192 | }
193 | }
194 |
195 | /**
196 | * 在真实列表中,全包裹的分割线,计算指定position(从1开始)在列表中应该设置的偏移量
197 | */
198 | private fun getFullWrapOffsets(position: Int, total: Int): Rect {
199 | val rect = Rect()
200 | var leftOffset = 0
201 | var topOffset = 0
202 | var rightOffset = 0
203 | var bottomOffset = 0
204 | // 上下偏移量
205 | if (total == 1) {
206 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
207 | // 同时是第一行和最后一行,即只有一行
208 | topOffset = dividerWidth
209 | bottomOffset = dividerWidth
210 | } else {
211 | // 同时是第一列和最后一列,即只有一列
212 | leftOffset = dividerWidth
213 | rightOffset = dividerWidth
214 | }
215 | } else if (position == 1) {
216 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
217 | // 第一行
218 | topOffset = dividerWidth
219 | bottomOffset = (1f / total * dividerWidth).roundToInt()
220 | } else {
221 | // 第一列
222 | leftOffset = dividerWidth
223 | rightOffset = (1f / total * dividerWidth).roundToInt()
224 | }
225 |
226 | } else if (position == total) {
227 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
228 | // 最后一行
229 | topOffset = (1f / total * dividerWidth).roundToInt()
230 | bottomOffset = dividerWidth
231 | } else {
232 | // 最后一列
233 | leftOffset = (1f / total * dividerWidth).roundToInt()
234 | rightOffset = dividerWidth
235 | }
236 | } else {
237 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
238 | // 中间行
239 | topOffset = ((total + 1f - position) / total * dividerWidth).roundToInt()
240 | bottomOffset = (position.toFloat() / total * dividerWidth).roundToInt()
241 | } else {
242 | // 中间列
243 | leftOffset = ((total + 1f - position) / total * dividerWidth).roundToInt()
244 | rightOffset = (position.toFloat() / total * dividerWidth).roundToInt()
245 | }
246 | }
247 | // 计算完毕
248 | rect.set(leftOffset, topOffset, rightOffset, bottomOffset)
249 | return rect
250 | }
251 |
252 | /**
253 | * 在真实列表中,非全包裹的分割线,计算指定position(从1开始)在列表中应该设置的偏移量
254 | */
255 | private fun getNotFullWrapOffsets(position: Int, total: Int): Rect {
256 | val rect = Rect()
257 | var leftOffset = 0
258 | var topOffset = 0
259 | var rightOffset = 0
260 | var bottomOffset = 0
261 | if (orientation == androidx.recyclerview.widget.LinearLayoutManager.VERTICAL) {
262 | // 上下偏移量
263 | when {
264 | total == 1 -> {
265 | // 同时是第一行和最后一行,即只有一行
266 | topOffset = 0
267 | bottomOffset = 0
268 | }
269 | position == 1 -> {
270 | // 第一行
271 | topOffset = 0
272 | bottomOffset = ((total - 1f) / total * dividerWidth).roundToInt()
273 | }
274 | position == total -> {
275 | // 最后一行
276 | topOffset = ((total - 1f) / total * dividerWidth).roundToInt()
277 | bottomOffset = 0
278 | }
279 | else -> {
280 | // 中间行
281 | topOffset = ((position - 1f) / total * dividerWidth).roundToInt()
282 | bottomOffset =
283 | (((total - 1f) - (position - 1f)) / total * dividerWidth).roundToInt()
284 | }
285 | }
286 | } else {
287 | // 左右偏移量
288 | when {
289 | total == 1 -> {
290 | // 同时是第一列和最后一列,即只有一列
291 | leftOffset = 0
292 | rightOffset = 0
293 | }
294 | position == 1 -> {
295 | // 第一列
296 | leftOffset = 0
297 | rightOffset = ((total - 1f) / total * dividerWidth).roundToInt()
298 | }
299 | position == total -> {
300 | // 最后一列
301 | leftOffset = ((total - 1f) / total * dividerWidth).roundToInt()
302 | rightOffset = 0
303 | }
304 | else -> {
305 | // 中间列
306 | leftOffset = ((position - 1f) / total * dividerWidth).roundToInt()
307 | rightOffset =
308 | (((total - 1f) - (position - 1f)) / total * dividerWidth).roundToInt()
309 | }
310 | }
311 | }
312 | // 计算完毕
313 | rect.set(leftOffset, topOffset, rightOffset, bottomOffset)
314 | return rect
315 | }
316 |
317 | /**
318 | * 不会对布局造成影响,仅用于分割线计算
319 | */
320 | fun addHeaderView(headerView: View) {
321 | headerViewList.add(headerView)
322 | }
323 |
324 | /**
325 | * 不会对布局造成影响,仅用于分割线计算
326 | */
327 | fun removeHeaderView(headerView: View) {
328 | headerViewList.remove(headerView)
329 | }
330 |
331 | /**
332 | * 不会对布局造成影响,仅用于分割线计算
333 | */
334 | fun addFooterView(footerView: View) {
335 | footerViewList.add(footerView)
336 | }
337 |
338 | /**
339 | * 不会对布局造成影响,仅用于分割线计算
340 | */
341 | fun removeFooterView(footerView: View) {
342 | footerViewList.remove(footerView)
343 | }
344 |
345 | private fun isHeader(view: View): Boolean {
346 | if (headerViewList.contains(view)) {
347 | return true
348 | }
349 | // 兼容Brvah的addHeaderView方法设置的Header
350 | if (view is ViewGroup) {
351 | for (i in 0 until view.childCount) {
352 | val chideView = view.getChildAt(0)
353 | if (headerViewList.contains(chideView)) {
354 | return true
355 | }
356 | }
357 | return false
358 | } else {
359 | return false
360 | }
361 | }
362 |
363 | private fun isFooter(view: View): Boolean {
364 | if (footerViewList.contains(view)) {
365 | return true
366 | }
367 | // 兼容Brvah的addFooterView方法设置的Footer
368 | if (view is ViewGroup) {
369 | for (i in 0 until view.childCount) {
370 | val chideView = view.getChildAt(0)
371 | if (footerViewList.contains(chideView)) {
372 | return true
373 | }
374 | }
375 | return false
376 | } else {
377 | return false
378 | }
379 | }
380 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/zeropercenthappy/divider/GridLayoutManagerDivider.kt:
--------------------------------------------------------------------------------
1 | package com.zeropercenthappy.divider
2 |
3 | import android.graphics.Canvas
4 | import android.graphics.Paint
5 | import android.graphics.Rect
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import androidx.annotation.ColorInt
9 | import androidx.recyclerview.widget.RecyclerView
10 | import kotlin.math.roundToInt
11 |
12 | class GridLayoutManagerDivider(
13 | @ColorInt dividerColor: Int,
14 | private val horizontalDividerHeight: Int,
15 | private val verticalDividerWidth: Int,
16 | private val fullWrap: Boolean
17 | ) : RecyclerView.ItemDecoration() {
18 |
19 |
20 | constructor(
21 | @ColorInt dividerColor: Int,
22 | dividerSize: Int,
23 | fullWrap: Boolean
24 | ) : this(dividerColor, dividerSize, dividerSize, fullWrap)
25 |
26 | private val paint: Paint = Paint()
27 | private val headerViewList = arrayListOf()
28 | private val footerViewList = arrayListOf()
29 |
30 | init {
31 | paint.isAntiAlias = true
32 | paint.style = Paint.Style.FILL
33 | paint.color = dividerColor
34 | }
35 |
36 | override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
37 | if (fullWrap) {
38 | drawFullWrap(canvas, parent)
39 | } else {
40 | drawNotFullWrap(canvas, parent, state)
41 | }
42 | }
43 |
44 | /**
45 | * 每个item都画满宽度的线
46 | * 以item的offset来决定最终显示出来的分割线
47 | */
48 | private fun drawFullWrap(canvas: Canvas, parent: RecyclerView) {
49 | for (i in 0 until parent.childCount) {
50 | val childView = parent.getChildAt(i)
51 | // HeaderView和FooterView不处理
52 | if (isHeader(childView) || isFooter(childView)) {
53 | continue
54 | }
55 | // 绘制分割线
56 | // 左边
57 | canvas.drawRect(
58 | childView.left.toFloat() - verticalDividerWidth,
59 | childView.top.toFloat() - horizontalDividerHeight,
60 | childView.left.toFloat(),
61 | childView.bottom.toFloat() + horizontalDividerHeight,
62 | paint
63 | )
64 | // 上边
65 | canvas.drawRect(
66 | childView.left.toFloat() - verticalDividerWidth,
67 | childView.top.toFloat() - horizontalDividerHeight,
68 | childView.right.toFloat() + verticalDividerWidth,
69 | childView.top.toFloat(), paint
70 | )
71 | // 右边
72 | canvas.drawRect(
73 | childView.right.toFloat(),
74 | childView.top.toFloat() - horizontalDividerHeight,
75 | childView.right.toFloat() + verticalDividerWidth,
76 | childView.bottom.toFloat() + horizontalDividerHeight,
77 | paint
78 | )
79 | // 下边
80 | canvas.drawRect(
81 | childView.left.toFloat() - verticalDividerWidth,
82 | childView.bottom.toFloat(),
83 | childView.right.toFloat() + verticalDividerWidth,
84 | childView.bottom.toFloat() + horizontalDividerHeight,
85 | paint
86 | )
87 | }
88 | }
89 |
90 | private fun drawNotFullWrap(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
91 | for (i in 0 until parent.childCount) {
92 | val childView = parent.getChildAt(i)
93 | // HeaderView和FooterView不处理
94 | if (isHeader(childView) || isFooter(childView)) {
95 | continue
96 | }
97 | // 排除HeaderView和FooterView后,计算真实表格中的ChildView数量
98 | val realItemCount = state.itemCount - headerViewList.size - footerViewList.size
99 | val spanCount = getSpanCount(parent)
100 | // 找到当前ChildView在真实表格中的position(从1数起)
101 | val positionInGrid = parent.getChildLayoutPosition(childView) + 1 - headerViewList.size
102 | // 根据偏移量绘制分割线
103 | val offsetRect = getNotFullWrapOffSets(positionInGrid, spanCount, realItemCount)
104 | // 左边
105 | canvas.drawRect(
106 | (childView.left - offsetRect.left).toFloat(),
107 | (childView.top - offsetRect.top).toFloat(),
108 | childView.left.toFloat(),
109 | (childView.bottom + offsetRect.bottom).toFloat(),
110 | paint
111 | )
112 | // 右边
113 | // 分割线的右端绘制到从item的右侧+分割线宽度为止,以解决未排满spanCount时的分割线绘制不准确问题
114 | // 右边的分割线只在非最后一个item才绘制
115 | canvas.drawRect(
116 | childView.right.toFloat(),
117 | (childView.top - offsetRect.top).toFloat(),
118 | (childView.right + verticalDividerWidth).toFloat(),
119 | (childView.bottom + offsetRect.bottom).toFloat(),
120 | paint
121 | )
122 | // 上边
123 | // 分割线的右端绘制到从item的右侧+分割线宽度为止,以解决未排满spanCount时的分割线绘制不准确问题
124 | canvas.drawRect(
125 | (childView.left - offsetRect.left).toFloat(),
126 | (childView.top - offsetRect.top).toFloat(),
127 | (childView.right + horizontalDividerHeight).toFloat(),
128 | childView.top.toFloat(),
129 | paint
130 | )
131 | // 下边
132 | if (!isLastRow(positionInGrid, spanCount, realItemCount)) {
133 | // 下边的分割线只在非最后一行才绘制
134 | // 并且绘制满宽度分割线,以解决未排满spanCount时的分割线绘制不准确问题
135 | canvas.drawRect(
136 | (childView.left - offsetRect.left).toFloat(),
137 | childView.bottom.toFloat(),
138 | (childView.right + offsetRect.right).toFloat(),
139 | (childView.bottom + horizontalDividerHeight).toFloat(),
140 | paint
141 | )
142 | }
143 | }
144 | }
145 |
146 | override fun getItemOffsets(
147 | outRect: Rect,
148 | view: View,
149 | parent: RecyclerView,
150 | state: RecyclerView.State
151 | ) {
152 | val layoutManager = parent.layoutManager
153 | require(layoutManager is androidx.recyclerview.widget.GridLayoutManager && layoutManager.orientation == androidx.recyclerview.widget.GridLayoutManager.VERTICAL) {
154 | "GridLayoutManagerDivider can only use with vertical GridLayoutManager"
155 | }
156 | // HeaderView和FooterView不设置偏移量
157 | if (isHeader(view) || isFooter(view)) {
158 | outRect.set(0, 0, 0, 0)
159 | return
160 | }
161 | // 排除HeaderView和FooterView后,计算真实表格中的ChildView数量
162 | val realItemCount = state.itemCount - headerViewList.size - footerViewList.size
163 | val spanCount = getSpanCount(parent)
164 |
165 | // 找到当前ChildView在真实表格中的position(从1数起)
166 | val positionInGrid = parent.getChildLayoutPosition(view) + 1 - headerViewList.size
167 | // 计算偏移量
168 | if (fullWrap) {
169 | outRect.set(getFullWrapOffsets(positionInGrid, spanCount, realItemCount))
170 | } else {
171 | outRect.set(getNotFullWrapOffSets(positionInGrid, spanCount, realItemCount))
172 | }
173 | // 第一次计算真实表格第二行第一个item的偏移量后,重新计算第一行的item的偏移量
174 | // 因为第一行的item在第一次计算时,是当作同时是第一行和最后一行计算的
175 | // 所以开始出现第二行时,要重新计算一次第一行
176 | if (realItemCount == spanCount + 1) {
177 | parent.postDelayed({
178 | for (i in headerViewList.size + 0 until headerViewList.size + spanCount) {
179 | parent.adapter?.notifyItemChanged(i)
180 | }
181 | }, 50)
182 | }
183 | }
184 |
185 | /**
186 | * 在真实表格中,全包裹的分割线,计算指定position(从1开始)在表格中应该设置的偏移量
187 | */
188 | private fun getFullWrapOffsets(position: Int, spanCount: Int, total: Int): Rect {
189 | val rect = Rect()
190 | val rowCount = getRowCount(spanCount, total)
191 | var leftOffset = 0
192 | var topOffset = 0
193 | var rightOffset = 0
194 | var bottomOffset = 0
195 | // 上下偏移量
196 | if (isFirstRow(position, spanCount) && isLastRow(position, spanCount, total)) {
197 | // 同时是第一行和最后一行,即只有一行
198 | topOffset = horizontalDividerHeight
199 | bottomOffset = horizontalDividerHeight
200 | } else if (isFirstRow(position, spanCount)) {
201 | // 第一行
202 | topOffset = horizontalDividerHeight
203 | bottomOffset = (1f / rowCount * horizontalDividerHeight).roundToInt()
204 | } else if (isLastRow(position, spanCount, total)) {
205 | // 最后一行
206 | topOffset = (1f / rowCount * horizontalDividerHeight).roundToInt()
207 | bottomOffset = horizontalDividerHeight
208 | } else {
209 | // 中间行
210 | val atRow = atRow(position, spanCount)
211 | topOffset = ((rowCount + 1f - atRow) / rowCount * horizontalDividerHeight).roundToInt()
212 | bottomOffset = (atRow.toFloat() / rowCount * horizontalDividerHeight).roundToInt()
213 | }
214 | // 左右偏移量
215 | if (isFirstColumn(position, spanCount) && isLastColumn(position, spanCount, total)) {
216 | // 同时是第一列和最后一列,即只有一列
217 | leftOffset = verticalDividerWidth
218 | rightOffset = verticalDividerWidth
219 | } else if (isFirstColumn(position, spanCount)) {
220 | // 第一列
221 | leftOffset = verticalDividerWidth
222 | rightOffset = (1f / spanCount * verticalDividerWidth).roundToInt()
223 | } else if (isLastColumn(position, spanCount, total, false)) {
224 | // 最后一列
225 | leftOffset = (1f / spanCount * verticalDividerWidth).roundToInt()
226 | rightOffset = verticalDividerWidth
227 | } else {
228 | // 中间列
229 | val atColumn = atColumn(position, spanCount)
230 | leftOffset =
231 | ((spanCount + 1f - atColumn) / spanCount * verticalDividerWidth).roundToInt()
232 | rightOffset = (atColumn.toFloat() / spanCount * verticalDividerWidth).roundToInt()
233 | }
234 | // 计算完毕
235 | rect.set(leftOffset, topOffset, rightOffset, bottomOffset)
236 | return rect
237 | }
238 |
239 | /**
240 | * 在真实表格中,非全包裹的分割线,计算指定position(从1开始)在表格中应该设置的偏移量
241 | */
242 | private fun getNotFullWrapOffSets(position: Int, spanCount: Int, total: Int): Rect {
243 | val rect = Rect()
244 | val rowCount = getRowCount(spanCount, total)
245 |
246 | var leftOffset = 0
247 | var topOffset = 0
248 | var rightOffset = 0
249 | var bottomOffset = 0
250 |
251 | // 上下偏移量
252 | if (isFirstRow(position, spanCount) && isLastRow(position, spanCount, total)) {
253 | // 同时是第一行和最后一行,即只有一行
254 | topOffset = 0
255 | bottomOffset = 0
256 | } else if (isFirstRow(position, spanCount)) {
257 | // 第一行
258 | topOffset = 0
259 | bottomOffset = ((rowCount - 1f) / rowCount * horizontalDividerHeight).roundToInt()
260 | } else if (isLastRow(position, spanCount, total)) {
261 | // 最后一行
262 | topOffset = ((rowCount - 1f) / rowCount * horizontalDividerHeight).roundToInt()
263 | bottomOffset = 0
264 | } else {
265 | // 中间行
266 | val atRow = atRow(position, spanCount)
267 | topOffset = ((atRow - 1f) / rowCount * horizontalDividerHeight).roundToInt()
268 | bottomOffset =
269 | (((rowCount - 1f) - (atRow - 1f)) / rowCount * horizontalDividerHeight).roundToInt()
270 | }
271 | // 左右偏移量
272 | if (isFirstColumn(position, spanCount) && isLastColumn(position, spanCount, total)) {
273 | // 同时是第一列和最后一列,即只有一列
274 | leftOffset = 0
275 | rightOffset = 0
276 | } else if (isFirstColumn(position, spanCount)) {
277 | // 第一列
278 | leftOffset = 0
279 | rightOffset = ((spanCount - 1f) / spanCount * verticalDividerWidth).roundToInt()
280 | } else if (isLastColumn(position, spanCount, total, false)) {
281 | // 最后一列
282 | leftOffset = ((spanCount - 1f) / spanCount * verticalDividerWidth).roundToInt()
283 | rightOffset = 0
284 | } else {
285 | // 中间列
286 | val atColumn = atColumn(position, spanCount)
287 | leftOffset = ((atColumn - 1f) / spanCount * verticalDividerWidth).roundToInt()
288 | rightOffset =
289 | (((spanCount - 1f) - (atColumn - 1f)) / spanCount * verticalDividerWidth).roundToInt()
290 | }
291 |
292 | // 计算完毕
293 | rect.set(leftOffset, topOffset, rightOffset, bottomOffset)
294 | return rect
295 | }
296 |
297 | /**
298 | * 计算真实表格的行数
299 | */
300 | private fun getRowCount(spanCount: Int, total: Int): Int {
301 | return when {
302 | total <= spanCount -> {
303 | // 小于一行
304 | 1
305 | }
306 | total % spanCount != 0 -> {
307 | // 多于一行且未排满定义的spanCount
308 | total / spanCount + 1
309 | }
310 | else -> {
311 | // 多于一行且刚好排满定义的spanCount
312 | total / spanCount
313 | }
314 |
315 | }
316 | }
317 |
318 | /**
319 | * 在真实表格中,判断position(从1开始)是否在第一行
320 | */
321 | private fun isFirstRow(position: Int, spanCount: Int): Boolean {
322 | return position <= spanCount
323 | }
324 |
325 | /**
326 | * 在真实表格中,判断position(从1开始)是否在最后一行
327 | */
328 | private fun isLastRow(position: Int, spanCount: Int, total: Int): Boolean {
329 | return when {
330 | total <= spanCount -> {
331 | // 只有一行
332 | true
333 | }
334 | total % spanCount == 0 -> {
335 | // 多于一行
336 | // 总数刚好将列数排满
337 | position > (total / spanCount - 1) * spanCount
338 | }
339 | else -> {
340 | // 多于一行
341 | // 总数不够将列数排满
342 | // 利用int/int向下取整,得到排满的行数,再乘以spanCount,算出最后一行的条件
343 | position > (total / spanCount) * spanCount
344 | }
345 |
346 | }
347 | }
348 |
349 | /**
350 | * 在真实表格中,计算指定position(从1开始)在第几行
351 | */
352 | private fun atRow(position: Int, spanCount: Int): Int {
353 | return if (position % spanCount == 0) {
354 | position / spanCount
355 | } else {
356 | position / spanCount + 1
357 | }
358 | }
359 |
360 | /**
361 | * 在真实表格中,判断position(从1开始)是否在第一列
362 | */
363 | private fun isFirstColumn(position: Int, spanCount: Int): Boolean {
364 | return when (spanCount) {
365 | 1 -> {
366 | // 只有一列
367 | true
368 | }
369 | else -> {
370 | position % spanCount == 1
371 | }
372 | }
373 | }
374 |
375 | /**
376 | * 在真实表格中,判断position(从1开始)是否在最后一列
377 | * @param isCountLastItem 是否将最后一个item不是最后一列来当作最后一列处理
378 | */
379 | private fun isLastColumn(
380 | position: Int,
381 | spanCount: Int,
382 | total: Int,
383 | isCountLastItem: Boolean = false
384 | ): Boolean {
385 | return when {
386 | spanCount == 1 -> {
387 | // 只有一列
388 | true
389 | }
390 | position % spanCount == 0 -> {
391 | // 大于一列
392 | // position刚好处于定义最后一列上
393 | true
394 | }
395 | (position == total && isCountLastItem) -> {
396 | // 大于一列
397 | // position是最后一个item
398 | true
399 | }
400 | else -> {
401 | false
402 | }
403 | }
404 | }
405 |
406 | /**
407 | * 在真实表格中,判断position(从1开始)在第几列
408 | */
409 | private fun atColumn(position: Int, spanCount: Int): Int {
410 | return when {
411 | position % spanCount != 0 -> {
412 | // 非最后一列
413 | position % spanCount
414 | }
415 | else -> {
416 | // 最后一列
417 | spanCount
418 | }
419 | }
420 | }
421 |
422 | private fun getSpanCount(parent: RecyclerView): Int {
423 | val spanCount: Int
424 | val layoutManager = parent.layoutManager
425 | spanCount = (layoutManager as androidx.recyclerview.widget.GridLayoutManager).spanCount
426 | return spanCount
427 | }
428 |
429 | /**
430 | * 不会对布局造成影响,仅用于分割线计算
431 | */
432 | fun addHeaderView(headerView: View) {
433 | headerViewList.add(headerView)
434 | }
435 |
436 | /**
437 | * 不会对布局造成影响,仅用于分割线计算
438 | */
439 | fun removeHeaderView(headerView: View) {
440 | headerViewList.remove(headerView)
441 | }
442 |
443 | /**
444 | * 不会对布局造成影响,仅用于分割线计算
445 | */
446 | fun addFooterView(footerView: View) {
447 | footerViewList.add(footerView)
448 | }
449 |
450 | /**
451 | * 不会对布局造成影响,仅用于分割线计算
452 | */
453 | fun removeFooterView(footerView: View) {
454 | footerViewList.remove(footerView)
455 | }
456 |
457 | private fun isHeader(view: View): Boolean {
458 | if (headerViewList.contains(view)) {
459 | return true
460 | }
461 | // 兼容Brvah的addHeaderView方法设置的Header
462 | if (view is ViewGroup) {
463 | for (i in 0 until view.childCount) {
464 | val chideView = view.getChildAt(0)
465 | if (headerViewList.contains(chideView)) {
466 | return true
467 | }
468 | }
469 | return false
470 | } else {
471 | return false
472 | }
473 | }
474 |
475 | private fun isFooter(view: View): Boolean {
476 | if (footerViewList.contains(view)) {
477 | return true
478 | }
479 | // 兼容Brvah的addFooterView方法设置的Header
480 | if (view is ViewGroup) {
481 | for (i in 0 until view.childCount) {
482 | val chideView = view.getChildAt(0)
483 | if (footerViewList.contains(chideView)) {
484 | return true
485 | }
486 | }
487 | return false
488 | } else {
489 | return false
490 | }
491 | }
492 | }
--------------------------------------------------------------------------------