【问题标题】:AppBarLayout inside ViewPager with scrolling behavior具有滚动行为的 ViewPager 内的 AppBarLayout
【发布时间】:2016-05-25 19:43:00
【问题描述】:

有没有办法将 AppBarLayout 放入 ViewPager 并让滚动行为正常工作?背景是我想要在用户向下滚动时滚动出屏幕的第一个片段上的标题。 ViewPager 中的所有其他片段都没有标题。到目前为止,我的努力没有成功,因为标题没有滚动。在 ViewPager 之外,滚动工作正常。

感谢您的帮助。

活动布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

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

            <include layout="@layout/toolbar"/>

            <include layout="@layout/toolbar_shadow"/>

        </LinearLayout>

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu"/>

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

片段布局:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/space_between_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:elevation="0dp">

        <!-- HEADER -->
        <RelativeLayout
            android:id="@+id/header_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">

            <include layout="@layout/journal_header" />

        </RelativeLayout>

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

    <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/button_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white_24dp"
        app:layout_behavior="de.malnvenshorn.slimly.ui.component.ScrollingBehaviorFAB">

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/button_add_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_restaurant_menu_white_24dp"
            fab:fab_size="mini"/>

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/button_add_activity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_directions_bike_white_24dp"
            fab:fab_size="mini"/>

    </com.github.clans.fab.FloatingActionMenu>

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

【问题讨论】:

    标签: android android-viewpager android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    来自 Android 文档 AppBarLayout https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

    此视图在很大程度上取决于在 CoordinatorLayout 中用作直接子级。如果您在不同的 ViewGroup 中使用 AppBarLayout,它的大部分功能都将不起作用。

    尝试将您的 AppBarlLayout 放在 CoordinatorLayout 中。

    【讨论】:

      猜你喜欢
      • 2015-12-15
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多