【问题标题】:Tab Layout only collapsing on fling, not on slow scroll选项卡布局仅在翻动时折叠,而不是在慢速滚动时折叠
【发布时间】:2018-11-08 06:07:46
【问题描述】:

对于下面的布局,当我在 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(滚动的视图):

<?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>

【问题讨论】:

  • 你应该在TabLayout上定义app:layout_collapseMode
  • @RowlandMtetezi 如果我在 tabLayout 上设置了 collapseMode,它根本不会折叠
  • 你试过删除 android:fitsSystemWindows="true" 吗?
  • @ParaskevasNtsounos 是的,我有
  • @Onik 发生了很多自定义事件。简而言之,它是活动(具有工具栏)-> 父片段(具有选项卡布局和视图分页器)-> 子片段(x2)。在子片段中,我们有一个滑动刷新视图和一个回收器视图

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


【解决方案1】:

旧答案

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

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


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

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

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);
        }
    }

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

新答案

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

【讨论】:

  • recyclerView 是 SwipeRefreshLayout 的直接子级,但刷新布局不是该布局文件中的顶级视图。它是 frameLayout > FrameLayout > 刷新布局 > recyclerview
  • CoordinatorLayout 的直接子级应该实现 NestedScrollingChild 接口。因此,尝试将 FrameLayout 包装在 NestedScrollView 中。
  • 我已将 xml 添加到 OP。立即进行建议的更改
  • 仍在努力。必须让 recyclerview 在滚动视图中运行
  • 我将直接子元素设置为nestedscrollview,它立即崩溃了。这些问题没有解决,因为我需要让它适用于我的用例,但是问题已经确定并且解决方案很明确。请更改您的答案,我会给它检查。谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 2017-01-14
相关资源
最近更新 更多