1


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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

yes it worked when I put a high to the viewpager but I don't need to do that because I don't know what high will be enough for the fragment inside the viewpager
@Tefa if that is the case, then ViewPager is not the right widget to use. Consider using a RecyclerView with a horizontal LayoutManager, the NestedScrollView would still apply though.
I already removed the recycler-view and used FrameLayout and replaced may fragments on it
This is working well but why we have to use fixed height ? Because hard code isn't recommended. Is there any other way ? Thanks.
@MehmetGür i believe NestedScrollView wraps it's content, so despite saying match parent in the layout it doesn't know how big to draw itself so the child needs to be sized instead

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.