How to fix the position of a view inside a ViewPager and CoordinatorLayout

9.6k views Asked by At

I have an activity with the layout of:

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="56dp"
                android:layout_width="match_parent"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="wrap_content"
                app:tabIndicatorColor="@color/indicator"
                app:tabMode="scrollable"
                android:layout_height="wrap_content" />

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

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

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

The fragment loaded in ViewPager has a layout of:

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp"
            android:scrollbars="vertical" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/download_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:src="@drawable/ic_btn_download" />

</RelativeLayout> 

How can I fix the position (prevent scrolling) of the FloatingActionButton inside the fragment (it scrolls with its parent)?

Note that I couldn't move FloatingActionButton to the activity layout.

2

There are 2 answers

0
Ahmad Shahriari On BEST ANSWER

You should use a FAB in activity layout. .If you need different colors or actions you can define FAB in your fragment and set the color or the action for each fragment.

0
HendraWD On

You should move your floating action button from your view pager to coordinator layout. From there you can set your floating action button position with

android:layout_gravity="end|bottom"

or

app:layout_anchor="@id/yourViewToAnchor"
app:layout_anchorGravity="bottom|right|end"

And don't forget to put your floating action button on the lowermost of your coordinator layout XML to ensure that your floating action button at the top of your layout.