android - TabLayout 上方的页眉布局

标签 android android-layout android-fragments android-activity android-xml

我正在尝试做这样的事情:

http://i.imgur.com/NwewaBx.png

我目前有上面的布局,但没有实现 header 。我只需要弄清楚如何添加 header 。

这是我想放在选项卡上方的页眉布局:

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

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal"
        android:background="@color/green">

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

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@drawable/ic_image"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:layout_gravity="center_vertical" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hey!"
                    android:textColor="@color/white"
                    android:textSize="24sp"
                    android:layout_gravity="center_vertical" />

            </LinearLayout>

    </LinearLayout>

</LinearLayout>

我为选项卡下方的内容使用了 RecyclerView。我为 Activity 使用了 3 个不同的 Fragments

这是我的 Activity :

<android.support.design.widget.CoordinatorLayout
    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="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:background="@color/blue"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextAppearance="@style/TabLayout"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/white"
            />

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

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

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

这是我的一个 fragment :

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

如何在 TabLayout 上方添加标题并保持正常的滚动行为?

最佳答案

你只需要一个 RecyclerView 就可以做到这一点。 将 header 和 tabLayout 组合成一个 Header View ,然后添加到您的 RecyclerView。

检查 https://github.com/u3breeze/android-RecyclerView-WithHeaderAndFooter .我做了 RecyclerView.Adapter 的扩展。轻松向 RecyclerView 添加页眉和页脚。

关于android - TabLayout 上方的页眉布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32544616/

相关文章:

android - 当用户点击回来时,Admob 插页式广告仍然打开

java - Appium - 通过java代码在Android设备上滑动无法工作

android-layout - 为什么在使用 Kotlin 中的后退按钮导航到另一个片段并返回后片段中的 EditText 显示相同的值?

android - 如何修复我的布局位置错误?

android - 单击整个布局上的监听器

android - 如何动态创建 fragment 寻呼机适配器

java - FirebaseRecyclerAdapter 抛出空对象

Android 3 调用 WCF REST

Android getMeasuredHeight 返回错误值!

android - 我的应用程序中显示的空 map