3

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.

Florian Hansen
  • 746
  • 7
  • 19

3 Answers3

1

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());
            }
        }
    }
varotariya vajsi
  • 3,965
  • 37
  • 39
0

CollapsingToolbar layout scroll set this propety

app:layout_scrollFlags="scroll|exitUntilCollapsed"
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
0

make changes like this , this will work

    <CoordinatorLayout>
  <AppBar> 
    <CollapsingToolbar             
     app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"/>
  </AppBar>
    <ViewPager
          app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</CoordinatorLayout>

and in your fragments layout take parent as NestedScrollView and it'll work.

Muhib Pirani
  • 765
  • 1
  • 6
  • 15
  • Still does not work :/ My VP now hast the behaviour applied. My Fragment looks like The inner List is still the same. is this also supposed to be a root? – Florian Hansen May 19 '17 at 12:17