视图寻呼器滚动布局行为不适用于工具栏



我正在处理一些与ViewPagerAppBarLayoutToolbar有关的奇怪事情。

在我的布局中,我有一个标准的材质设计设置,例如:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="...">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/grey_900"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways" />
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabBackground="@color/grey_900"
            app:tabIndicatorColor="@color/white_1000"
            app:tabIndicatorHeight="4dp" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.design.widget.FloatingActionButton
        ...
        app:layout_behavior="com.inkstinctapp.inkstinct.FabBehavior" />
</android.support.design.widget.CoordinatorLayout>

我的 ViewPager 包含 4 个具有此布局的片段:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <me.zhanghai.android.materialprogressbar.MaterialProgressBar
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:indeterminate="true"
        android:tint="@color/white_1000"
        android:layout_centerInParent="true" />
    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

我无法让工具栏滚动,我已经尝试了以下所有组合:

app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|enterAlways"

除非我从我的视图页中删除appbar_scrolling_view_behavior,否则它们似乎都不起作用,这是不行的,因为视图页程序位于 AppBarLayout 下。

我做错了什么?任何帮助将不胜感激!

下面是您想要实现的行为的广泛布局实现。

<CoordinatorLayout>
 <AppbarLayout/>
 <Viewpager 
 .
 .
 app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
 <FloatingActionButton/>
</CoordinatorLayout>

将行为添加到底层ViewPager,它确实有效。现在,假设您的AppbarLayout是一个自定义实现,其中ToolBar TabLayout和自定义视图表示LinearLayout这就是该代码应实现 scroll 属性的方式。

<AppBarLayout>
    <CollapsingToolbarLayout
        app:layout_scrollFlags="scroll|snap"
        />
    <Toolbar
        app:layout_scrollFlags="scroll|snap"
        />
    <LinearLayout
        android:id="+id/title_container"
        app:layout_scrollFlags="scroll|enterAlways"
        />
    <TabLayout /> <!-- no flags -->
</AppBarLayout>

此外,您可以尝试标志及其值。但请记住,AppbarLayout是一个拥有超能力的垂直LinearLayout。因此,请相应地放置您的观点。试试这篇博文了解详情

尝试将工具栏包装在 CollapsingToolbarLayout 中,如下所示

<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            />
    </android.support.design.widget.CollapsingToolbarLayout>
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@color/ezy_green"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/gray_background"
        app:tabIndicatorHeight="4dp"
        app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

并且不要忘记将滚动视图行为添加到回收器视图,并设置其余属性以满足您的需求

您应该使用坐标布局作为父布局,以便在视图寻呼机中访问 (app:layout_behavior )。

你可以试试;

app:layout_behavior="@string/appbar_scrolling_view_behavior"

例如代码;

<android.support.design.widget.CoordinatorLayout 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:id="@+id/mainScreen">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_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="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    -->
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        style="@style/AppTabLayout"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<!--<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragment_container">-->
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<!--</FrameLayout>-->

最新更新