Search code examples
androidandroid-layoutandroid-viewpagerscrollview

Unable to Put ViewPager inside a ScrollVIew/NestedScrollView


I have a ViewPager which resides inside a parent Linear Layout and below the ViewPager there are some buttons. The ViewPager was working great until recently I changed the parent from LinearLayout to a ScrollView. Now the ViewPager disappeared. Please don't mark it as spam or duplicate.

Also, there are tons of apps on Playstore currently using this. See this app - link

What I Tried:

  1. setFillViewport (true) - This didn't work for me.
  2. Set layout_height to 0dp
  3. Use a CustomScrollView

All these doesn't help me. I am running the app on Android M.

This is my layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
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:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".activities.HomeActivity">

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

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:onClick="pagerClick"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="top"
android:layout_weight="1"
android:overScrollMode="never"/>

<RelativeLayout
android:layout_weight="0.8"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....two buttons
</RelativeLayout>

</LinearLayout>

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

Question:

How can I achieve this? Also, is there an official way of doing it?


Solution

  • I solved the issue by fixing the height of my ViewPager like :

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
    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:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".activities.HomeActivity">
    
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:onClick="pagerClick"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="top"/>
    
    ...
    
    </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>