10

I am having a Nested ScrollView which contains contents inside a Linear Layout.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:CoverFlowPager="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:clipToPadding="false"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

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

</FrameLayout>

I have this layout inside a ViewPager and ViewPager is inside a CordinatorLayout.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

Now when I scroll the view the view is not scrolling. But since the layout is placed inside the Cordinator layout its moving up till the ToolBar is hid. But its not scrolling up.

Here is my main activity xml, The view pager is inside a tabbed layout.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

           <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:attrs="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            <FrameLayout
                android:id="@+id/titleContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"    
                android:gravity="center">

                <com.CustomFontTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Toolbar Title"
                    android:textColor="#ffffff"
                    attrs:customFont="handyman_bold"
                    android:textSize="8pt"
                    android:layout_marginLeft="-20dp"
                    android:id="@+id/toolbar_title"/>
            </FrameLayout>

            <ImageButton
                android:id="@+id/btn_ToolBarRightBtn"
                android:layout_width="32dp"
                android:layout_height="28dp"
                android:tag="0"
                android:background="@null"
                android:scaleType="fitXY"
                android:src="@drawable/icon_shopping"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:layout_marginRight="10dp"
                />   

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_gravity="bottom"
        android:background="@color/offwhite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.design.widget.CoordinatorLayout>

Here is my screenshots,

Initial View enter image description here

When view is scrolled up, it scrolls only to hide the top navigation bar. Its not scrolling to display the items below the tab bar,

enter image description here

zys
  • 1,176
  • 3
  • 16
  • 34
Zach
  • 9,411
  • 18
  • 63
  • 102

6 Answers6

1

app:layout_behavior="..." should be set on a direct child of CoordinatorLayout. If your ViewPager is a direct child of CoordinatorLayout, place it to ViewPager declaration.

Artem Ufimtcev
  • 321
  • 2
  • 4
0

Quote from here: Troubleshooting Coordinator Layouts

When coordinating between a fragment with a list of items inside of a ViewPager and a parent activity, you want to put the app:layout_behavior property on the ViewPager as outlined here so the scrolls within the pager are bubbled up and can be managed by the CoordinatorLayout. Note that you should not put that app:layout_behavior property anywhere within the fragment or the list within.

Adam Kimi
  • 46
  • 4
0

You can try this! I had the same problem with my app. This worked for me.

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">


    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:attrs="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <FrameLayout
            android:id="@+id/titleContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:gravity="center">

            <com.CustomFontTextView
                android:id="@+id/toolbar_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-20dp"
                android:gravity="center"
                android:text="Toolbar Title"
                android:textColor="#ffffff"
                android:textSize="8pt"
                attrs:customFont="handyman_bold" />
        </FrameLayout>

        <ImageButton
            android:id="@+id/btn_ToolBarRightBtn"
            android:layout_width="32dp"
            android:layout_height="28dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/icon_shopping"
            android:tag="0" />


    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/offwhite" />


    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:clipToPadding="false"
    android:fillViewport="true">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"></LinearLayout>
</android.support.v4.widget.NestedScrollView>

tintin21
  • 245
  • 2
  • 13
0

try this

scroll.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});
Pratik
  • 436
  • 4
  • 17
0

The ScrollingViewBehavior is just add top and bottom offset to your ViewPager. Generally the offset depends on AppBarLayout height and it do not depends on your TabLayout height because it is not in the AppBarLayout. So, for quick workaround you can just add paddingBottom to your ViewPager like this:

<android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="50dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_gravity="bottom"
        android:background="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>
Zellius
  • 216
  • 1
  • 9
-1

So here is the problem, you cannot have a NestedScrollView within the FrameLayout.

Also, some other smaller changes needed in xml to make it work. Like the TabLayout has to be inside AppBar Layout and be pinned.

Here's your code -->

</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_gravity="bottom"
android:background="@color/offwhite"
android:layout_width="match_parent"
app:layout_collapseMode="pin"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom"
android:layout_height="wrap_content"/>

</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

And --> Change your Fragment code to remove the frame layout!

    <android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:CoverFlowPager="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:clipToPadding="false"
        android:fillViewport="true">

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

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

Hope this helps!

Kakshil Shah
  • 2,726
  • 14
  • 30
Anant Shah
  • 3,069
  • 27
  • 46