视图不在ViewPager中的片段中心和工具栏隐藏的RecyclerView

时间:2016-03-29 23:14:28

标签: android xml android-fragments android-viewpager

我有一个与TabLayout集成的ViewPager,它显示4个片段(4个标签)。每个片段都是RecyclerViews,显示项目列表。每个列表中的项目数由给定日期生成,但有时不会显示任何项目。当发生这种情况时,我切换到另一个片段,该片段显示在TextView“No Games Scheduled”中。

问题是文本不在屏幕中心。它位于中心但朝向底部。我相信它与我在XML中的ViewPager属性有关,我将其设置为app:layout_behavior="@string/appbar_scrolling_view_behavior"(注意:此行不会显示在已发布的代码中。我稍后会解释。)

当我点击ViewPager的xml代码时,在预览屏幕中(在Android Studio中)点亮的蓝色部分会扩展到屏幕下方。所以我知道这与此有关。当我删除该行代码时,蓝色部分适合屏幕。作为测试,我将TextView显示“No Games Scheduled”layout_gravity设置为center|bottom,当我运行应用程序时,文本未显示在屏幕上。所以我知道它被ViewPager中的那行代码隐藏了。但我需要它,因为我希望工具栏在向下滚动时隐藏,并在我向上滚动时显示。

然后我尝试从ViewPager中删除app:layout_behavior="@string/appbar_scrolling_view_behavior"并将其放在每个片段中。它工作但现在每个片段的顶部都被工具栏和像这样的选项卡覆盖(不是我的程序,因为我不允许显示UI,但它显示我的确切问题):

enter image description here

我该如何解决这个问题?谢谢你的帮助!

main_activity.xml:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/nav_drawer"
    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:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

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

            <android.support.design.widget.TabLayout
                android:id="@+id/main_activity_tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/light_black"/>

            <LinearLayout
                android:id="@+id/league_and_date_title_view"
                android:layout_width="match_parent"
                android:layout_height="34dp"
                android:background="@color/gray"
                android:orientation="horizontal"
                android:paddingEnd="0dp"
                android:paddingLeft="4dp"
                android:paddingRight="0dp"
                android:paddingStart="4dp"
                android:weightSum="1">

                <TextView
                    android:id="@+id/league_title_header"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center|start"
                    android:paddingEnd="0dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="0dp"
                    android:paddingStart="5dp"
                    android:textSize="14sp"
                    android:textColor="@color/green"
                    tools:text="League"/>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center|end"
                    android:orientation="horizontal">

                    <ImageButton
                        android:id="@+id/previous_date_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="?attr/selectableItemBackgroundBorderless"
                        android:contentDescription="@string/prev_date_button"
                        android:src="@drawable/previous_arrow"/>

                    <TextView
                        android:id="@+id/display_date_text_view"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="?attr/selectableItemBackgroundBorderless"
                        android:gravity="center"
                        android:textColor="@color/outnix_orange"
                        android:textSize="14sp"
                        tools:text="Date Goes Here"/>

                    <ImageButton
                        android:id="@+id/next_date_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="?attr/selectableItemBackgroundBorderless"
                        android:contentDescription="@string/next_date_button"
                        android:src="@drawable/next_arrow"/>
                </LinearLayout>
            </LinearLayout>

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

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <ProgressBar
                android:id="@+id/loading_circle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:indeterminate="true"/>
        </RelativeLayout>

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

    <fragment
        android:id="@+id/fragment_nav_drawer"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_nav_drawer"
        tools:layout="@layout/fragment_nav_drawer"/>

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

片段的布局之一:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh"
    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.v7.widget.RecyclerView
        android:id="@+id/all_games_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/light_black"
        android:paddingBottom="8dp"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>

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

片段我在RecyclerView中没有任何项目时切换到:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/no_games"
        android:textColor="@color/white"
        android:textSize="18sp"/>

</RelativeLayout>

0 个答案:

没有答案