在ViewPager中使用的Fragment内部嵌套NestedScrollView时,CoordinatorLayout无法正常工作。

9

我有一个包含下一个布局的 Activity

Activity 布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/issue_browse_app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/app_default_color"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/issue_browse_toolbar"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

    <android.support.design.widget.TabLayout
        android:id="@+id/issue_browse_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

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

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

实例化后的ViewPager有2个片段

第一个可见片段的布局

    <?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/issue_browse_view_switcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <FrameLayout
        android:id="@+id/issue_browse_status_buttons_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/issue_status_browse_buttons"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >

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

                <LinearLayout
                    android:id="@+id/issue_browse_fragment_view_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="start" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginTop="1000dp"
                    android:text="end" />

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </FrameLayout>

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

        <TextView
            android:id="@+id/issue_browse_load_error_text"
            style="@style/list_view_empty_view_style"
            android:drawableTop="@drawable/error" />

        <TextView
            android:id="@+id/issue_browse_refresh_after_error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/content_offset_small"
            android:textColor="#c0bebe" />
    </LinearLayout>
</ViewSwitcher>

你可以看到,这里有一个带有app:layout_behavior="@string/appbar_scrolling_view_behavior"的NestedScrollView。

而且不管NestedScrollView是否有这个标签,Coordinator都不会隐藏Toolbar。

这段代码中出了什么问题?


2
你有得到任何解决方案吗? - Pratik Butani
1个回答

0

您没有使用 "CollapsingToolbarLayout"

文档说明:

CollapsingToolbarLayout 是 Toolbar 的包装器,实现了可折叠的应用栏。它的设计目的是作为 AppBarLayout 的直接子视图来使用。

为了帮助您更好地理解,请参考以下示例。

 <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        style="@style/ToolBarStyle"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接