35

CoordinatorLayout我对and有一个奇怪的问题NestedScrollView(使用设计支持库 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>
4

5 回答 5

38

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

<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">
于 2015-07-21T05:58:23.847 回答
37

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

我能够使用这个类(现在)解决这个问题: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>
于 2015-06-12T15:45:00.717 回答
5

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

于 2015-11-12T03:56:13.527 回答
3

我的回答可能会迟到,但这里有。我遇到了类似的问题,但上述解决方案都不适合我。最后,我使用支持库的版本 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'
于 2015-10-25T14:07:21.573 回答
2

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;
于 2015-07-01T21:47:46.823 回答