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

时间:2017-04-20 20:30:40

标签: android android-layout

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

________________________________
|        custom toolbar        |
|------------------------------|
|                              |
|         some content         |
|                              |
|------------------------------|
|    layout (or tab layout)    |
|------------------------------|
|                              |
|     rest of the contents     |
|                              |
|                              |
|                              |
|                              |
|______________________________|

滚动后我希望它像这样:

________________________________
|     layout (or tab layout)   |
|------------------------------|
|                              |
|     rest of the contents     |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|                              |
|______________________________|

有点像我的应用&游戏' Play商店应用中的页面。

1 个答案:

答案 0 :(得分:6)

1。使用CoordinatorLayout作为根布局。

2。AppBarLayoutNestedScrollView添加为CoordinatorLayout的直接子女

    AppBarLayout     -> Toolbar + Some content + TabLayout
    NestedScrollView -> Rest of the contents

3。AppBarLaout内,添加子CollapsingToolbarLayoutTabLayout。将ToolbarImageView保留到CollapsingToolbarLayout

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

       <TabLayout />
   </AppBarLaout>

4。将属性app:layout_scrollFlags="scroll|exitUntilCollapsed"添加到CollapsingToolbarLayout,将app:layout_scrollFlags="scroll|enterAlways"属性添加到Toolbar以获得折叠效果。

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

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

<CoordinatorLayout>

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

       <TabLayout />
    </AppBarLaout>

    <NestedScrollView>

        <!-- Your content -->

    </NestedScrollView>

<CoordinatorLayout>

以下是工作代码:

<?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>

希望这会有所帮助〜

相关问题