这似乎是ViewPager2
实现错误。pager 第一次获取我们创建的视图时,pager 会调用requestApplyInsets
它。但不幸的是,视图没有附加父视图,因此调用requestApplyInsets
无效。
可以通过添加View.OnAttachStateChangeListener
which call requestApplyInsets
on来解决onViewAttachedToWindow
。
您的ParallelNestedScrollingActivity
示例似乎通过以下方式运行良好:
diff --git a/ViewPager2/app/src/main/java/androidx/viewpager2/integration/testapp/ParallelNestedScrollingActivity.kt b/ViewPager2/app/src/main/java/androidx/viewpager2/integration/testapp/ParallelNestedScrollingActivity.kt
index 4e3753a..d2683df 100644
--- a/ViewPager2/app/src/main/java/androidx/viewpager2/integration/testapp/ParallelNestedScrollingActivity.kt
+++ b/ViewPager2/app/src/main/java/androidx/viewpager2/integration/testapp/ParallelNestedScrollingActivity.kt
@@ -29,0 +30 @@ import android.widget.TextView
+import androidx.core.view.ViewCompat
@@ -57 +58,3 @@ class ParallelNestedScrollingActivity : Activity() {
- val root = inflater.inflate(R.layout.item_nested_recyclerviews, parent, false)
+ val root = inflater.inflate(R.layout.item_nested_recyclerviews, parent, false).apply {
+ addOnAttachStateChangeListener(RequestApplyInsetsOnAttached)
+ }
@@ -132,0 +136,5 @@ internal val CELL_COLORS = listOf(
+
+private object RequestApplyInsetsOnAttached : View.OnAttachStateChangeListener {
+ override fun onViewAttachedToWindow(view: View) = ViewCompat.requestApplyInsets(view)
+ override fun onViewDetachedFromWindow(view: View) = Unit
+}