├── 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
│ │ │ ├── layout
│ │ │ │ ├── item_2.xml
│ │ │ │ ├── item_1.xml
│ │ │ │ ├── item_1_origin.xml
│ │ │ │ ├── item_2_origin.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── menu
│ │ │ │ └── toolbar_menu.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── joker
│ │ │ └── recyclerviewtest
│ │ │ ├── ArrayListWrapper.java
│ │ │ ├── Item2.java
│ │ │ ├── Item1.java
│ │ │ ├── RecyclerViewWrapper.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── joker
│ │ │ └── recyclerviewtest
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── joker
│ │ └── recyclerviewtest
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── apk
└── app-debug.apk
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/apk/app-debug.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/apk/app-debug.apk
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RecyclerViewTest
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [手摸手第二弹,可视化 RecyclerView 缓存机制](https://juejin.im/post/5a5d3d9b518825734216e1e8)
2 |
3 | [about ListView](https://github.com/jokermonn/ListViewVisualization)
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jokermonn/RecyclerViewVisualization/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/jokermonn/RecyclerViewVisualization/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/jokermonn/RecyclerViewVisualization/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/jokermonn/RecyclerViewVisualization/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/jokermonn/RecyclerViewVisualization/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Jan 07 22:09:45 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFC107
4 | #FFA000
5 | #FFC107
6 | #00796B
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/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/test/java/com/joker/recyclerviewtest/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
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_1_origin.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_2_origin.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/toolbar_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
21 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/joker/recyclerviewtest/ArrayListWrapper.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class ArrayListWrapper extends ArrayList {
6 | public int maxSize = 0;
7 | public boolean canReset = true;
8 | private int lastSize = 0;
9 |
10 | @Override public boolean remove(Object o) {
11 | if (size() > maxSize) {
12 | maxSize = size();
13 | canReset = false;
14 | }
15 | if (size() == 0) {
16 | canReset = true;
17 | }
18 | return super.remove(o);
19 | }
20 |
21 | @Override public boolean add(T t) {
22 | if (canReset) {
23 | if (size() + 1 > lastSize) {
24 | maxSize = size() + 1;
25 | }
26 | }
27 | return super.add(t);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/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/androidTest/java/com/joker/recyclerviewtest/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
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.joker.recyclerviewtest", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/joker/recyclerviewtest/Item2.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.Nullable;
5 | import android.util.AttributeSet;
6 | import android.widget.LinearLayout;
7 | import android.widget.TextView;
8 |
9 | public class Item2 extends LinearLayout {
10 | private TextView tv;
11 |
12 | public Item2(Context context) {
13 | this(context, null);
14 | }
15 |
16 | public Item2(Context context,
17 | @Nullable AttributeSet attrs) {
18 | this(context, attrs, 0);
19 | }
20 |
21 | public Item2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
22 | super(context, attrs, defStyleAttr);
23 | inflate(context, R.layout.item_2_origin, this);
24 | tv = findViewById(R.id.tv_content);
25 | }
26 |
27 | @Override public String toString() {
28 | return "Item2---" + tv.getText();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/joker/recyclerviewtest/Item1.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.Nullable;
5 | import android.util.AttributeSet;
6 | import android.widget.LinearLayout;
7 | import android.widget.TextView;
8 |
9 | public class Item1 extends LinearLayout {
10 | private TextView tv;
11 |
12 | public Item1(Context context) {
13 | this(context, null);
14 | }
15 |
16 | public Item1(Context context,
17 | @Nullable AttributeSet attrs) {
18 | this(context, attrs, 0);
19 | }
20 |
21 | public Item1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
22 | super(context, attrs, defStyleAttr);
23 | inflate(context, R.layout.item_1_origin, this);
24 | tv = findViewById(R.id.tv_content);
25 | }
26 |
27 | @Override public String toString() {
28 | return "Item1---『" + tv.getText() + "』";
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.joker.recyclerviewtest"
7 | minSdkVersion 21
8 | targetSdkVersion 26
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.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:26.1.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
28 | implementation 'com.android.support:recyclerview-v7:26.1.0'
29 | }
30 |
--------------------------------------------------------------------------------
/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/joker/recyclerviewtest/RecyclerViewWrapper.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.util.AttributeSet;
7 | import java.lang.reflect.Field;
8 | import java.util.ArrayList;
9 |
10 | public class RecyclerViewWrapper extends RecyclerView {
11 | private LayoutListener listener;
12 |
13 | public RecyclerViewWrapper(Context context) {
14 | super(context);
15 | }
16 |
17 | public RecyclerViewWrapper(Context context,
18 | @Nullable AttributeSet attrs) {
19 | super(context, attrs);
20 | }
21 |
22 | public RecyclerViewWrapper(Context context, @Nullable AttributeSet attrs, int defStyle) {
23 | super(context, attrs, defStyle);
24 | }
25 |
26 | public interface LayoutListener {
27 | void beforeLayout();
28 |
29 | void afterLayout();
30 | }
31 |
32 | public void setLayoutListener(LayoutListener listener) {
33 | this.listener = listener;
34 | }
35 |
36 | @SuppressWarnings("unchecked")
37 | public void reflectObject(RecyclerViewWrapper recyclerViewWrapper) {
38 | try {
39 | Field mRecycler =
40 | Class.forName("android.support.v7.widget.RecyclerView").getDeclaredField("mRecycler");
41 | mRecycler.setAccessible(true);
42 | RecyclerView.Recycler recyclerInstance =
43 | (RecyclerView.Recycler) mRecycler.get(recyclerViewWrapper);
44 |
45 | Class> recyclerClass = Class.forName(mRecycler.getType().getName());
46 | Field mAttachedScrap = recyclerClass.getDeclaredField("mAttachedScrap");
47 | mAttachedScrap.setAccessible(true);
48 | mAttachedScrap.set(recyclerInstance, new ArrayListWrapper());
49 |
50 | ArrayList mAttached =
51 | (ArrayList) mAttachedScrap.get(recyclerInstance);
52 | } catch (Exception e) {
53 | e.printStackTrace();
54 | }
55 | }
56 |
57 | @Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
58 | if (listener != null) {
59 | listener.beforeLayout();
60 | }
61 | super.onLayout(changed, l, t, r, b);
62 | if (listener != null) {
63 | listener.afterLayout();
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
25 |
26 |
34 |
35 |
46 |
47 |
53 |
54 |
60 |
61 |
62 |
63 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
8 |
11 |
16 |
21 |
26 |
31 |
36 |
41 |
46 |
51 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
91 |
96 |
101 |
106 |
111 |
116 |
121 |
126 |
131 |
136 |
141 |
146 |
151 |
156 |
161 |
166 |
171 |
172 |
--------------------------------------------------------------------------------
/app/src/main/java/com/joker/recyclerviewtest/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.joker.recyclerviewtest;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.graphics.Canvas;
5 | import android.graphics.Rect;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.os.Bundle;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.support.v7.widget.Toolbar;
11 | import android.util.SparseArray;
12 | import android.view.LayoutInflater;
13 | import android.view.Menu;
14 | import android.view.MenuItem;
15 | import android.view.View;
16 | import android.view.ViewGroup;
17 | import android.widget.ScrollView;
18 | import android.widget.TextView;
19 | import java.lang.reflect.Field;
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | /**
24 | * Create by @author jokermonn
25 | */
26 | public class MainActivity extends AppCompatActivity {
27 | private List data = new ArrayList<>(32);
28 | private TextView mBindInfo;
29 | private ScrollView mSV;
30 | private Adapter mAdapter;
31 | private RecyclerViewWrapper mRv;
32 | private TextView mTv;
33 | private int refreshCount = 0;
34 |
35 | {
36 | for (int i = 1; i < 33; i++) {
37 | data.add((char) (i + 64));
38 | }
39 | }
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_main);
45 | Toolbar toolbar = findViewById(R.id.tb_title);
46 | // 如果你要试试刷新机制的话
47 | // toolbar.setVisibility(View.VISIBLE);
48 | setSupportActionBar(toolbar);
49 | mRv = findViewById(R.id.rv_content);
50 | mTv = findViewById(R.id.tv_info);
51 | mBindInfo = findViewById(R.id.tv_bind_info);
52 | mSV = findViewById(R.id.sv_content);
53 | findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
54 | @Override public void onClick(View v) {
55 | mBindInfo.setText("");
56 | }
57 | });
58 | mAdapter = new Adapter();
59 | mRv.setAdapter(mAdapter);
60 | final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
61 | mRv.setLayoutListener(new RecyclerViewWrapper.LayoutListener() {
62 | @SuppressWarnings("unchecked") @Override public void beforeLayout() {
63 | try {
64 | Field mRecycler =
65 | Class.forName("android.support.v7.widget.RecyclerView").getDeclaredField("mRecycler");
66 | mRecycler.setAccessible(true);
67 | RecyclerView.Recycler recyclerInstance =
68 | (RecyclerView.Recycler) mRecycler.get(mRv);
69 |
70 | Class> recyclerClass = Class.forName(mRecycler.getType().getName());
71 | Field mAttachedScrap = recyclerClass.getDeclaredField("mAttachedScrap");
72 | mAttachedScrap.setAccessible(true);
73 | mAttachedScrap.set(recyclerInstance, new ArrayListWrapper());
74 |
75 | ArrayList mAttached =
76 | (ArrayList) mAttachedScrap.get(recyclerInstance);
77 | } catch (Exception e) {
78 | e.printStackTrace();
79 | }
80 | }
81 |
82 | @Override
83 | public void afterLayout() {
84 | // 第二种方式:添加在这里
85 | //rv.getRecycledViewPool().setMaxRecycledViews(0, 8);
86 | //for (int i = 0; i < 8; i++) {
87 | // Adapter.ViewHolder viewHolder = adapter.createViewHolder(rv, 0);
88 | // rv.getRecycledViewPool().putRecycledView(viewHolder);
89 | //}
90 | showMessage(mRv, mTv, layoutManager);
91 | }
92 | });
93 | mRv.addItemDecoration(new ItemDecoration());
94 | mRv.setLayoutManager(layoutManager);
95 | mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
96 | @Override
97 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
98 | showMessage();
99 | }
100 | });
101 | // 第一种方式:添加在这里
102 | //rv.getRecycledViewPool().setMaxRecycledViews(0, 8);
103 | //for (int i = 0; i < 8; i++) {
104 | // Adapter.ViewHolder viewHolder = adapter.createViewHolder(rv, 0);
105 | // rv.getRecycledViewPool().putRecycledView(viewHolder);
106 | //}
107 | }
108 |
109 | private void showMessage() {
110 | showMessage(mRv, mTv, (LinearLayoutManager) mRv.getLayoutManager());
111 | mSV.fullScroll(ScrollView.FOCUS_DOWN);
112 | }
113 |
114 | @Override public boolean onCreateOptionsMenu(Menu menu) {
115 | getMenuInflater().inflate(R.menu.toolbar_menu, menu);
116 | return true;
117 | }
118 |
119 | @Override public boolean onOptionsItemSelected(MenuItem item) {
120 | data.add(1, (char) ((refreshCount++) + 33));
121 | switch (item.getItemId()) {
122 | case R.id.part_refresh:
123 | mAdapter.notifyItemInserted(1);
124 | return true;
125 | case R.id.all_refresh:
126 | mAdapter.notifyDataSetChanged();
127 | return true;
128 | }
129 | return false;
130 | }
131 |
132 | @SuppressLint("SetTextI18n") @SuppressWarnings("unchecked")
133 | private void showMessage(RecyclerViewWrapper rv, TextView tv,
134 | LinearLayoutManager layoutManager) {
135 | try {
136 | Field mRecycler =
137 | Class.forName("android.support.v7.widget.RecyclerView").getDeclaredField("mRecycler");
138 | mRecycler.setAccessible(true);
139 | RecyclerView.Recycler recyclerInstance = (RecyclerView.Recycler) mRecycler.get(rv);
140 |
141 | Class> recyclerClass = Class.forName(mRecycler.getType().getName());
142 | Field mViewCacheMax = recyclerClass.getDeclaredField("mViewCacheMax");
143 | Field mAttachedScrap = recyclerClass.getDeclaredField("mAttachedScrap");
144 | Field mChangedScrap = recyclerClass.getDeclaredField("mChangedScrap");
145 | Field mCachedViews = recyclerClass.getDeclaredField("mCachedViews");
146 | Field mRecyclerPool = recyclerClass.getDeclaredField("mRecyclerPool");
147 | mViewCacheMax.setAccessible(true);
148 | mAttachedScrap.setAccessible(true);
149 | mChangedScrap.setAccessible(true);
150 | mCachedViews.setAccessible(true);
151 | mRecyclerPool.setAccessible(true);
152 |
153 | int mViewCacheSize = (int) mViewCacheMax.get(recyclerInstance);
154 | ArrayListWrapper mAttached =
155 | (ArrayListWrapper) mAttachedScrap.get(recyclerInstance);
156 | ArrayList mChanged =
157 | (ArrayList) mChangedScrap.get(recyclerInstance);
158 | ArrayList mCached =
159 | (ArrayList) mCachedViews.get(recyclerInstance);
160 | RecyclerView.RecycledViewPool recycledViewPool =
161 | (RecyclerView.RecycledViewPool) mRecyclerPool.get(recyclerInstance);
162 |
163 | Class> recyclerPoolClass = Class.forName(mRecyclerPool.getType().getName());
164 | tv.setText(
165 | "mAttachedScrap(一缓) size is:"
166 | + mAttached.maxSize
167 | + "\n"
168 | + "mCachedViews(二缓) max size is:"
169 | + mViewCacheSize
170 | + "\n"
171 | + getMChangedScrapViewsInfo(mChanged)
172 | + getMCacheViewTargetPositionInfo(rv, layoutManager)
173 | + getMCachedViewsInfo(mCached)
174 | + getRVPoolInfo(recyclerPoolClass, recycledViewPool)
175 | );
176 | } catch (Exception e) {
177 | e.printStackTrace();
178 | }
179 | }
180 |
181 | private String getMCacheViewTargetPositionInfo(RecyclerView recycledView,
182 | LinearLayoutManager layoutManager) {
183 | int lastItemPosition = layoutManager.findLastVisibleItemPosition();
184 | int firstItemPosition = layoutManager.findFirstVisibleItemPosition();
185 |
186 | try {
187 | Field mAdapterHelper = Class.forName("android.support.v7.widget.RecyclerView")
188 | .getDeclaredField("mAdapterHelper");
189 | mAdapterHelper.setAccessible(true);
190 | Object o = mAdapterHelper.get(recycledView);
191 |
192 | Field mLayoutStateField = layoutManager.getClass().getDeclaredField("mLayoutState");
193 | mLayoutStateField.setAccessible(true);
194 | Class> layoutStateClass =
195 | Class.forName("android.support.v7.widget.LinearLayoutManager$LayoutState");
196 | Field mCurrentPositionField = layoutStateClass.getDeclaredField("mCurrentPosition");
197 | mCurrentPositionField.setAccessible(true);
198 |
199 | return "target mCachedView position:" + mCurrentPositionField.get(
200 | mLayoutStateField.get(recycledView.getLayoutManager())) + "\n";
201 | } catch (Exception e) {
202 | e.printStackTrace();
203 | }
204 | return "";
205 | }
206 |
207 | @SuppressLint("SetTextI18n") public void appendText(final String append) {
208 | mBindInfo.post(new Runnable() {
209 | @Override public void run() {
210 | mBindInfo.setText(mBindInfo.getText() + append);
211 | mSV.fullScroll(ScrollView.FOCUS_DOWN);
212 | }
213 | });
214 | }
215 |
216 | @SuppressWarnings("unchecked")
217 | private String getRVPoolInfo(Class> aClass, RecyclerView.RecycledViewPool recycledViewPool) {
218 | try {
219 | Field mScrapField = aClass.getDeclaredField("mScrap");
220 | mScrapField.setAccessible(true);
221 | SparseArray mScrap = (SparseArray) mScrapField.get(recycledViewPool);
222 |
223 | Class> scrapDataClass =
224 | Class.forName("android.support.v7.widget.RecyclerView$RecycledViewPool$ScrapData");
225 | Field mScrapHeapField = scrapDataClass.getDeclaredField("mScrapHeap");
226 | Field mMaxScrapField = scrapDataClass.getDeclaredField("mMaxScrap");
227 | mScrapHeapField.setAccessible(true);
228 | mMaxScrapField.setAccessible(true);
229 | String s = "mRecyclerPool(四缓) info:\n";
230 | for (int i = 0; i < mScrap.size(); i++) {
231 | ArrayList item =
232 | (ArrayList) mScrapHeapField.get(mScrap.get(i));
233 | for (int j = 0; j < item.size(); j++) {
234 | if (j == item.size() - 1) {
235 | s += ">>> ";
236 | } else if (j == 0) {
237 | s += "mScrap[" + i + "] max size is:" + (mMaxScrapField.get(mScrap.get(i))) + "\n";
238 | }
239 | s += "mScrap[" + i + "] 中的 mScrapHeap[" + j + "] info is:" + item.get(j) + "\n";
240 | }
241 | s += "\n";
242 | }
243 | return s;
244 | } catch (Exception e) {
245 | e.printStackTrace();
246 | return "\n";
247 | }
248 | }
249 |
250 | private String getMCachedViewsInfo(ArrayList viewHolders) {
251 | String s = "mCachedViews(二缓) info:\n";
252 | if (viewHolders.size() > 0) {
253 | int i = 0;
254 | for (; i < viewHolders.size(); i++) {
255 | s += "mCachedViews[" + i + "] is " + viewHolders.get(i).toString() + "\n";
256 | }
257 |
258 | // append \n
259 | if (i == 0) {
260 | s += "\n\n\n";
261 | } else if (i == 1) {
262 | s += "\n\n";
263 | } else if (i == 2) {
264 | s += "\n";
265 | }
266 | } else {
267 | s += "\n\n\n";
268 | }
269 | return s
270 | + "\n";
271 | }
272 |
273 | private String getMChangedScrapViewsInfo(ArrayList viewHolders) {
274 | if (viewHolders != null) {
275 | String s = "";
276 | for (int i = 0; i < viewHolders.size(); i++) {
277 | if (i == 0) {
278 | s += "mChangedScrap info:\n";
279 | }
280 | s += "mChangedScrap[" + i + "] is " + viewHolders.get(i).toString();
281 | }
282 | return "mChangedScrap size is: "
283 | + viewHolders.size()
284 | + "\n"
285 | + s;
286 | }
287 | return "";
288 | }
289 |
290 | class Adapter extends RecyclerView.Adapter {
291 | final int TYPE_1 = 0;
292 | final int TYPE_2 = 1;
293 |
294 | @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
295 | appendText(viewType == TYPE_1 ? ""
296 | + "\nItem1 createViewHolder" : "\nItem2 createViewHolder");
297 | return new ViewHolder(
298 | LayoutInflater.from(MainActivity.this)
299 | .inflate(
300 | viewType == TYPE_1 ? R.layout.item_1 : R.layout.item_2,
301 | parent,
302 | false
303 | )
304 | );
305 | }
306 |
307 | @SuppressLint("SetTextI18n") @Override
308 | public void onBindViewHolder(ViewHolder holder, int position) {
309 | String targetText = data.get(position) + "(layoutPosition:" + position + ")";
310 | appendText("\n『" + targetText + "』 BindViewHolder");
311 | holder.tv.setText(targetText);
312 | }
313 |
314 | @Override public int getItemCount() {
315 | return data.size();
316 | }
317 |
318 | @Override public int getItemViewType(int position) {
319 | return isFirstViewType(position) ? TYPE_1 : TYPE_2;
320 | }
321 |
322 | private boolean isFirstViewType(int position) {
323 | return position < 17;
324 | }
325 |
326 | class ViewHolder extends RecyclerView.ViewHolder {
327 | TextView tv;
328 |
329 | ViewHolder(View itemView) {
330 | super(itemView);
331 | tv = itemView.findViewById(R.id.tv_content);
332 | }
333 |
334 | @Override public String toString() {
335 | return itemView.toString();
336 | }
337 | }
338 | }
339 |
340 | class ItemDecoration extends RecyclerView.ItemDecoration {
341 | ItemDecoration() {
342 | }
343 |
344 | @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
345 | RecyclerView.State state) {
346 | outRect.set(0, 0, 0, 2);
347 | }
348 |
349 | @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
350 | super.onDraw(c, parent, state);
351 | c.drawColor(0xFFEEEEEE);
352 | }
353 | }
354 | }
355 |
--------------------------------------------------------------------------------