【问题标题】:NestedScrollView and CoordinatorLayout. Issue on ScrollingNestedScrollView 和 CoordinatorLayout。滚动问题
【发布时间】:2015-08-28 15:26:55
【问题描述】:

CoordinatorLayoutNestedScrollView 有一个奇怪的问题(使用设计支持库 22.2.0)

使用小于NestedScrollView 的内容我应该有一个固定的内容。 但是,尝试上下滚动内容时,我可以得到内容已被替换,并且再也不会出现在自己的位置。

这里有一个小例子:

代码如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:paddingTop="24dp"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/padding">

        </FrameLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:visibility="gone"
        android:src="@drawable/ic_done" />

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

标签: android material-design android-support-library android-design-library android-coordinatorlayout


【解决方案1】:

我认为这不是支持库中的错误,请使用它

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_gravity="fill_vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

【讨论】:

  • 我已经尝试了上面的工作并且工作起来就像一个魅力。试试看!
  • 实际上,这确实有效!至少在 22.2.1 上。不过对我来说,没有必要拥有fillViewPort
  • 我刚刚添加了 'android:layout_gravity="fill_vertical"' 并且它起作用了。谢谢@LiFei。
  • 这是问题的正确答案。这不是NestedScrollView 中的错误,上面使用的类是不必要的。
  • 这在我的情况下绝对行不通,在嵌套滚动视图中我有一个带有内部回收器视图的片段。如果我使用这种方法,recyclerview 会滚动,但 appbar 不再折叠。
【解决方案2】:

这也可以在cheesesquare 演示中观察到,当删除详细信息片段中除一张卡之外的所有卡时。​​p>

我能够(目前)使用这个类来解决这个问题:https://gist.github.com/EmmanuelVinas/c598292f43713c75d18e

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.evs.demo.layout.FixedScrollingViewBehavior">
    .....   
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 目前解决方法是一个很好的解决方案。我认为这绝对是支持库中的一个错误。
  • 很好的修复。但是,当滚动内容小于滚动本身时,滚动仍然响应并折叠工具栏。知道如何在根本不需要滚动时禁用滚动和折叠(对于小内容场景)
  • @GuillermoMP 你有没有解决这个问题?我也面临同样的问题。
  • 此修复在 onMeasure 后工作正常。但是在片段 ViewCompat.isLaidOut(appBar) 开始之后总是假的。此方法在例如屏幕旋转之后返回 true。我哪里错了?
  • 这是一个在父 ScrollView 中演示 NestedScrollView 的示例应用:github.com/AdamSHurwitz/NestedScrolling
【解决方案3】:

android:layout_gravity="fill_vertical" 也为我工作。

【讨论】:

    【解决方案4】:

    我的回答可能会迟到,但这里是。 我遇到了类似的问题,但上述解决方案都不适合我。 最后我使用支持库的版本 23 修复了它。

    ...
    compileSdkVersion 23
    ...
    targetSdkVersion 23
    ...
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:design:23.1.0'
    

    【讨论】:

    • 出于兴趣,切换到 v23 的支持库破坏了我的构建,sdk 声称我没有使用 Theme.AppCompat,即使我是。
    • 嗯,我在 v23 上也遇到了 Apache 已弃用的库的问题,但这是另一个问题的问题。一旦你解决了这个问题,请尝试查看是否已解决。
    【解决方案5】:

    onMeasureChild() 方法在布局过程中被多次调用。显然,关键是在过程的早期为孩子的身高获得一个非零值。 ScrollingViewBehavior 在以下情况下无法这样做:

    int scrollRange = appBar.getTotalScrollRange();
    int height = parent.getHeight() 
                 - appBar.getMeasuredHeight()
                 + scrollRange;
    

    FixedScrollingviewBehavior 解决了这个问题:

    int height = parent.getHeight() 
                 - appBar.getMeasuredHeight() 
                 + Math.min(scrollRange, parent.getHeight() - heightUsed);
    

    很早就给 height 的值为 -128,即应用栏的高度。

    接近原版的另一种选择是:

    int height = parent.getMeasuredHeight()
                 - appBar.getMeasuredHeight()
                 + scrollRange;
    

    【讨论】:

    • 您能解释一下吗?作为初学者,我发现很难遵循。我尝试阅读ScrollingViewBehavior 类,它是基类HeaderScrollingViewBehavior。但我无法理解它
    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2017-01-16
    • 2016-11-02
    • 2019-07-08
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多