[英]Where should 'app:layout_behavior' be set?
Should it be set at the AppBarLayout sibling's parent or at the first Scrollable View inside its sibling?它应该设置在AppBarLayout兄弟姐妹的父级还是其兄弟姐妹内的第一个可滚动视图?
With Material Design for Android , there are Views that let us work with the behavior of the layout depending on its surroundings, one of them is the CoordinatorLayout , as this CodePath guide mentions:使用Material Design for Android ,有一些 视图可以让我们根据其周围环境处理布局的行为,其中之一是CoordinatorLayout ,正如本 CodePath 指南所述:
CoordinatorLayout extends the ability to accomplish many of the Google's Material Design scrolling effects. CoordinatorLayout 扩展了完成许多 Google Material Design 滚动效果的能力。 Currently, there are several ways provided in this framework that allow it to work without needing to write your own custom animation code.目前,该框架提供了多种方式,使其无需编写您自己的自定义动画代码即可工作。
The one I'm interested in now is:我现在感兴趣的是:
- Expanding or contracting the Toolbar or header space to make room for the main content.扩展或收缩工具栏或标题空间,为主要内容腾出空间。
So, we would use the AppBarLayout with a Toolbar with app:layout_scrollFlags
set and another ViewGroup sibling to the AppBarLayout with app:layout_behavior
.因此,我们将使用AppBarLayout一个工具栏与app:layout_scrollFlags
设置和其他的ViewGroup兄弟到AppBarLayout与app:layout_behavior
。
My question is: in what exact ViewGroup (or maybe View) should we put that app:layout_behavior
?我的问题是:我们应该将该app:layout_behavior
放在哪个 ViewGroup(或者可能是 View)中?
So far, I've tried with (And they have all worked , and they are all siblings to the AppBarLayout):到目前为止,我已经尝试过(并且它们都有效,并且它们都是 AppBarLayout 的兄弟):
Scrolling View滚动视图
First ViewGroup inside a Scrollable View可滚动视图中的第一个 ViewGroup
ScrollView inside a ViewGroup ViewGroup 内的 ScrollView
And this one didn't work:而这个没有用:
There are multiple examples online, but none of them really state where should you put it, like:网上有很多例子,但没有一个真正说明你应该把它放在哪里,比如:
http://www.ingloriousmind.com/blog/quick-look-on-the-coordinatorlayout/ https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout https://developer.android.com/training/basics/firstapp/building-ui.html https://www.bignerdranch.com/blog/becoming-material-with-android-design-support-library/ http://www.ingloriousmind.com/blog/quick-look-on-the-coordinatorlayout/ https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout https://developer.android.com /training/basics/firstapp/building-ui.html https://www.bignerdranch.com/blog/becoming-material-with-android-design-support-library/
Check this link: https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html检查此链接: https : //developer.android.com/reference/android/support/design/widget/AppBarLayout.html
AppBarLayout
also requires a separate scrolling sibling in order to know when to scroll.AppBarLayout
还需要一个单独的滚动兄弟,以便知道何时滚动。 The binding is done through theAppBarLayout.ScrollingViewBehavior
class, meaning that you should set your scrolling view's behavior to be an instance ofAppBarLayout.ScrollingViewBehavior
.绑定是通过AppBarLayout.ScrollingViewBehavior
类完成的,这意味着您应该将滚动视图的行为设置为AppBarLayout.ScrollingViewBehavior
的实例。 A string resource containing the full class name is available.包含完整类名的字符串资源可用。
They mentioned about that, it should be the View
which will be shown under the AppBarLayout
like this:他们提到了这一点,它应该是将在AppBarLayout
下显示的View
,如下所示:
<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.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Your scrolling content -->
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
...
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
My question is: in what exact
ViewGroup
(or maybeView
) should we put thatapp:layout_behavior
?我的问题是:我们应该把那个app:layout_behavior
放在哪个ViewGroup
(或者View
)中?
And in this link: http://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout在这个链接中: http : //guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout
Next, we need to define an association between the
AppBarLayout
and the View that will be scrolled .接下来,我们需要定义AppBarLayout
和将被滚动的 View之间的关联。 Add anapp:layout_behavior
to aRecyclerView
or any other View capable of nested scrolling such asNestedScrollView
.将app:layout_behavior
添加到RecyclerView
或任何其他能够嵌套滚动的视图,例如NestedScrollView
。 The support library contains a special string resource@string/appbar_scrolling_view_behavior
that maps toAppBarLayout.ScrollingViewBehavior
, which is used to notify theAppBarLayout
when scroll events occur on this particular view .支持库包含映射到AppBarLayout.ScrollingViewBehavior
的特殊字符串资源@string/appbar_scrolling_view_behavior
,用于在此特定视图上发生滚动事件时通知AppBarLayout
。 The behavior must be established on the view that triggers the event.行为必须建立在触发事件的视图上。
Make sure you added the appbar_scrolling_view_behavior field in your String.xml请确保你在String.xml添加appbar_scrolling_view_behavior场
<!-- The class name to the ScrollingChildBehavior required for AppBarLayout -->
<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>
And as everyone knows we just can use this like below众所周知,我们可以像下面这样使用它
<android.support.v7.widget.RecyclerView
android:id="@+id/rvSomeList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Its just for info not OP answer.它只是为了信息而不是 OP 答案。
app:layout_behavior
应设置为协调器布局的直接子视图
I had to add the following to the gradle file otherwise it gave me a compile error.我必须将以下内容添加到 gradle 文件中,否则它会给我一个编译错误。
implementation 'com.google.android.material:material:1.0.0'
Hope this would help some others too!希望这也能帮助其他人!
For someone who uses CoordinatorLayout
with FragmentContainer
and AppBarLayout
:对于将CoordinatorLayout
与FragmentContainer
和AppBarLayout
一起使用的人:
app:layout_behavior
also on the container (not just on NestedScrollView
or RecyclerView
).在容器上设置app:layout_behavior
真的很好(不仅仅是在NestedScrollView
或RecyclerView
)。 It deletes unnecessary bottom margin of the FragmentContainer
and guarantees that the appbar hides when the keyboard is shown.它删除FragmentContainer
不必要的底部边距,并保证在显示键盘时隐藏应用栏。AppBarLayout also requires a separate scrolling sibling in order to know when to scroll. AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。
This description from Android is woefully incomplete and caused me hours of wasted time. Android 的这个描述非常不完整,导致我浪费了数小时的时间。
Scrolling sibling is a misnomer and need not be a scrolling view of any type.滚动同级是用词不当,不需要是任何类型的滚动视图。
For example, below my AppBarLayout
, I'm using a ViewPager2
that will render a Fragment
that will render a Scrollview
, so I needed to set app:layout_behavior="@string/appbar_scrolling_view_behavior"
directly on the ViewPager2
in the main layout, NOT the deeply nested Scrollview
in the fragment layout.例如,在我的AppBarLayout
下方,我使用的是ViewPager2
,它将呈现一个将呈现Scrollview
的Fragment
,所以我需要直接在主布局的ViewPager2
上设置app:layout_behavior="@string/appbar_scrolling_view_behavior"
,而不是在片段布局中深度嵌套Scrollview
。
I also have no use for scrolling the AppBarLayout
or any of its children on or off the screen, so I falsely assumed I could get away with not setting the app:layout_behavior
anywhere.我也没有用于在屏幕上或屏幕外滚动AppBarLayout
或其任何子项,所以我错误地认为我可以逃脱不设置app:layout_behavior
任何地方。
Wrong.错误的。
This reveals a more insidious issue: AppBarLayout
requires the scrolling sibling , yes.这揭示了一个更阴险的问题: AppBarLayout
需要滚动兄弟,是的。 But not just to "know when to scroll", but to actually adjust the size of the sibling to fit properly on screen alongside it!但不仅仅是“知道何时滚动”,而是要实际调整兄弟姐妹的大小以使其正确地显示在屏幕上! Otherwise, the sibling maintains its configured size and will be nudged downward offscreen by the height of the AppBarLayout
!否则,同级将保持其配置的大小,并将被AppBarLayout
的高度向下推到屏幕外! You can even see this in Android Studio's layout editor.您甚至可以在 Android Studio 的布局编辑器中看到这一点。
Long story short: If you're going to use an AppBarLayout
, you need to mark one of your views with app:layout_behavior="@string/appbar_scrolling_view_behavior"
, whether it's a scroll view or not.长话短说:如果您打算使用AppBarLayout
,则需要使用app:layout_behavior="@string/appbar_scrolling_view_behavior"
标记您的视图之一,无论它是否是滚动视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.