├── 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 │ │ │ │ ├── holder_tab.xml │ │ │ │ ├── holder_item3.xml │ │ │ │ ├── holder_item2.xml │ │ │ │ ├── holder_item1.xml │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── nestrecyclerview │ │ │ │ └── demo2 │ │ │ │ ├── bean │ │ │ │ ├── MainTabBean.kt │ │ │ │ └── MainListBean.kt │ │ │ │ ├── App.kt │ │ │ │ ├── adapter │ │ │ │ ├── SubAdapter.kt │ │ │ │ ├── TabAdapter.kt │ │ │ │ ├── VViewPagerAdapter.kt │ │ │ │ └── MainAdapter.kt │ │ │ │ ├── ry │ │ │ │ ├── BaseRecyclerView.kt │ │ │ │ ├── NestChildRecyclerView.kt │ │ │ │ └── NestRecyclerView.kt │ │ │ │ └── MainActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── nestrecyclerview │ │ │ └── demo2 │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── example │ │ └── nestrecyclerview │ │ └── demo2 │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── screenshot ├── 示例.gif └── 下载二维码.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml ├── vcs.xml ├── misc.xml ├── runConfigurations.xml ├── gradle.xml └── jarRepositories.xml ├── .gitignore ├── README.md ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | rootProject.name = "NestRecyclerView" -------------------------------------------------------------------------------- /screenshot/示例.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/screenshot/示例.gif -------------------------------------------------------------------------------- /screenshot/下载二维码.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/screenshot/下载二维码.png -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | NestRecyclerView 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ubitar/NestRecyclerView_V2/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/Ubitar/NestRecyclerView_V2/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/Ubitar/NestRecyclerView_V2/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/Ubitar/NestRecyclerView_V2/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/Ubitar/NestRecyclerView_V2/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/bean/MainTabBean.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.bean 2 | 3 | data class MainTabBean( 4 | val text: String, 5 | var isSelected: Boolean 6 | ) -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #6200EE 4 | #3700B3 5 | #03DAC5 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jul 15 17:24:15 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.1.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/App.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2 2 | 3 | import android.app.Application 4 | import com.blankj.utilcode.util.Utils 5 | 6 | class App: Application() { 7 | override fun onCreate() { 8 | super.onCreate() 9 | Utils.init(this) 10 | } 11 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### NestRecyclerView 2 | ------------ 3 | #### 效果 4 | 5 | ![示例.gif](https://github.com/Ubitar/NestRecyclerView_V2/blob/master/screenshot/%E7%A4%BA%E4%BE%8B.gif) 6 | 7 | #### 下载二维码 8 | ![下载二维码.png](https://github.com/Ubitar/NestRecyclerView_V2/blob/master/screenshot/%E4%B8%8B%E8%BD%BD%E4%BA%8C%E7%BB%B4%E7%A0%81.png) 9 | 10 | #### 详细文章 11 | https://www.jianshu.com/p/0e91648173dc -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/nestrecyclerview/demo2/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/adapter/SubAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.adapter 2 | 3 | import com.chad.library.adapter.base.BaseQuickAdapter 4 | import com.chad.library.adapter.base.viewholder.BaseViewHolder 5 | import com.example.nestrecyclerview.demo2.R 6 | 7 | class SubAdapter : BaseQuickAdapter(R.layout.holder_item3) { 8 | override fun convert(holder: BaseViewHolder, item: String) { 9 | holder.setText(R.id.txt, item) 10 | } 11 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/holder_tab.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/adapter/TabAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.adapter 2 | 3 | import com.chad.library.adapter.base.BaseQuickAdapter 4 | import com.chad.library.adapter.base.viewholder.BaseViewHolder 5 | import com.example.nestrecyclerview.demo2.R 6 | import com.example.nestrecyclerview.demo2.bean.MainTabBean 7 | 8 | class TabAdapter : BaseQuickAdapter(R.layout.holder_tab) { 9 | override fun convert(holder: BaseViewHolder, item: MainTabBean) { 10 | val text=if(item.isSelected) "${item.text}\n选中" else item.text 11 | holder.setText(R.id.txt, text) 12 | } 13 | 14 | fun setSelectAt(index:Int){ 15 | data.forEach { it.isSelected=false } 16 | data[index].isSelected=true 17 | setList(data) 18 | } 19 | } -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /app/src/main/res/layout/holder_item3.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/nestrecyclerview/demo2/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.example.nestrecyclerview.demo", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/holder_item2.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/holder_item1.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/bean/MainListBean.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.bean 2 | 3 | import com.chad.library.adapter.base.entity.MultiItemEntity 4 | import com.example.nestrecyclerview.demo2.adapter.SubAdapter 5 | import com.example.nestrecyclerview.demo2.adapter.TabAdapter 6 | 7 | class MainListBean private constructor(override val itemType: Int) : MultiItemEntity { 8 | 9 | lateinit var text: String 10 | lateinit var tabAdapter: TabAdapter 11 | lateinit var subAdapters: MutableList 12 | 13 | constructor(itemType: Int, text: String) : this(itemType) { 14 | this.text = text 15 | } 16 | 17 | constructor(itemType: Int, tabAdapter: TabAdapter,subAdapters: MutableList) : this(itemType) { 18 | this.tabAdapter = tabAdapter 19 | this.subAdapters=subAdapters 20 | } 21 | 22 | companion object { 23 | const val TYPE_MAIN = 1 24 | const val TYPE_SUB = 2 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/adapter/VViewPagerAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.adapter 2 | 3 | import android.view.View 4 | import android.view.ViewGroup 5 | import androidx.viewpager.widget.PagerAdapter 6 | import java.util.* 7 | 8 | class VViewPagerAdapter( 9 | var mTitles: ArrayList? = null, 10 | var viewList: ArrayList 11 | ) : PagerAdapter() { 12 | 13 | override fun getCount(): Int { 14 | return viewList.size 15 | } 16 | 17 | override fun isViewFromObject(view: View, obj: Any): Boolean { 18 | return view === obj 19 | } 20 | 21 | override fun instantiateItem(container: ViewGroup, position: Int): Any { 22 | val view = viewList[position] 23 | if (container == view.parent) { 24 | container.removeView(view) 25 | } 26 | container.addView(view) 27 | return view 28 | } 29 | 30 | override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) { 31 | //container.removeView((View) object); 32 | } 33 | 34 | override fun getPageTitle(position: Int): CharSequence? { 35 | if (mTitles == null) return "" 36 | return mTitles!![position] 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /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 | buildToolsVersion "29.0.3" 8 | 9 | defaultConfig { 10 | applicationId "com.example.nestrecyclerview.demo2" 11 | minSdkVersion 21 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: "libs", include: ["*.jar"]) 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.71" 30 | implementation 'androidx.core:core-ktx:1.3.0' 31 | implementation 'androidx.appcompat:appcompat:1.1.0' 32 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 33 | implementation 'androidx.recyclerview:recyclerview:1.1.0' 34 | implementation "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4" 35 | implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2' 36 | implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.2' 37 | implementation "com.blankj:utilcode:1.28.0" 38 | testImplementation 'junit:junit:4.12' 39 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 40 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 41 | 42 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 17 | 18 | 22 | 23 | 28 | 29 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/ry/BaseRecyclerView.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.ry 2 | 3 | import android.content.Context 4 | import android.util.AttributeSet 5 | import android.widget.OverScroller 6 | import androidx.recyclerview.widget.RecyclerView 7 | import java.lang.reflect.Field 8 | 9 | /** 10 | * 一个可以方便获取Y轴速率的RecyclerView基类 11 | */ 12 | open class BaseRecyclerView @JvmOverloads constructor( 13 | context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 14 | ) : RecyclerView(context, attrs, defStyleAttr) { 15 | 16 | private val overScroller: OverScroller; 17 | 18 | private val scrollerYObj: Any; 19 | 20 | private val velocityYField: Field; 21 | 22 | init { 23 | // 1. mViewFlinger对象获取 24 | val viewFlingField = RecyclerView::class.java.getDeclaredField("mViewFlinger") 25 | viewFlingField.isAccessible = true 26 | var viewFlingObj = viewFlingField.get(this) 27 | 28 | // 2. overScroller对象获取 29 | val overScrollerFiled = viewFlingObj.javaClass.getDeclaredField("mOverScroller") 30 | overScrollerFiled.isAccessible = true 31 | overScroller = overScrollerFiled.get(viewFlingObj) as OverScroller 32 | 33 | // 3. scrollerY对象获取 34 | val scrollerYFiled = OverScroller::class.java.getDeclaredField("mScrollerY") 35 | scrollerYFiled.isAccessible = true 36 | scrollerYObj = scrollerYFiled.get(overScroller) 37 | 38 | // 4. Y轴速率filed获取 39 | velocityYField = scrollerYObj.javaClass.getDeclaredField("mCurrVelocity") 40 | velocityYField.isAccessible = true 41 | } 42 | 43 | /** 44 | * 获取垂直方向的速率 45 | */ 46 | fun getVelocityY(): Int = (velocityYField.get(scrollerYObj) as Float).toInt() 47 | 48 | /** 49 | * 停止滑动fling 50 | */ 51 | fun stopFling() { 52 | this.overScroller.forceFinished(true) 53 | velocityYField.set(scrollerYObj, 0) 54 | } 55 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.recyclerview.widget.LinearLayoutManager 6 | import com.blankj.utilcode.util.ToastUtils 7 | import com.example.nestrecyclerview.demo2.adapter.MainAdapter 8 | import com.example.nestrecyclerview.demo2.adapter.SubAdapter 9 | import com.example.nestrecyclerview.demo2.adapter.TabAdapter 10 | import com.example.nestrecyclerview.demo2.bean.MainListBean 11 | import com.example.nestrecyclerview.demo2.bean.MainTabBean 12 | import kotlinx.android.synthetic.main.activity_main.* 13 | 14 | class MainActivity : AppCompatActivity() { 15 | 16 | private lateinit var mainAdapter: MainAdapter 17 | private lateinit var tabAdapter: TabAdapter 18 | private lateinit var subAdapters: MutableList 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_main) 23 | initRecyclerView() 24 | loadData() 25 | initEvent() 26 | } 27 | 28 | /** 初始化列表 */ 29 | private fun initRecyclerView() { 30 | mainAdapter = MainAdapter() 31 | recyclerView.itemAnimator = null 32 | recyclerView.layoutManager = LinearLayoutManager(this) 33 | // recyclerView.setPreLoadHeight(200) //px单位,设置RY预加载不可见(屏幕外)距离的视图,默认是一个RY的高度 34 | recyclerView.adapter = mainAdapter 35 | } 36 | 37 | /** 加载数据 */ 38 | private fun loadData() { 39 | tabAdapter = TabAdapter() 40 | subAdapters = arrayListOf( 41 | SubAdapter(), 42 | SubAdapter(), 43 | SubAdapter(), 44 | SubAdapter() 45 | ) 46 | 47 | val list = ArrayList() 48 | for (i in 1..10) 49 | list.add(MainListBean(MainListBean.TYPE_MAIN, "${i}")) 50 | 51 | val tabs = ArrayList(4) 52 | for (i in 1..4) 53 | tabs.add(MainTabBean("Tab${i}", i == 1)) 54 | tabAdapter.setList(tabs) 55 | 56 | subAdapters.forEach { 57 | val list = ArrayList() 58 | for (i in 1..12) 59 | list.add("${i}") 60 | it.setList(list) 61 | } 62 | 63 | list.add(MainListBean(MainListBean.TYPE_SUB, tabAdapter, subAdapters)) 64 | 65 | mainAdapter.setList(list) 66 | } 67 | 68 | /** 初始化触发事件 */ 69 | private fun initEvent() { 70 | mainAdapter.setOnChildScrollEndListener { recyclerView, newState -> 71 | ToastUtils.showShort("滚动子RY到底部了") 72 | //这里可以加载更多数据 73 | } 74 | mainAdapter.setTabSelectListener { viewPager, position -> 75 | tabAdapter.setSelectAt(position) 76 | viewPager.setCurrentItem(position, false) 77 | } 78 | mainAdapter.setOnTurnPageListener { tabRecyclerView, position -> 79 | tabAdapter.setSelectAt(position) 80 | } 81 | } 82 | 83 | 84 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/ry/NestChildRecyclerView.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.ry 2 | 3 | import android.content.Context 4 | import android.util.AttributeSet 5 | import android.view.MotionEvent 6 | import android.view.View 7 | import android.view.ViewConfiguration 8 | 9 | /** 10 | * 内层的RecyclerView 11 | */ 12 | class NestChildRecyclerView @JvmOverloads constructor( 13 | context: Context, 14 | attrs: AttributeSet? = null, 15 | defStyleAttr: Int = 0 16 | ) : BaseRecyclerView(context, attrs, defStyleAttr) { 17 | 18 | private var parentRecyclerView: NestRecyclerView? = null 19 | 20 | /** 21 | * 触发移动事件的最小距离 22 | */ 23 | private val mTouchSlop = ViewConfiguration.get(context).scaledTouchSlop 24 | 25 | /** 26 | * 触摸按下时的X、Y值 27 | */ 28 | private var touchDownX: Float = 0f 29 | private var touchDownY: Float = 0f 30 | 31 | /** 32 | * 当前状态 33 | */ 34 | private var state: Int = DRAG_IDLE 35 | 36 | override fun onScrollStateChanged(state: Int) { 37 | super.onScrollStateChanged(state) 38 | // 是否已经停止scrolling 39 | if (state == SCROLL_STATE_IDLE) { 40 | // 这里是考虑到当整个childRecyclerView被detach之后,及时上报parentRecyclerView 41 | val velocityY = getVelocityY() 42 | if (velocityY < 0 && computeVerticalScrollOffset() == 0) { 43 | parentRecyclerView?.fling(0, velocityY) 44 | } 45 | } 46 | } 47 | 48 | override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { 49 | if (ev.action == MotionEvent.ACTION_DOWN) { 50 | // ACTION_DOWN 触摸按下,保存临时变量 51 | state = DRAG_IDLE 52 | touchDownX = ev.rawX 53 | touchDownY = ev.rawY 54 | this.stopFling() 55 | } 56 | return super.onInterceptTouchEvent(ev) 57 | } 58 | 59 | /** 60 | * 这段逻辑主要是RecyclerView最底部,垂直上拉后居然还能左右滑动,不能忍 61 | */ 62 | override fun onTouchEvent(ev: MotionEvent): Boolean { 63 | if (ev.action == MotionEvent.ACTION_MOVE) { 64 | // ACTION_MOVE 判定垂直还是水平滑动 65 | if (state == DRAG_IDLE) { 66 | val xDistance = Math.abs(ev.rawX - touchDownX) 67 | val yDistance = Math.abs(ev.rawY - touchDownY) 68 | 69 | if (xDistance > yDistance && xDistance > mTouchSlop) { 70 | // 水平滑动 71 | state = DRAG_HORIZONTAL 72 | } else if (yDistance > xDistance && yDistance > mTouchSlop) { 73 | // 垂直滑动 74 | state = DRAG_VERTICAL 75 | parent.requestDisallowInterceptTouchEvent(true) 76 | } 77 | } 78 | } 79 | return super.onTouchEvent(ev) 80 | } 81 | 82 | override fun onAttachedToWindow() { 83 | super.onAttachedToWindow() 84 | searchAndBindNestRecyclerView() 85 | } 86 | 87 | /** 88 | * 与NestRecyclerView父组件绑定 89 | */ 90 | private fun searchAndBindNestRecyclerView() { 91 | var lastTraverseView: View = this 92 | var parentView = this.parent as View? 93 | while (parentView != null) { 94 | if (parentView is NestRecyclerView) { 95 | // 碰到ParentRecyclerView,设置结束 96 | parentView.setChildPagerContainer(lastTraverseView) 97 | this.parentRecyclerView = parentView 98 | return 99 | } 100 | 101 | lastTraverseView = parentView 102 | parentView = parentView.parent as View 103 | } 104 | } 105 | 106 | companion object { 107 | private const val DRAG_IDLE = 0 108 | private const val DRAG_VERTICAL = 1 109 | private const val DRAG_HORIZONTAL = 2 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | xmlns:android 17 | 18 | ^$ 19 | 20 | 21 | 22 |
23 |
24 | 25 | 26 | 27 | xmlns:.* 28 | 29 | ^$ 30 | 31 | 32 | BY_NAME 33 | 34 |
35 |
36 | 37 | 38 | 39 | .*:id 40 | 41 | http://schemas.android.com/apk/res/android 42 | 43 | 44 | 45 |
46 |
47 | 48 | 49 | 50 | .*:name 51 | 52 | http://schemas.android.com/apk/res/android 53 | 54 | 55 | 56 |
57 |
58 | 59 | 60 | 61 | name 62 | 63 | ^$ 64 | 65 | 66 | 67 |
68 |
69 | 70 | 71 | 72 | style 73 | 74 | ^$ 75 | 76 | 77 | 78 |
79 |
80 | 81 | 82 | 83 | .* 84 | 85 | ^$ 86 | 87 | 88 | BY_NAME 89 | 90 |
91 |
92 | 93 | 94 | 95 | .* 96 | 97 | http://schemas.android.com/apk/res/android 98 | 99 | 100 | ANDROID_ATTRIBUTE_ORDER 101 | 102 |
103 |
104 | 105 | 106 | 107 | .* 108 | 109 | .* 110 | 111 | 112 | BY_NAME 113 | 114 |
115 |
116 |
117 |
118 | 119 | 121 |
122 |
-------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/adapter/MainAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.adapter 2 | 3 | import android.view.View 4 | import androidx.recyclerview.widget.GridLayoutManager 5 | import androidx.recyclerview.widget.LinearLayoutManager 6 | import androidx.recyclerview.widget.RecyclerView 7 | import androidx.recyclerview.widget.StaggeredGridLayoutManager 8 | import androidx.viewpager.widget.ViewPager 9 | import com.chad.library.adapter.base.BaseMultiItemQuickAdapter 10 | import com.chad.library.adapter.base.viewholder.BaseViewHolder 11 | import com.example.nestrecyclerview.demo2.R 12 | import com.example.nestrecyclerview.demo2.bean.MainListBean 13 | import com.example.nestrecyclerview.demo2.ry.NestChildRecyclerView 14 | import com.example.nestrecyclerview.demo2.ry.NestRecyclerView 15 | 16 | class MainAdapter : BaseMultiItemQuickAdapter(null), 17 | NestRecyclerView.INestAdapter { 18 | 19 | private var viewPagerAdapter: VViewPagerAdapter? = null 20 | 21 | private var mCurPageIndex = 0 22 | 23 | private var onTabSelectListener: ((viewPager: ViewPager, position: Int) -> Unit)? = null 24 | private var onTurnPageListener: ((tabRecyclerView: RecyclerView, position: Int) -> Unit)? = null 25 | private var onChildScrollEndListener: ((recyclerView: NestChildRecyclerView, newState: Int) -> Unit)? = 26 | null 27 | 28 | init { 29 | addItemType(MainListBean.TYPE_MAIN, R.layout.holder_item1) 30 | addItemType(MainListBean.TYPE_SUB, R.layout.holder_item2) 31 | } 32 | 33 | override fun convert(holder: BaseViewHolder, item: MainListBean) { 34 | when (item.itemType) { 35 | MainListBean.TYPE_MAIN -> convertMainItem(holder, item) 36 | MainListBean.TYPE_SUB -> convertSub(holder, item) 37 | } 38 | } 39 | 40 | private fun convertMainItem(holder: BaseViewHolder, item: MainListBean) { 41 | holder.setText(R.id.txt, item.text) 42 | } 43 | 44 | private fun convertSub(holder: BaseViewHolder, item: MainListBean) { 45 | val tabLayout = holder.getView(R.id.tabLayout) 46 | val viewPager = holder.getView(R.id.viewPager) 47 | 48 | item.tabAdapter.setOnItemClickListener { adapter2, view, position -> 49 | onTabSelectListener?.invoke(viewPager, position) 50 | } 51 | tabLayout.layoutManager = 52 | LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) 53 | tabLayout.itemAnimator = null 54 | tabLayout.adapter = item.tabAdapter 55 | 56 | 57 | val viewList = ArrayList(item.subAdapters.size) 58 | item.subAdapters.forEachIndexed { index, mainSecondAdapter -> 59 | val recyclerView = createChildRecyclerView(item, index) 60 | recyclerView.adapter = mainSecondAdapter 61 | viewList.add(recyclerView) 62 | } 63 | 64 | val pageChangeListener = object : ViewPager.OnPageChangeListener { 65 | override fun onPageScrollStateChanged(state: Int) { 66 | } 67 | 68 | override fun onPageScrolled( 69 | position: Int, 70 | positionOffset: Float, 71 | positionOffsetPixels: Int 72 | ) { 73 | } 74 | 75 | override fun onPageSelected(i: Int) { 76 | item.tabAdapter.setSelectAt(i) 77 | mCurPageIndex = i 78 | onTurnPageListener?.invoke(tabLayout, mCurPageIndex) 79 | } 80 | } 81 | if (viewPagerAdapter == null) { 82 | viewPagerAdapter = VViewPagerAdapter(null, viewList) 83 | viewPager.addOnPageChangeListener(pageChangeListener) 84 | } else viewPagerAdapter?.viewList = viewList 85 | viewPager.pageMargin = 10 86 | viewPager.adapter = viewPagerAdapter 87 | 88 | /** 触发选择了当前片段的事件无论该片段是否被初始化过 ,要自己在adapter外自行判断是否已初始化 */ 89 | pageChangeListener.onPageSelected(mCurPageIndex) 90 | 91 | } 92 | 93 | /** 获得当前子RecyclerView组件 */ 94 | override fun getCurrentChildRecyclerView(): NestChildRecyclerView? { 95 | if (viewPagerAdapter == null) return null 96 | return viewPagerAdapter?.viewList?.get(mCurPageIndex) as NestChildRecyclerView 97 | } 98 | 99 | /** 创建子NestRecyclerView组件 */ 100 | override fun createChildRecyclerView(item: MainListBean, index: Int): NestChildRecyclerView { 101 | val recyclerView = NestChildRecyclerView(context, null, 0) 102 | val columnCount = 1 103 | recyclerView.layoutManager = 104 | StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL) 105 | recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { 106 | override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { 107 | if (newState == RecyclerView.SCROLL_STATE_IDLE) { 108 | if (getLastVisibleItem(recyclerView) >= getTotalItemCount(recyclerView) - 1) 109 | onChildScrollEndListener?.invoke( 110 | recyclerView as NestChildRecyclerView, 111 | newState 112 | ) 113 | } 114 | } 115 | 116 | private fun getLastVisibleItem(childRecyclerView: RecyclerView): Int { 117 | val layoutManager = childRecyclerView.layoutManager 118 | 119 | var lastVisibleIndex = -1 120 | 121 | layoutManager.let { 122 | if (it is StaggeredGridLayoutManager) { 123 | if (columnCount > 1) { 124 | val iArr = IntArray(2) 125 | it.findLastVisibleItemPositions(iArr) 126 | lastVisibleIndex = if (iArr[0] > iArr[1]) iArr[0] else iArr[1] 127 | } else { 128 | val iArr = IntArray(1) 129 | it.findLastVisibleItemPositions(iArr) 130 | lastVisibleIndex = iArr[0] 131 | } 132 | } else if (it is LinearLayoutManager) { 133 | lastVisibleIndex = it.findLastVisibleItemPosition() 134 | } else if (it is GridLayoutManager) { 135 | lastVisibleIndex = it.findLastVisibleItemPosition() 136 | } 137 | } 138 | 139 | return lastVisibleIndex 140 | } 141 | 142 | fun getTotalItemCount(childRecyclerView: RecyclerView): Int { 143 | return childRecyclerView.adapter?.itemCount ?: -1 144 | } 145 | 146 | }) 147 | return recyclerView 148 | } 149 | 150 | 151 | override fun setList(list: Collection?) { 152 | //在刷新列表数据时将索引重新设置为0 153 | mCurPageIndex = 0 154 | super.setList(list) 155 | } 156 | 157 | fun setTabSelectListener(listener: ((viewPager: ViewPager, position: Int) -> Unit)) { 158 | this.onTabSelectListener = listener 159 | } 160 | 161 | fun setOnTurnPageListener(listener: (tabRecyclerView: RecyclerView, position: Int) -> Unit) { 162 | this.onTurnPageListener = listener 163 | } 164 | 165 | fun setOnChildScrollEndListener(listener: ((recyclerView: NestChildRecyclerView, newState: Int) -> Unit)? = null) { 166 | this.onChildScrollEndListener = listener 167 | } 168 | 169 | 170 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/nestrecyclerview/demo2/ry/NestRecyclerView.kt: -------------------------------------------------------------------------------- 1 | package com.example.nestrecyclerview.demo2.ry 2 | 3 | import android.content.Context 4 | import android.util.AttributeSet 5 | import android.view.MotionEvent 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import androidx.core.view.NestedScrollingParent3 9 | import androidx.core.view.ViewCompat 10 | 11 | /** 12 | * 外层的RecylerView 13 | */ 14 | class NestRecyclerView @JvmOverloads constructor( 15 | context: Context, 16 | attrs: AttributeSet? = null, 17 | defStyleAttr: Int = 0 18 | ) : BaseRecyclerView(context, attrs, defStyleAttr), NestedScrollingParent3 { 19 | 20 | private var childPagerContainer: View? = null 21 | 22 | private var doNotInterceptTouchEvent: Boolean = false 23 | 24 | private var stickyListener: ((isAtTop: Boolean) -> Unit)? = null 25 | 26 | private var innerIsStickyTop = false 27 | 28 | /** 29 | * 顶部悬停的高度 30 | */ 31 | private var stickyHeight = 0 32 | 33 | init { 34 | this.overScrollMode = View.OVER_SCROLL_NEVER 35 | this.descendantFocusability = ViewGroup.FOCUS_BLOCK_DESCENDANTS 36 | } 37 | 38 | override fun onInterceptTouchEvent(e: MotionEvent?): Boolean { 39 | if (e!!.action == MotionEvent.ACTION_DOWN) { 40 | val childRecyclerView = findCurrentChildRecyclerView() 41 | 42 | // 1. 是否禁止拦截 43 | doNotInterceptTouchEvent = doNotInterceptTouch(e.rawY, childRecyclerView) 44 | 45 | // 2. 停止Fling 46 | this.stopFling() 47 | childRecyclerView?.stopFling() 48 | } 49 | 50 | return if (doNotInterceptTouchEvent) { 51 | false 52 | } else { 53 | super.onInterceptTouchEvent(e) 54 | } 55 | } 56 | 57 | /** 58 | * 是否禁止拦截TouchEvent事件 59 | */ 60 | private fun doNotInterceptTouch(rawY: Float, childRecyclerView: NestChildRecyclerView?): Boolean { 61 | if (null != childRecyclerView && 62 | null != childPagerContainer && 63 | ViewCompat.isAttachedToWindow(childPagerContainer!!) 64 | ) { 65 | val coorValue = IntArray(2) 66 | childRecyclerView.getLocationOnScreen(coorValue) 67 | 68 | val childRecyclerViewY = coorValue[1] 69 | if (rawY > childRecyclerViewY) { 70 | return true 71 | } 72 | 73 | if (childPagerContainer!!.top == stickyHeight) { 74 | return true 75 | } 76 | } 77 | 78 | // 默认不禁止 79 | return false 80 | } 81 | 82 | override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) { 83 | if (target is NestChildRecyclerView) { 84 | // 下面这一坨代码的主要目的是计算consumeY 85 | var consumeY = dy 86 | val childScrollY = target.computeVerticalScrollOffset() 87 | if (childPagerContainer!!.top > stickyHeight) { 88 | if (childScrollY > 0 && dy < 0) { 89 | consumeY = 0 90 | } else if (childPagerContainer!!.top - dy < stickyHeight) { 91 | consumeY = childPagerContainer!!.top - stickyHeight 92 | } 93 | } else if (childPagerContainer!!.top == stickyHeight) { 94 | consumeY = if (-dy < childScrollY) { 95 | 0 96 | } else { 97 | dy + childScrollY 98 | } 99 | } 100 | 101 | if (consumeY != 0) { 102 | consumed[1] = consumeY 103 | this.scrollBy(0, consumeY) 104 | } 105 | } 106 | } 107 | 108 | override fun onScrollStateChanged(state: Int) { 109 | if (state == SCROLL_STATE_IDLE) { 110 | val velocityY = getVelocityY() 111 | if (velocityY > 0) { 112 | // 滑动到最底部时,骤然停止,这时需要把速率传递给ChildRecyclerView 113 | val childRecyclerView = findCurrentChildRecyclerView() 114 | childRecyclerView?.fling(0, velocityY) 115 | } 116 | } 117 | } 118 | 119 | override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean { 120 | return target is NestChildRecyclerView 121 | } 122 | 123 | override fun onNestedScroll( 124 | target: View, 125 | dxConsumed: Int, 126 | dyConsumed: Int, 127 | dxUnconsumed: Int, 128 | dyUnconsumed: Int, 129 | type: Int 130 | ) { 131 | // do nothing 132 | } 133 | 134 | override fun onNestedScroll( 135 | target: View, 136 | dxConsumed: Int, 137 | dyConsumed: Int, 138 | dxUnconsumed: Int, 139 | dyUnconsumed: Int, 140 | type: Int, 141 | consumed: IntArray 142 | ) { 143 | // do nothing 144 | } 145 | 146 | override fun onNestedScrollAccepted(child: View, target: View, axes: Int, type: Int) { 147 | // do nothing 148 | } 149 | 150 | override fun onStopNestedScroll(target: View, type: Int) { 151 | // do nothing 152 | } 153 | 154 | /** 155 | * 获取当前的ChildRecyclerView 156 | */ 157 | private fun findCurrentChildRecyclerView(): NestChildRecyclerView? { 158 | (adapter as? INestAdapter<*>)?.apply { 159 | return this.getCurrentChildRecyclerView() 160 | } 161 | return null 162 | } 163 | 164 | /** 165 | * 由ChildRecyclerView上报ViewPager(ViewPager2)的父容器,用做内联滑动逻辑判断,及Touch拦截等 166 | */ 167 | fun setChildPagerContainer(childPagerContainer: View) { 168 | if (this.childPagerContainer != childPagerContainer) { 169 | this.childPagerContainer = childPagerContainer 170 | this.post { 171 | adjustChildPagerContainerHeight() 172 | } 173 | } 174 | } 175 | 176 | /** 177 | * Activity调用方法 178 | */ 179 | fun setStickyHeight(stickyHeight: Int) { 180 | val scrollOffset = this.stickyHeight - stickyHeight 181 | this.stickyHeight = stickyHeight 182 | this.adjustChildPagerContainerHeight() 183 | 184 | this.post { 185 | this.scrollBy(0, scrollOffset) 186 | } 187 | } 188 | 189 | /** 190 | * 调整Child容器的高度 191 | */ 192 | private fun adjustChildPagerContainerHeight() { 193 | if (null != childPagerContainer) { 194 | val layoutParams = childPagerContainer!!.layoutParams 195 | val newHeight = this.height - stickyHeight 196 | if (newHeight != layoutParams.height) { 197 | layoutParams.height = newHeight 198 | childPagerContainer!!.layoutParams = layoutParams 199 | } 200 | } 201 | } 202 | 203 | /** 204 | * 吸顶回调 205 | */ 206 | fun setStickyListener(stickyListener: (isAtTop: Boolean) -> Unit) { 207 | this.stickyListener = stickyListener 208 | } 209 | 210 | override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) { 211 | super.onScrollChanged(l, t, oldl, oldt) 212 | 213 | var currentStickyTop = false 214 | if (childPagerContainer != null && childPagerContainer?.top == stickyHeight) { 215 | currentStickyTop = true 216 | } 217 | 218 | if (currentStickyTop != innerIsStickyTop) { 219 | innerIsStickyTop = currentStickyTop 220 | stickyListener?.invoke(innerIsStickyTop) 221 | } 222 | } 223 | 224 | interface INestAdapter { 225 | fun getCurrentChildRecyclerView(): NestChildRecyclerView? 226 | fun createChildRecyclerView(item: T, index: Int): NestChildRecyclerView 227 | } 228 | } --------------------------------------------------------------------------------