滚动时自动隐藏工具栏,当中间有其他几层视图时 [英] Auto-hiding toolbar on scroll, when there are several other layers of views in-between

查看:76
本文介绍了滚动时自动隐藏工具栏,当中间有其他几层视图时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的活动的Toolbar随着给定的scrollview的增长而平滑地滚动出,然后如果用户向下滚动则平滑地向后滚动. IE.基本的scroll|enterAlways行为,例如答案.

I would like my activity's Toolbar to smoothly scroll out as a given scrollview grows, and then smoothly scroll back in if the user scrolls down. I.e. the basic scroll|enterAlways behaviour from e.g. this answer.

但是,我的布局在包含ToolbarCoordinatorLayout和应该驱动所有内容的ScrollView之间有一些层.与此答案不同,我希望采用以下布局:

However, my layout has some layers between the CoordinatorLayout containing the Toolbar, and the ScrollView that should be driving everything. Unlike in this answer, I'd like to have the following layout:

CoordinatorLayout
  AppBarLayout
    Toolbar
  LinearLayout [vertical]
    LinearLayout [vertical]
      ImageView
      ScrollView

看似多余的两个LinearLayout用于控制背景色;而ImageViewScrollView的想法是我的UI应该如下所示:

The seemingly redundant two LinearLayouts are for background color control; and the idea with the ImageView and the ScrollView is that my UI should look like this:

+------------+
|TOOLBAR    =|
+------------+
|            |
|   IMAGE    |
|            |
|111111111111| 
|222222222222|
|333333333333|
|444444444444|
+------------+

然后应滚动到

+TOOLBAR----=+
+------------+
|            |
|   IMAGE    |
|            |
|111111111111| 
|222222222222|
|333333333333|
|444444444444|
|555555555555|
+------------+

并进一步

+------------+
|            |
|   IMAGE    |
|            |
|111111111111| 
|222222222222|
|333333333333|
|444444444444|
|555555555555|
|666666666666|
+------------+

然后到

+------------+
|            |
|   IMAGE    |
|            |
|222222222222|
|333333333333|
|444444444444|
|555555555555|
|666666666666|
|777777777777| 
+------------+

(数字代表ScrollView中的内容).

(with the numbers representing the things in the ScrollView).

仅通过设置即可获得上述行为 Toolbar上的app:layout_scrollFlags="scroll|enterAlways"作为标准.但是,如果我随后开始在ScrollView上向下拖动,则Toolbar不会回来.实际上,一旦Toolbar滚动出来,我还没有找到任何方法来取回它.

The above behaviour I was able to get just by setting app:layout_scrollFlags="scroll|enterAlways" on the Toolbar as standard. However, if I then start dragging down on the ScrollView, the Toolbar doesn't come back. In fact, I haven't found any way to get the Toolbar back once it has scrolled out.

推荐答案

原来,我需要使用

It turns out I need to use NestedScrollView, since, unlike its name suggested to me, it is not just for nesting ScrollViews into other ScrollViews, but also for nesting a ScrollView inside other containers:

NestedScrollView就像ScrollView一样,但是它支持表演 作为新老版本Android上的嵌套滚动父级和子级.

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android.

这篇关于滚动时自动隐藏工具栏,当中间有其他几层视图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆