RecyclerView within ViewPager within NestedScrollView within CoordinatorLayout not scrollable

Viewed 799

I got the following structure:

<CoordinatorLayout>
  <AppBar> 
    <CollapsingToolbar             
     app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"/>
  </AppBar>
  <NestedScrollView
        android:layout_height="wrap_content"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <ViewPager />
  </NestedScrollView>
</CoordinatorLayout>

The fragment inside the ViewPager looks like:

<LinearLayout
  android:layout_height="wrap_content">
  <RecyclerView
      android:layout_height="wrap_content" />
</LinearLayout>

Inside this Fragment's RecyclerView, I got

<RelativeLayout
  android:layout_height="wrap_content">
  <...>
  <RecyclerView
    ....
    android:layout_height="wrap_content" />
</RelativeLayout>

Now the problem is, that the Toolbar gets collapsed correctly when I scroll the List below. But when the Toolbar is fully collapsed, no more scrolling is happening and I can't figure out how to achieve that.

For every RecyclerView, I am using a LinearLayoutManager with AutoMeasureEnabled set to true. Furthermore, HasFixedSize-Flags are set to false.

Any ideas?

//Edit: the content is not fixed; it gets passed in via reactiveUI.

3 Answers

You have to make view pager with dynamic height, I use below custom view pager inside NestedScrollView,

public class CustomPager extends ViewPager {

private View mCurrentView;

public CustomPager(Context context) {
    super(context);
}

public CustomPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mCurrentView == null) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }
    int height = 0;
    mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int h = mCurrentView.getMeasuredHeight();
    if (h > height) height = h;
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void measureCurrentView(View currentView) {
    mCurrentView = currentView;
    requestLayout();
}

public int measureFragment(View view) {
    if (view == null)
        return 0;

    view.measure(0, 0);
    return view.getMeasuredHeight();
}
}

and Override below method into your FragmentStatePagerAdapter or FragmentPagerAdapter

private int mCurrentPosition = -1;
@Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (position != mCurrentPosition) {
            Fragment fragment = (Fragment) object;
            CustomPager pager = (CustomPager) container;
            if (fragment != null && fragment.getView() != null) {
                mCurrentPosition = position;
                pager.measureCurrentView(fragment.getView());
            }
        }
    }
Related