16
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】TabLayout + ViewPager + RecyclerView の上にViewを置いてスクロールでTabLayoutは上部に固定したい

Last updated at Posted at 2017-10-13

どんなレイアウトなのか?(メモ)

こういうレイアウトがあって、下にスクロールした時にTabはToolbar or ActionBarの下にピタッと止めたいってデザイナーさんに言われた時に組んでみたレイアウト。

スクリーンショット 2017-10-13 10.13.21.pngスクリーンショット 2017-10-13 10.14.32.png

◯◯◯Activityのレイアウト

activity.xml
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <!-- 上記の図にあるViewの部分 -->
        <include layout="@layout/header_view" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

上部に表示させたいViewのレイアウト

header_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

<!-- --- -->

</LinearLayout>

ViewPager内の◯◯◯Fragmentのレイアウト

fragment.xml
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
16
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?