android - View Pager 的 fragment 隐藏在 Tab Bar 后面

标签 android android-layout android-fragments tabs android-recyclerview

我的 android 项目中的标签栏和 ViewPager 有问题。该应用程序的作用它有一个 Activity ,该 Activity 承载一个选项卡布局,然后有 2 个 fragment 代表每个选项卡。

当 Activity 打开时,它会发送到 API 以获取一些数据,并将数据放入每个 fragment 中的 Recycler View 和 Card 布局的数据适配器中。

回收器 View 将包含 3 个项目,但仅显示 2 个,因为第一个隐藏在工具栏和/或选项卡栏下,如下面的屏幕截图所示。

下面是我的activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.BoardiesITSolution.CritiMonApp.AppsActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <include layout="@layout/toolbar" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill" />
    </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_behaviour="@string/appbar_scrolling_view_behaviour" />-->
</android.support.design.widget.CoordinatorLayout>

下面是 fragment 的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <view
        android:id="@+id/recycler_view"
        class="android.support.v7.widget.RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />
</LinearLayout>

下面是卡片布局的布局

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="8dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:elevation="5dp">
        <TextView
            android:id="@+id/txtApplicationName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:maxLines="3"
            android:padding="8dp"
            android:textColor="#222"
            android:textSize="15dp"
            android:layout_centerInParent="true"/>

    </RelativeLayout>
</android.support.v7.widget.CardView>

下面是上面提到的显示问题的屏幕截图。我对一些文本进行了像素化处理,但它应该是我的意思,应该有 3 个项目,但第一个项目隐藏在选项卡栏下方。

Screenshot showing the issue

最佳答案

编辑:正如下面@smeet 和@hardik 所建议的那样,添加滚动行为 app:layout_behavior="@string/appbar_scrolling_view_behavior" 应该可以解决问题,同时保留滚动行为。 只有当 View 是协调器布局的直接 subview 时,滚动行为才有效。


旧答案

只需将您的 appbar 布局和 viewpager 包装在垂直的 LinearLayout 中

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.BoardiesITSolution.CritiMonApp.AppsActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        //appbar layout

        //viewpager    


    </LinearLayout>

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

根据文档,CoordinatorLayout 是一个功能强大的 FrameLayout。因此,您可以期待典型的“将 View 置于其他 View 之上”的 FrameLayout 行为。

关于android - View Pager 的 fragment 隐藏在 Tab Bar 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319928/

相关文章:

android - 在 Fragment 之间共享数据的更好方法是什么?

javascript - 安装 Android 6.0.1 后,应用程序在调试时崩溃

java - db4o获取特定对象android java

ANDROID:线性>相对>LinearLayout>表格>SeekBar太小

android - 子背景属性覆盖父圆角背景

java - Recycleview 没有出现在 Fragment 中

android - 每次我将 View 从 ListView 更改为 GridView 时如何停止加载

java - 如何在 android 中创建可重用的 AsyncTasks?

java - 使用 Java/Android 测量下载速度

滚动时 Android ListView 生涩