【问题标题】:NestedScrollView fling stopping bug on Nougat (API 25)NestedScrollView 在 Nougat (API 25) 上抛出停止错误
【发布时间】:2017-08-04 14:44:21
【问题描述】:

我对 Nexus 5x (7.1.2) 和 Google Pixel (7.1.1) 上的 NestedScrollView fling 有一个奇怪的问题。在其他操作系统版本上它工作正常。

弹跳动画有时会在抬起手指后立即停止。它卡住了,接下来的几次投掷也可能停止。 为了重现它,你需要上下甩动几次。

在日志中,这些弹跳在速度、方向等方面看起来几乎相同,所以我找不到这个错误的真正原因。

另外,NestedScrollView 不一定要在CoordinatorLayout 之内,也可以根本没有NestedScrollingChild

例如,此错误可通过以下NestedScrollView 子项之一重现:

1) LinearLayout 填充有TextViews

2)WebView

3) LinearLayout 填充有RecyclerViews

我知道RecyclerViewCoordinatorLayout 内部的行为可能存在的问题,但这并不相关。 所以请不要提及任何

recyclerView.getLayoutManager().setAutoMeasureEnabled(true);
recyclerView.setNestedScrollingEnabled(false);

或类似的东西。

代码示例:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="Put a super long text here"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="Put a super long text here"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

【问题讨论】:

标签: android scroll android-support-library android-7.0-nougat nestedscrollview


【解决方案1】:

根据Animating a Scroll Gesture training guide,在覆盖computeScroll()时,在使用mScroller.computeScrollOffset()计算适当的滚动视图偏移量后,我们需要使用:

ViewCompat.postInvalidateOnAnimation(this);

为下一个滚动设置动画。 然而,在 NestedScrollView 中,computeScroll() 看起来像这样:

public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
    ...     
    }
}

它不请求下一个滚动动画!也就是说,使用mScroller.fling(...)后,computeScroll()方法有时只会被调用一次,view不会一直fling。

为了解决这个问题,我尝试过用这种方式替换computeScroll:

public void computeScroll(){
    if(mScroller.computeScrollOffset()){
       ...
       ViewCompat.postInvalidateOnAnimation(this); 
    }
}

这听起来可能不是一个好的解决方案,但它现在可以正常工作。

最新版本的 NestedScrollView 增加了 ViewCompat.postInvalidateOnAnimation(this)。

【讨论】:

    【解决方案2】:

    所以这显然是 NestedScrollView 中的一个错误。 我已经为此制定了解决方法,但仍在等待 Google 方面的适当修复。

    https://github.com/Dimezis/FlingableNestedScrollView/

    编辑

    该问题似乎已在支持库 26.0.0-beta2 中得到解决

    https://chris.banes.me/2017/06/09/carry-on-scrolling/

    编辑 2: 虽然现在滚动工作正常,但在我的应用程序中我可以不断重现这个错误:

    https://issuetracker.google.com/issues/37051723

    如果有人也遇到它,您可以在提到的线程中找到解决方法。

    【讨论】:

    • 这对我帮助很大。谢谢:)
    • Support lib 26仍有问题未解决。足以看到Android Studio“ScrollingActivity”创建的android项目和Google IO App本身在滚动时出现错误。
    猜你喜欢
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2017-12-02
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多