Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


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:

CoordinatorLayout extends the ability to accomplish many of the Google's Material Design scrolling effects. 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.

My question is: in what exact ViewGroup (or maybe View) should we put that app:layout_behavior?


So far, I've tried with (And they have all worked, and they are all siblings to the AppBarLayout):

  • Scrolling View

  • First ViewGroup inside a Scrollable View

  • ScrollView inside a ViewGroup

And this one didn't work:

  • ViewGroup with no Scrollable View children.

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/

like image 893
Evin1_ Avatar asked Feb 03 '16 15:02

Evin1_


People also ask

How do you set layout behavior programmatically?

You can set the behavior on an instance of CoordinatorLayout. LayoutParams with setBehavior method. To get a proper Behavior object that represents the same thing as @string/appbar_scrolling_view_behavior you should create an instance of AppBarLayout. ScrollingViewBehavior .

How do I use CoordinatorLayout?

By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout by implementing the AttachedBehavior interface.


1 Answers

Check this link: 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. The binding is done through the AppBarLayout.ScrollingViewBehavior class, meaning that you should set your scrolling view's behavior to be an instance of 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:

<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 maybe View) should we put that app:layout_behavior?

And in this link: 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. Add an app:layout_behavior to a RecyclerView or any other View capable of nested scrolling such as NestedScrollView. The support library contains a special string resource @string/appbar_scrolling_view_behavior that maps to AppBarLayout.ScrollingViewBehavior, which is used to notify the AppBarLayout when scroll events occur on this particular view. The behavior must be established on the view that triggers the event.

like image 90
ʍѳђઽ૯ท Avatar answered Oct 06 '22 00:10

ʍѳђઽ૯ท