簡體   English   中英

使用Scrollview作為CoordinatorLayout的子項時,工具欄不會崩潰

[英]Toolbar will not collapse with Scrollview as child of CoordinatorLayout

我正在嘗試使用CoordinatorLayout關注Google文檔,但我在CoordinatorLayout中遇到了ScrollView的問題。 基本上,當向下滾動時,工具欄通常會使用RecyclerView或Listview折疊。 現在使用ScrollView它不會崩潰。

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

    </ScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

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

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

ScrollView不與CoordinatorLayout合作。 您必須使用NestedScrollView而不是ScrollView

使用NestedScrollView將您的scrollview折疊為Coordinator Layout的子項。 用以下代碼替換您的代碼:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

        <TextView
            android:id="@+id/tv_View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/filler"
            style="@style/TextAppearance.AppCompat.Large"
            />

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            />

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

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

你可以保留ScrollView並添加這個XML屬性: android:nestedScrollingEnabled="true"所以它知道CoordinatorLayout是一個兄弟,請記住這個屬性僅在棒棒糖版本及以上版本中受支持。

使用NestedScrollView而不是常規的ScrollView使用時CoordinatorLayout

要使CollapsingToolbarLayout滾動,可以通過將NestedScrollView的子布局的最小高度設置為* 1000dp來觸發滾動行為

 android:minHeight="1000dp" 

布局:

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

    <!--to trigger scroll behavior-->
    <LinearLayout android:minHeight="1000dp"/>

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

* SupportDesignDemos示例: https//github.com/android/platform_development/blob/master/samples/SupportDesignDemos/res/layout/include_appbar_scrollview.xml

實際答案應該是CoordinatorLayout不能與ScrollView ,因為ScrollView沒有實現NestedScrollingChild接口。 NestedScrollView是一個帶有NestedScrollingChild實現的ScrollView 如果你想了解更多關於嵌套滾動的信息,我發了一篇關於它的博客文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM