【问题标题】:Why content is dragging after removing scrollFlags `scroll`?为什么在删除 scrollFlags `scroll` 后拖动内容?
【发布时间】:2019-04-27 23:01:49
【问题描述】:

如果在 Toolbar 中的 app:layout_scrollFlagsscroll 处删除,则内容将移至顶部。看截图

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <!--region Content-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <!--region EmptyView-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="16dp">

                <TextView
                    android:id="@+id/titleView"
                    fontPath="@string/font_semibold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:gravity="center"
                    android:padding="8dp"
                    android:textColor="#9b9b9b"
                    android:textSize="16sp"
                    tools:ignore="MissingPrefix"
                    tools:text="@string/error_view_internet_connection_title"/>

                <TextView
                    android:id="@+id/messageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="32dp"
                    android:gravity="center"
                    android:paddingStart="32dp"
                    android:paddingEnd="32dp"
                    android:textColor="#9b9b9b"
                    android:textSize="14sp"
                    tools:ignore="MissingPrefix"
                    tools:text="@string/error_view_internet_connection_message"
                    />

            </LinearLayout>
            <!--endregion-->

            <!--region List-->
            <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clipToPadding="false"
                    android:scrollbarSize="2dp"
                    android:scrollbars="vertical"
                    android:visibility="gone"
                    tools:visibility="visible"/>

            </android.support.v4.widget.SwipeRefreshLayout>
            <!--endregion-->

        </LinearLayout>
        <!--endregion-->

        <!--region Toolbar-->
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@android:color/transparent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="0dp"
            app:layout_behavior="@string/appbar_layout_behavior"
            app:layout_collapseMode="pin">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                style="?android:attr/toolbarStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#636363"
                android:minHeight="?android:attr/actionBarSize"
                app:elevation="0dp"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                app:popupTheme="@style/ToolbarStyle"
                app:theme="@style/ToolbarStyle"
                tools:ignore="NewApi"/>

        </android.support.design.widget.AppBarLayout>
        <!--endregion-->

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

    <!--region BottomNavigation-->
    <com.aurelhubert.ahbottomnavigation.AHBottomNavigation
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:accentColor="#111232"
        app:inactiveColor="#111232"
        app:titleState="always_hide"/>
    <!--endregion-->

</LinearLayout>

我通过 func 删除了标志,然后在 UI 中它在晃动:

    public void setToolbarCollapsible(boolean collapsible) {
        int defaultFlags =
                AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP
                | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
                | AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL;
        int none = 0;
//        //remove from toolbar
        Toolbar toolbar = getToolbar();
        if (toolbar == null) return;
        AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
        toolbarLayoutParams.setScrollFlags(collapsible ? defaultFlags : none);
        toolbar.setLayoutParams(toolbarLayoutParams);
    }

【问题讨论】:

    标签: xml android-layout android-toolbar android-coordinatorlayout


    【解决方案1】:

    如果在 Toolbar 中的 app:layout_scrollFlagsscroll 处删除,则内容将移至顶部。

    内容正在向上移动,因为包裹TextViewLinearLayout 的高度正在变化。因此,LinearLayout 上指定的 android:gravity="center" 会计算不同的垂直中心。

    以下布局是您的布局的简化视图,用于演示目的。

    <android.support.design.widget.CoordinatorLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <TextView
                android:id="@+id/titleView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/error_view_internet_connection_title"
                android:textSize="16sp" />
    
        </LinearLayout>
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:title="Toolbar" />
    
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    这是此布局的布局检查器视图,显示了设置和不设置滚动标志的LinearLayout 的高度。

    如果没有设置滚动标志,包裹TextViewsLinearLayout 的高度会比Toolbar 的高度小。这是有道理的,因为 LinearLayoutmatch_parent 它必须适合。设置滚动标志后,LinearLayout 可以更高,因为它可以滚动并且不必适应屏幕。事实上,它和屏幕一样高,但在工具栏下方移动。

    您可以快速测试是否是这种情况,但将android:gravity="center" 替换为上边距。

    我通过 func 删除了标志,然后在 UI 中它在晃动

    我想我以前见过这种行为,但我不记得它的原因。由于CoordinatorLayout 是如此动态,它可能是由于一些布局模糊性导致的布局中的某种振荡。然而,这只不过是猜测。如果您稍微清理一下布局并考虑为什么要操纵滚动标志(还有其他方法吗?),您也许可以解决问题。

    除此之外,一个演示抖动问题的小型可运行项目可能对发布很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多