user2240193
user2240193

Reputation: 21

Resize viewpager in scrollview

I have a viewpager hosted in a scrollview. The viewpager hosts fragments with variable height linearlayouts. If I set the layout_width to wrap_content,I find that on some pages the viewpager holds are skewered. Ideally I would like the viewpager to resize dynamically

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />


    <Scrollview
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fillViewport="true"
        android:scrollbars="none">


        <LinearLayout
            android:id="@+id/container_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <include
                android:id="@+id/details_playWindow"
                layout="@layout/details_play_window" />

            <include
                android:id="@+id/details_topPanel"
                layout="@layout/details_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <include
                android:id="@+id/details_body"
                layout="@layout/details_header_body"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


            <include
                android:id="@+id/relatedMovies"
                layout="@layout/layout_stiff"
                android:layout_width="match_parent"
                android:layout_height="250dp" />


            <LinearLayout
                android:id="@+id/tv_show_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <include layout="@layout/view_series_header" />
                </LinearLayout>

                <!--Setting the height 2000dp is silly but for the mean time -->
                <android.support.v4.view.ViewPager
                    android:id="@+id/download_pager"
                    android:layout_width="match_parent"
                    android:layout_height="1800dp" />


            </LinearLayout>
        </LinearLayout>

    </Scrollview>


</RelativeLayout>

Upvotes: 0

Views: 720

Answers (1)

mdzeko
mdzeko

Reputation: 932

I am not sure you can do that using xml layouts only, but here is what I used to create flexibile layouts.

There is a LinearLayout atribute namely android:layout_weight. It is similar as percentage in CSS. What I would do can be roughly put in these steps:

  1. Set view pagers parent Linear layout to be of vertical orientation and height to wrap content.
  2. Set view pager android:layout_height=0dp and android:layout_weight="1" match parent.

More about layout weight here

Upvotes: 1

Related Questions