【问题标题】:ViewPager scroll layout behavior not working with ToolbarViewPager 滚动布局行为不适用于工具栏
【发布时间】:2016-06-01 11:16:10
【问题描述】:

我正在处理与 ViewPagerAppBarLayoutToolbar 相关的奇怪问题。

在我的布局中,我有一个标准的 Material Design 设置,例如:

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

除非我从我的 viewpager 中删除 appbar_scrolling_view_behavior,否则它们似乎都不起作用,因为 viewpager 位于 AppBarLayout 下方。

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

【问题讨论】:

  • 尝试在片段布局中的RecyclerView 中添加app:layout_behavior="@string/appbar_scrolling_view_behavior"
  • 您使用哪个版本的 appcompat ? 23.2 AppBarLayout + SwipeRefreshLayout + SwipeRefreshLayout 有一些错误
  • @Rehan 没什么,工具栏还是不能滚动.. 顺便谢谢你
  • @marco 我在 23.4.0,可能还有问题
  • 更多信息在这里。 code.google.com/p/android/issues/detail?id=201775 23.1.1 一切正常。但我不知道这是否是真正的问题

标签: android android-layout android-fragments android-viewpager android-toolbar


【解决方案1】:

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

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

将行为添加到底层ViewPager 并且它确实有效。现在假设您的AppbarLayout 是一个自定义实现,带有ToolBarTabLayout 和自定义视图说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。因此,请相应地放置您的观点。 Try this blogpost for details

【讨论】:

  • 这似乎是合法的,不幸的是我现在无法测试,因为此后应用程序设计发生了变化,我会稍后再试。也感谢博客的链接,这将非常有用
  • @Matteo 自首次发布以来,我一直在对这些组件进行试验。别担心,它肯定会奏效的。现在 QuickReturn 行为主要取决于 Viewpager 中布局的复杂性。不过欢迎!
【解决方案2】:

尝试像这样将您的工具栏包装在 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>

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

【讨论】:

    【解决方案3】:

    您应该使用 坐标布局 作为父布局,以便在您的视图分页器中访问 (app:layout_behavior )。

    【讨论】:

      【解决方案4】:

      你可以试试;

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-31
        • 1970-01-01
        • 2020-12-11
        • 2015-09-03
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多