android - 选项卡布局仅在滑动时折叠,而不是在慢速滚动时折叠

标签 android android-layout android-tablayout android-coordinatorlayout android-appbarlayout

对于下面的布局,当我在 viewpager 中的页面上向上滚动内容时...如果我将手指放在屏幕上并缓慢向上滚动,选项卡布局不会折叠到工具栏中,但如果我一扔内容起来,确实如此。有什么想法吗?

activity_home.xml

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".newnav.home.HomeActivity">

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".newnav.NavigationActivity">

        <!-- Check whether do we need this surface view still? -->
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="0dp"
            android:layout_height="0dp" />

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

            <com.my.package.view.MyToolBar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="@color/primary_color" />

            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/toolbar" />

            <com.my.package.view.SomeCustomView
                android:id="@+id/breaking_news_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true" />
        </RelativeLayout>

        <com.my.package.navigation.NavigationLayout
            android:id="@+id/navigation_layout"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:fitsSystemWindows="true" />
    </android.support.v4.widget.DrawerLayout>
</layout>

fragment_home.xml(父布局有工具栏):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:fitsSystemWindows="true"
            app:layout_behavior="com.nbc.news.newnav.home.NbcTabBarScrollingBehavior">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                style="@style/HomeTabDesign"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/top_nav_tab_bg"
                app:tabBackground="@color/transparent"
                android:elevation="4dp"
                android:minHeight="@dimen/tab_layout_height"
                app:layout_scrollFlags="scroll|enterAlways"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/home_tab_strip_color"
                app:tabIndicatorHeight="3dp"
                app:tabMaxWidth="0dp"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/home_tab_text_color_selected"
                app:tabTextColor="@color/home_tab_text_color_normal" />

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

        <com.my.package.view.MyViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>
</layout>

section_fragment.xml(滚动的 View ):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.my.package.view.ProgressiveFrameLayout
        android:id="@+id/progressiveLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/divider_white"
        app:contentLayoutId="@id/contentLayout">

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

            <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/sectionRecyclerView"
                    android:layout_width="match_parent"
                    android:descendantFocusability="blocksDescendants"
                    android:layout_height="match_parent"
                    android:background="@android:color/white" />
            </android.support.v4.widget.SwipeRefreshLayout>

            <FrameLayout
                android:id="@+id/adContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center_horizontal" />
        </FrameLayout>

    </com.nbc.news.view.ProgressiveFrameLayout>
</layout>

最佳答案

旧答案

此解决方案在这种情况下不起作用。但它可能对其他人有帮助。

您没有提供必要的代码部分。但是我强烈怀疑您在 SwipeRefreshLayout 和您的 Recyclerview(或 ListView)之间使用中间布局。换句话说,SwipeRefreshLayout 不是 Recyclerview 的直接父级。修复它,它将起作用。


如果你想知道后台到底发生了什么

如果你查看android源代码,你可以找到一个方法canSwipeRefreshChildScrollUp()。它负责决定这个布局的 subview 是否可以向上滚动。如果 subview 是自定义 View ,则需要覆盖它。

public boolean canChildScrollUp() {
        if (mChildScrollUpCallback != null) {
            return mChildScrollUpCallback.canChildScrollUp(this, mTarget);
        }
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mTarget instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mTarget;
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                                .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mTarget, -1);
        }
    }

如果您使用中间 View ,滑动刷新布局将假定它是目标(请参阅方法第 6 行中的 mTarget)并且该 View 的默认实现始终返回 false会导致这个问题。

新答案

CoordinatorLayout 的子项应该实现 NestedScrollingChild 接口(interface)。因此,尝试将 FrameLayout 包装在 NestedScrollView 中

关于android - 选项卡布局仅在滑动时折叠,而不是在慢速滚动时折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50586835/

相关文章:

android - 给定 map 叠加层上的一组点绘制空多边形 (Android 2.1)

android - Gradle 不导入 PagerSlidingTabStrip

android - 对齐单选组内的单选按钮

android - 尝试更改设备配置时找不到 ID XXX 的 View

android - 如何为 10"以下的选项卡自定义应用程序首选项屏幕(双 Pane 布局)

android - Sqlite Xamarin android nuget

android:有没有办法水平翻转布局?

android - 在 android TabLayout 中显示虚拟键盘

安卓 : How to create TabLayout with drawable for count textview?

android - 仅包含图标的表格布局