28

I implement NonSwipeableViewPager with a fragment has NestedScrollView like this, what I expect is that the scrollview can scroll up and show 2 textviews:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/header" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"
                android:src="@drawable/ic_up" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

But it could not scroll, I tried many ways but still did not get any solution

Norutan
  • 1,352
  • 1
  • 11
  • 24
  • 1
    ScrollView needs their child's height to be wrap_content – Tim May 13 '16 at 09:24
  • @TimCastelijns so we don't have any ways to do it with match_parent height, do we? – Norutan May 13 '16 at 09:32
  • that's just how a scrollview works, you can set the scrollview to match parent, but not the child of the scrollview, that needs to be wrap content – Tim May 13 '16 at 09:34

5 Answers5

116
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

This linearlayout should have android:layout_height="wrap_content".

Reason for that is that if the scrollview's child is the same size as the scrollview itself (both match_parent for height) it means that there is nothing to scroll through, since they are of same size and the scrollview will only be as high as the screen.

If the linearlayout has a height of wrap_content then the height is not related to the height of the screen and the scrollview will be able to scroll through it.

Just remember that a scrollview can only have 1 direct child, and that child needs android:layout_height="wrap_content"

Tim
  • 38,263
  • 17
  • 115
  • 131
  • 12
    For me, only `fillViewPort = true` woked. Child's height didn't matter in my case – Dhruvam Gupta Oct 20 '17 at 13:59
  • Yup same here! Just using `fillViewPort = true` worked. But @Tim has a point about `android:layout_height` which is true at times, faced issues in the past. – sud007 Feb 20 '20 at 11:10
  • FYI `fillViewPort=true` will make it so the scrollview takes up the remaining space on the screen, if the content of the scrollview in itself does not – Tim Oct 22 '20 at 11:58
12

In my case app:layout_behavior="@string/appbar_scrolling_view_behavior" this is working only if some one face problem will be try it and may be solve your problem too. you should add also android:fillViewport="true" but without this my code working.

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/subscription_background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
Tariqul
  • 2,043
  • 1
  • 15
  • 28
2

For me it worked when i added "android:layout_marginBottom="100dp"" for last child in androidx.core.widget.NestedScrollView

  • Not a solution, but I saw this same thing, I'm yet to understand why the scrollview doesn't understand the content is scrollable – Boy Sep 20 '20 at 18:17
  • Had the same problem added "android:layout_marginBottom="200dp" worked for me. – Toby Feb 15 '21 at 14:08
0

if you have used netedscrollview as follow you have to use android:scrollbars="vertical"

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar_updateUserDetails"
    app:layout_constraintBottom_toBottomOf="parent"
    android:fillViewport="true">
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        >
</LinearLayout>
</androidx.core.widget.NestedScrollView>
-1

You have to calculate your other child because last child binding on nestedScrollView. You add margin like child height. It is working.