滚动时如何在顶部修复布局位置

Had*_*290 5 android android-layout

我在活动中间有一个布局(或标签布局),我希望当用户滚动并且此布局到达顶部时,它保持在顶部(替换工具栏),其余内容保持滚动.例如,我的页面如下所示:

________________________________
|        custom toolbar        |
|------------------------------|
|                              |
|         some content         |
|                              |
|------------------------------|
|    layout (or tab layout)    |
|------------------------------|
|                              |
|     rest of the contents     |
|                              |
|                              |
|                              |
|                              |
|______________________________|
Run Code Online (Sandbox Code Playgroud)

滚动后我希望它像这样:

________________________________
|     layout (or tab layout)   |
|------------------------------|
|                              |
|     rest of the contents     |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|______________________________|
Run Code Online (Sandbox Code Playgroud)

有点像Play商店应用中的"我的应用和游戏"页面.

Fer*_*med 9

1.使用CoordinatorLayout作为根布局.

2.添加AppBarLayoutNestedScrollView作为直接的孩子CoordinatorLayout

    AppBarLayout     -> Toolbar + Some content + TabLayout
    NestedScrollView -> Rest of the contents
Run Code Online (Sandbox Code Playgroud)

3.在里面AppBarLaout,加上孩子CollapsingToolbarLayoutTabLayout.保持ToolbarImageView进入CollapsingToolbarLayout.

   <AppBarLaout>
       <CollapsingToolbarLayout>
           <ImageView /> 
           <Toolbar />
       </CollapsingToolbarLayout>   

       <TabLayout />
   </AppBarLaout>
Run Code Online (Sandbox Code Playgroud)

4.属性添加app:layout_scrollFlags="scroll|exitUntilCollapsed"CollapsingToolbarLayout和属性app:layout_scrollFlags="scroll|enterAlways",以Toolbar用于崩溃效果.

5.属性添加app:layout_behavior="@string/appbar_scrolling_view_behavior"NestedScrollView您的内容scrolling的行为.

您的最终布局结构应如下所示:

<CoordinatorLayout>

    <AppBarLaout>
       <CollapsingToolbarLayout>
           <ImageView /> 
           <Toolbar />
       </CollapsingToolbarLayout>   

       <TabLayout />
    </AppBarLaout>

    <NestedScrollView>

        <!-- Your content -->

    </NestedScrollView>

<CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这是一个有效的代码:

<?xml version="1.0" encoding="utf-8"?>
<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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="206dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

            <ImageView
                android:id="@+id/image_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

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

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:tabGravity="fill"
            app:tabMode="scrollable" />

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

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

        <!-- Your content -->

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

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助〜