Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using the CoordinatorLayout my ScrollView has an incorrect size

I'm using a ScrollView in a layout, and am attempting to use the new CoordinatorLayout from the design support library.

My layout file looks 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">    <ScrollView       android:layout_width="match_parent"       android:layout_height="match_parent"       app:layout_behavior="@string/appbar_scrolling_view_behavior">     <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical">       ...     </LinearLayout>   </ScrollView>   <android.support.design.widget.AppBarLayout       android:layout_width="match_parent"       android:layout_height="wrap_content">     <android.support.v7.widget.Toolbar ... />   </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 

When testing this the scrollview only occupies part of the screen. What went wrong?

like image 928
beetstra Avatar asked Jun 01 '15 13:06

beetstra


People also ask

Is CoordinatorLayout deprecated?

The CoordinatorLayout. DefaultBehavior annotation is deprecated.

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.

What is difference between nested ScrollView and NestedScrollView?

Nested scrolling is enabled by default. Show activity on this post. NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView. But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed.

What is Android CoordinatorLayout?

Android CoordinatorLayout is a super-powered FrameLayout. It has a lot more to offer than it seems. It has additional level of control over it's child views. It coordinates the animations and transitions of child views with one another.


2 Answers

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. Nested scrolling is enabled by default.

You can use a NestedScrollView within a parent ScrollView. NestedScrollView is used when there is a need for a scrolling view inside another scrolling view. This is where it is useful, when the system needs to decide which view to scroll.

Here is an example of NestedScrollView with CoordinatorLayout:

 <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> 
like image 36
live-love Avatar answered Sep 17 '22 14:09

live-love


The standard ScrollView is only meant to be used as a parent. You need to change the ScrollView to a android.support.v4.widget.NestedScrollView.

An example can be seen in the reference documentation for AppBarLayout.

like image 158
beetstra Avatar answered Sep 18 '22 14:09

beetstra