Where should 'app:layout_behavior' be set?

后端 未结 5 2090
萌比男神i
萌比男神i 2020-12-07 17:39

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 th

相关标签:
5条回答
  • 2020-12-07 18:20

    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.

    0 讨论(0)
  • 2020-12-07 18:21

    app:layout_behavior should be set to those views which are direct child of Coordinator layout

    0 讨论(0)
  • 2020-12-07 18:26

    For someone who uses CoordinatorLayout with FragmentContainer and AppBarLayout:

    • It is really good to set the app:layout_behavior also on the container (not just on NestedScrollView or RecyclerView). It deletes unnecessary bottom margin of the FragmentContainer and guarantees that the appbar hides when the keyboard is shown.
    0 讨论(0)
  • 2020-12-07 18:36

    I had to add the following to the gradle file otherwise it gave me a compile error.

    implementation 'com.google.android.material:material:1.0.0'
    

    Hope this would help some others too!

    0 讨论(0)
  • 2020-12-07 18:37

    Make sure you added the appbar_scrolling_view_behavior field in your String.xml

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

    0 讨论(0)
提交回复
热议问题