Tefa
Tefa

Reputation: 346

How make put ViewPager inside ScrollView and scroll all view as one part?


I have an that have inside like the follwoing

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

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


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

                    <!--someViews-->

                </LinearLayout>


                <!--then viewPager-->
                <androidx.viewpager.widget.ViewPager
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>

        </ScrollView>

Then the view pager holds some which have a tall view too. The problem is the scroll view not scrolling because of the view is not need to scroll but the view inside the viewpager fragment's not scrolling so, Is it possible to make the whole view including the inside view pager scroll as one view? What I am saying that I don't need to scroll the viewpager fragment as separated part I need to scroll it as part of activity view.

Thanks

Upvotes: 1

Views: 860

Answers (1)

kandroidj
kandroidj

Reputation: 13932

I believe the setup you are looking for would work inside a CoordinatorLayout using the NestedScrollView widget like so:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
  android:layout_height="match_parent">

  <androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

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

      <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1" />

      <TextView
        android:id="@+id/subTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/subtitle"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" />

      <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    </LinearLayout>

  </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

I have just tested this with a sample project with a single text view in a Fragment and I was able to both scroll the view and swipe through the pages.

Please note though: I did have to provide a specific height for the ViewPager in order for it to display.

Upvotes: 1

Related Questions