How can I make ViewPager work with RecyclerView inside NestedScrollView

I am using NestedScrollView with ViewPager. NestedScrollView has a LinearLayout inside with some TextViews, TabLayout and ViewPager at the end. TextViews take up most of the space, and there is not much space left for the ViewPager. ViewPager uses two fragments, in one of them there are several TextViews and ImageViews, and in the other fragment there is RecyclerView.

When I set the height of the ViewPager to WRAP_CONTENT , it only takes up the remaining space, and I cannot scroll to see the rest of the first fragment, and the second scoll fragment inside the small ViewPager.

When I set the height of the ViewPager to 1000dp, for example, I can scroll down the first fragment, but the second fragment still scrolls inside the small ViewPager. And after I loop through the fragment using Scyccess RecyclerView in the first fragment that no longer works.

How can I fix the scroll problem and get ViewPager to work with WRAP_CONTENT ?

Here is my code:

 <?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_scrollFlags="scroll" android:fillViewport="true"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/mainBackground" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SOME TEXT" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SOME TEXT" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SOME TEXT" /> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> 
+7
source share
3 answers

I think you should override ViewPager as follows:

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = 0; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); if (h > height) height = h; } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 
+9
source

maybe you can try:

  NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.nest_scrollview); scrollView.setFillViewport (true); 

e.g. fooobar.com/questions/73824 / ...

+1
source

@StevenZhang's solution almost worked for me, but there was a problem when the two tabs have different heights, the smaller tab takes the height of the larger tab.

Based on this answer on how to make ViewPager dynamic height I made a decision in Kotlin

Custom view pager

 class CustomViewPager: ViewPager { constructor(context: Context) : super(context) constructor(context : Context, attrs : AttributeSet) : super(context, attrs) private var currentView : View? = null override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { currentView?.let { it.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)) val h = it.measuredHeight val height = if (h > 0) h else 0 val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) super.onMeasure(widthMeasureSpec, newHeightMeasureSpec) return } super.onMeasure(widthMeasureSpec, heightMeasureSpec) } fun measureCurrentView(currentView: View?) { this.currentView = currentView requestLayout() } } 

In custom PagerAdapter reassignment setPrimaryItem

 override fun setPrimaryItem(container: ViewGroup, position: Int, 'object': Any) { super.setPrimaryItem(container, position, 'object') if (position != currentPosition) { val fragment = 'object' as Fragment val pager = container as CustomViewPager if (fragment.view != null) { currentPosition = position pager.measureCurrentView(fragment.view) } } } 

You may also need to set the isNestedScrollingEnabled your RecyclerView to false

 recyclerView.isNestedScrollingEnabled = false 
0
source

Source: https://habr.com/ru/post/1233226/


All Articles