【问题标题】:Android CoordinatorLayout with AppbarLayout Double Fling BugAndroid CoordinatorLayout with AppbarLayout Double Fling Bug
【发布时间】:2021-12-18 08:55:55
【问题描述】:

AppbarLayout 有一个大孩子,所以我们可以拖拽它。

当我先将 appbar 与它的子项一起投掷并且在 之前的投掷动画完成之前投掷滚动视图时出现问题。

当 appbar 和 nestedscrollview 都发生双掷时,滚动变得一团糟

这是layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior"
        >
        <View
            android:layout_width="match_parent"
            android:layout_height="800dp"
            android:background="#8f8"
            app:layout_scrollFlags="scroll"
            />
    </com.google.android.material.appbar.AppBarLayout>
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <View
                android:layout_width="match_parent"
                android:layout_height="800dp"
                android:background="#88f" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    RecyclerView 也有同样的问题

    参见HeaderViewBehavior.setHeaderTopBottomOffset,它以不同的方向调用newOffset 参数。 当onStartNestedScroll 被调用时,你应该取消AppBarLayout.Behavior fling。

    如何解决:

    覆盖AppBarLayout.Behavior

    package com.google.android.material.appbar // original package
    
    import android.content.Context
    import android.util.AttributeSet
    import android.view.View
    import android.widget.OverScroller
    import androidx.coordinatorlayout.widget.CoordinatorLayout
    
    class FixFlingBehavior @JvmOverloads constructor(
        context: Context,
        attributeSet: AttributeSet? = null
    ) : AppBarLayout.Behavior(context, attributeSet) {
    
        private var isAppbarFlinging: Boolean = false
        private var originalScroller: OverScroller? = null
        private val fakeScroller: OverScroller = object : OverScroller(context) {
            override fun computeScrollOffset(): Boolean {
                scroller = originalScroller
                return false // it cancels HeaderBehavior FlingRunnable
            }
        }
    
        override fun setHeaderTopBottomOffset(coordinatorLayout: CoordinatorLayout, appBarLayout: AppBarLayout, newOffset: Int, minOffset: Int, maxOffset: Int): Int {
            isAppbarFlinging = minOffset == Int.MIN_VALUE && maxOffset == Int.MAX_VALUE
            if (scroller === fakeScroller) {
                scroller = originalScroller
            }
            return super.setHeaderTopBottomOffset(coordinatorLayout, appBarLayout, newOffset, minOffset, maxOffset)
        }
    
        override fun onFlingFinished(parent: CoordinatorLayout, layout: AppBarLayout) {
            super.onFlingFinished(parent, layout)
            isAppbarFlinging = false
        }
    
        override fun onStartNestedScroll(parent: CoordinatorLayout, child: AppBarLayout, directTargetChild: View, target: View, nestedScrollAxes: Int, type: Int): Boolean {
            if (isAppbarFlinging && scroller !== fakeScroller) {
                originalScroller = scroller
                scroller = fakeScroller
            }
            return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type)
        }
    }
    

    并在你的布局中使用它

    ...
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="com.google.android.material.appbar.FixFlingBehavior">
    ...
    

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 2015-12-07
      • 2015-12-15
      • 2015-08-23
      • 2016-03-12
      • 2015-09-27
      相关资源
      最近更新 更多