0

I have linear layout with viewpager on top and another vew (button in this example) bottom. Views in the pager have different heights. I would like the pager to have wrap_content height on all pages and button view to fill the rest of the screen.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="New Button"
    android:id="@+id/button3"
    android:layout_weight="1"/>

</LinearLayout>

This doesn't work. If I give layout_weight=1 to both pager and button, they share screen 50:50 no matter of content height. I tried to call requestLayout() in ViewPager.OnPageChangeListener.onPageSelected() but it doesn't help. Also tried this

Community
  • 1
  • 1
Roman Jokl
  • 39
  • 3
  • Found [this](http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content) question, which seems to be half-way to the solution. The only problem is how to implement descendant of ViewPager to measure only current page. I found it quite dificult, averything is private. – Roman Jokl Aug 09 '15 at 05:30
  • I answered on the SO question you provided, checkout https://github.com/rnevet/WCViewPager the implementation should solve your requierments. – Raanan Aug 28 '15 at 12:41

2 Answers2

0

Have you tried setting the layout_weight=1 for the pager and the layout_weight=2 for the button?

thatdude1087
  • 155
  • 1
  • 10
0

This works for me, but I don't consider it perfect.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    View titlestrip = getChildAt(0);
    int h = titlestrip.getMeasuredHeight();

    View fv = ((FragmentPagerAdapter)getAdapter()).getItem(getCurrentItem()).getView();
    if (fv != null) {
        fv.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        h += fv.getMeasuredHeight();
    }

    super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));
}

It relies on the fact that my FragmentPagerAdapter.getItem() doesn't instantiate new fragments but return it from container.

Roman Jokl
  • 39
  • 3