如何在工具栏和选项卡布局之间滚动内容

时间:2017-06-22 12:06:55

标签: java android android-layout

在我的布局中,我将CoordinatorLayout作为垂直方向的父级,         1 - 工具栏         2 - LinearLayout中的内容         3 - TabLayout         4 - ViewPager

当我向上滚动时,应使用视图寻呼机滚动内容,并且应使用工具栏附加标签布局。

怎么做?

<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/coordinatorLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- AppBar Layout   -->
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="fill_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="fill_parent"
                android:layout_height="?attr/actionBarSize"
                android:isScrollContainer="true"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <RelativeLayout
                app:layout_scrollFlags="scroll|enterAlways"

                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_centerInParent="true"
                    android:src="@mipmap/ic_launcher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>
            <!-- Tab Layout for creating tabs -->
            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </android.support.design.widget.AppBarLayout>
        <!-- Helps handing the Fragments for each Tab -->
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:0)

您需要将ViewPager包装在NestedScrollView中,并将布局行为移至滚动视图。

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

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

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.v4.widget.NestedScrollView

答案 1 :(得分:0)

//布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <include
        android:id="@+id/user_toolbar"
        layout="@layout/toolbar_user_profile"/>


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



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


            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapse_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/white"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:scrimAnimationDuration="100"
                app:titleEnabled="false">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    >
                    <include layout="@layout/user_content_profile_screen" />
                </LinearLayout>


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

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:visibility="visible"
                app:layout_collapseMode="pin"
                app:tabIndicatorColor="@color/black"
                app:tabSelectedTextColor="@color/black"
                app:tabTextColor="@color/levelTextColor"

                />

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

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

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white" />

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


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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_subjects"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add_white_24dp"
        android:visibility="gone"
        app:backgroundTint="@color/levelBackground"
        app:borderWidth="0dp"
        app:fabSize="normal"
        app:layout_anchor="@+id/viewPager"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="com.example.simon.behaviours.ScrollingFABBehavior" />


</RelativeLayout>