AlexF11 AlexF11 - 5 months ago 28
Android Question

How can I display viewpager below a layout?

I generated a swipe activity. The activity needs to have it's own layout with a view pager underneath

<?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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.mferreira.crm.recordSwipeActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
>

</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_item_view" />

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

</android.support.design.widget.CoordinatorLayout>


My only issue is at runtime the fragments are displayed inline with the layout itself at the top of the page rather than underneath it

Answer

It seems like your issue is because the top-level CoordinatorLayout behaves like a FrameLayout, not a LinearLayout. If you want your @layout/content_item_view and your ViewPager not to be inline with each other, wrap them in a LinearLayout:

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/content_item_view" />

    <android.support.v4.view.ViewPager android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>