0

我在片段中有一个 FAB,ViewPager2它在边缘到边缘时应该尊重窗口插图。我OnApplyWindowInsetsListener在 FAB 上添加了一个更新它的保证金。这在使用旧的ViewPager.

当更新到ViewPager2它时,似乎OnApplyWindowInsetsListener一开始没有调用。但是,当我启动ActionMode. 然后,调用监听器并使用新的边距,直到我离开父片段。


我已经分叉了演示项目来说明问题。请参阅https://github.com/hardysim/views-widgets-samples/tree/edge-to-edgeParallelNestedScrollingActivity上分支edge-to-edge上的“带有嵌套 RecyclerViews 的 ViewPager2”示例 ( ) 。

在这里,我向RecyclerViewViewPager2 页面上使用的(嵌套)添加了一个 FAB,并将 Activity-UI 设置为边对边(请参阅 参考资料View.goEdgeToEdge())。然后,FAB 位于导航栏后面,我们需要更新它的边距以添加窗口插图。

这就是它不起作用的地方(但它适用于旧的ViewPager)。

4

2 回答 2

3

这已在最初询问的问题跟踪器中得到解答:

这里的问题是当窗口插入被调度时页面还没有附加到视图层次结构中。附加视图时,系统不会使用当前插图调用,因此当视图附加到层次结构时OnApplyWindowInsetsListener,您必须调用。requestApplyInsets()

所以我创建了一个小帮手

/**
 * Call this everytime when using [ViewCompat.setOnApplyWindowInsetsListener]
 * to ensure that insets are always received.
 */
private fun View.requestApplyInsetsWhenAttached() {
    // https://chris.banes.dev/2019/04/12/insets-listeners-to-layouts/

    if (isAttachedToWindow) {
        // We're already attached, just request as normal
        requestApplyInsets()

    } else {
        // We're not attached to the hierarchy, add a listener to request when we are
        addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
            override fun onViewAttachedToWindow(v: View) {
                v.removeOnAttachStateChangeListener(this)
                v.requestApplyInsets()
            }

            override fun onViewDetachedFromWindow(v: View) = Unit
        })
    }
}

在调用ViewCompat.setOnApplyWindowInsetsListener()a之后立即调用View

ViewCompat.setOnApplyWindowInsetsListener(view) { view, insets ->
    // [do stuff]
    insets
}
view.requestApplyInsetsWhenAttached()
于 2020-05-20T09:06:51.263 回答
1

这似乎是ViewPager2实现错误。pager 第一次获取我们创建的视图时,pager 会调用requestApplyInsets它。但不幸的是,视图没有附加父视图,因此调用requestApplyInsets无效。

可以通过添加View.OnAttachStateChangeListenerwhich call requestApplyInsetson来解决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
+}
于 2020-05-19T17:49:31.323 回答