android – CollapsingToolbarLayout并在滚动时隐藏工具栏

前端之家收集整理的这篇文章主要介绍了android – CollapsingToolbarLayout并在滚动时隐藏工具栏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用CoordinatorLayout和CollapsingToolbarLayout创建一些组合的布局.

在第一个状态下,当我们在最上面的页面上,还没有滚动时,我希望工具栏消耗如下所示(是的,我做到了):

在第二种状态下,当开始向下滚动时,图像和工具栏应该消失,如下所示(仅显示标签):

在最后一个状态,一旦我在列表中的某个点(但不是列表的顶部),我想开始向上滚动,一旦我开始滚动我想要的工具栏(而不是消耗的图像)到开始whowing,如下图所示(如果没有到达列表的顶部,图像将不会显示,只有工具栏):

我能够掌握第一个状态,但另外2个状态是有问题的,
一旦在CollapsingToolbarLayout中实现了一个工具栏,我可以在CollapsingToolbarLayout组件之外实现的可伸缩性并不清楚.
我无法使工具栏隐藏,如果我这样做,那么只有当我到达顶部才会显示.

无论如何,我当前的XML(如下所示)处于第一张图片被执行的状态,但一旦我开始向下滚动,工具栏保持在顶部,不要隐藏.注意:我必须告诉工具栏保持“pin”,因为如果我没有,那么工具栏中的信息就会消失,只有一个空的工具栏会显示(这是另一个帖子,但仍然有意思的是为什么会发生这种情况?) .

这是我当前的xml:

  1. <android.support.design.widget.CoordinatorLayout
  2. android:id="@+id/benefit_coordinator_layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <android.support.design.widget.AppBarLayout
  7. android:id="@+id/app_bar_material_layout"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content">
  10.  
  11. <android.support.design.widget.CollapsingToolbarLayout
  12. android:id="@+id/main.collapsing"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. app:contentScrim="?attr/colorPrimary"
  16. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  17. >
  18.  
  19. <include
  20. android:id="@+id/toolbar_search_container"
  21. layout="@layout/search_Box"
  22. android:layout_height="192dp"
  23. android:layout_width="match_parent"
  24. app:layout_collapseMode="parallax"
  25.  
  26. />
  27.  
  28. <include
  29. android:id="@+id/toolbar_benefit"
  30. layout="@layout/toolbar_main"
  31. android:layout_width="match_parent"
  32. android:layout_height="?attr/actionBarSize"
  33. app:contentScrim="?attr/colorPrimary"
  34.  
  35. />
  36.  
  37. </android.support.design.widget.CollapsingToolbarLayout>
  38.  
  39. <android.support.design.widget.TabLayout
  40. android:id="@+id/benefit_tab_layout"
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content"
  43. android:background="@color/primaryColor"
  44. app:tabIndicatorColor="@color/accentColor"
  45. app:tabSelectedTextColor="@android:color/white"
  46. app:tabTextColor="@android:color/black"
  47. app:tabIndicatorHeight="4dp" />
  48.  
  49. </android.support.design.widget.AppBarLayout>
  50.  
  51. <android.support.v4.view.ViewPager
  52. android:id="@+id/benefit_pager"
  53. android:layout_width="match_parent"
  54. android:layout_height="match_parent"
  55. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  56. <include
  57. layout="@layout/floating_btn_benefits"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_gravity="bottom|right"
  61. android:layout_margin="16dp"
  62. />
  63. </android.support.design.widget.CoordinatorLayou

解决方法

我已经解决了这个问题,只是为了表彰,我希望我的工具栏能够在达到顶部时使用一个paralex图像进行扩展,但是如果滚动下来,我也希望工具栏消失,再次显示(没有paralex图像) )一旦我向上滚动.只有当我到达顶部时,才会显示旁白图像效果.

所以基本上解决方案是,使用以下属性更改组件CollapsingToolbarLayout:

  1. app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"

并使用以下属性更改工具栏组件

  1. android:minHeight="?attr/actionBarSize"

关于我的paralex效果图像(这是我的toolbar_search_container)我不应该添加任何layout_scrollFlags到它.

那为什么它工作?
要了解它,你需要知道什么是enterAlwaysCollapsed,
enterAlwaysCollapsed效果视图添加了minHeight属性.这意味着,具有minHeight的CollapsingToolbarLayout的每个孩子都将受到此属性的影响.
所以我的工具栏会受到影响.

enterAlwaysCollapsed attrubute定义简单单词:

假设enterAlways被声明,并指定了一个minHeight,你也可以指定enterAlwaysCollapsed.当使用此设置时,您的视图将仅显示在此最小高度.只有当滚动到达顶部时,视图才能扩展到其完整高度…“

那么这不是我们想要的吗? (不要回答这个retorical问题))

另外还有一件事情是,paralexed组件(toolbar_search_container)取决于要扩展的收费栏,因为工具栏只有到达顶部时才会扩展,所以这一切都是非常棒的!

新的代码是:

  1. <android.support.design.widget.CoordinatorLayout
  2. android:id="@+id/benefit_coordinator_layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true">
  6.  
  7. <android.support.design.widget.AppBarLayout
  8. android:id="@+id/app_bar_material_layout"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:fitsSystemWindows="true">
  12.  
  13. <android.support.design.widget.CollapsingToolbarLayout
  14. android:id="@+id/main.collapsing"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:fitsSystemWindows="true"
  18. app:contentScrim="?attr/colorPrimary"
  19. app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
  20. >
  21.  
  22. <include
  23. android:id="@+id/toolbar_search_container"
  24. layout="@layout/search_Box"
  25. android:layout_height="192dp"
  26. android:layout_width="match_parent"
  27. app:layout_collapseMode="parallax"
  28. />
  29.  
  30. <include
  31. android:id="@+id/toolbar_benefit"
  32. layout="@layout/toolbar_main"
  33. android:layout_width="match_parent"
  34. android:layout_height="?attr/actionBarSize"
  35. android:minHeight="?attr/actionBarSize"
  36. app:contentScrim="?attr/colorPrimary"
  37. app:layout_collapseMode="pin"
  38. android:fitsSystemWindows="true"
  39. />
  40.  
  41. </android.support.design.widget.CollapsingToolbarLayout>
  42.  
  43. <android.support.design.widget.TabLayout
  44. android:id="@+id/benefit_tab_layout"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:background="@color/primaryColor"
  48. app:tabIndicatorColor="@color/accentColor"
  49. app:tabSelectedTextColor="@android:color/white"
  50. app:tabTextColor="@android:color/black"
  51. app:tabIndicatorHeight="4dp" />
  52.  
  53. </android.support.design.widget.AppBarLayout>
  54.  
  55. <android.support.v4.view.ViewPager
  56. android:id="@+id/benefit_pager"
  57. android:layout_width="match_parent"
  58. android:layout_height="match_parent"
  59. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  60. <include
  61. layout="@layout/floating_btn_benefits"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:layout_gravity="bottom|right"
  65. android:layout_margin="16dp"
  66. />
  67. </android.support.design.widget.CoordinatorLayout>

猜你在找的Android相关文章