android - 芝士方 block : enterAlways produces wrong layout

标签 android material-design android-support-library android-design-library android-collapsingtoolbarlayout

enterAlways 添加到 Cheesesquare 演示的滚动标志中:

<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|enterAlways">

导致布局错误:

enter image description here

在向下滚动的过程中,标题正确出现,但没有停在正确的位置。滚动会进一步置换部件:背景图像出现在错误的位置,并且由于背景颜色的变化,工具栏变得不可见。 (我还在此处的工具栏中添加了一个 colorPrimary 背景,以使其更加明显,但问题当然不取决于颜色)。这些库是截至今天的最新版本,23.1.0。

是否有任何解决方法,或者我们必须等待它在库中修复?现在,对于任何需要此功能的应用程序来说,它似乎都是一个亮点。

enterAlwaysCollapsed 有效,但提供了不同的功能,这不是解决方法。

最佳答案

我通过修补 AppBarLayout 类源代码解决了这个问题。显然他们不认为人们会这样使用它。或者他们做到了,而我已经离开了。无论如何它对我有用。

您需要对这个方法做一点小改动。寻找 SCROLL_FLAG_EXIT_UNTIL_COLLAPSED

 /**
 * Return the scroll range when scrolling down from a nested pre-scroll.
 */
private int getDownNestedPreScrollRange() {
    if (mDownPreScrollRange != INVALID_SCROLL_RANGE) {
        // If we already have a valid value, return it
        return mDownPreScrollRange;
    }

    int range = 0;
    for (int i = getChildCount() - 1; i >= 0; i--) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int childHeight = child.getMeasuredHeight();
        final int flags = lp.mScrollFlags;

        if ((flags & LayoutParams.FLAG_QUICK_RETURN) == LayoutParams.FLAG_QUICK_RETURN) {
            // First take the margin into account
            range += lp.topMargin + lp.bottomMargin;
            // The view has the quick return flag combination...
            if ((flags & LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED) != 0) {
                // If they're set to enter collapsed, use the minimum height
                range += ViewCompat.getMinimumHeight(child);
                // This is what is missing...
            } else if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) == LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) {
                range += childHeight - ViewCompat.getMinimumHeight(child);
            } else {
                // Else use the full height
                range += childHeight;
            }
        } else if (range > 0) {
            // If we've hit an non-quick return scrollable view, and we've already hit a
            // quick return view, return now
            break;
        }
    }
    return mDownPreScrollRange = range;
}

如果你正在使用,你可能需要减少状态栏的高度 "android:fitsSystemWindows="true"。

希望对您有所帮助。您需要从设计库中复制一些类,以允许所有导入和一些公开的方法。

干杯。

关于android - 芝士方 block : enterAlways produces wrong layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32539710/

相关文章:

android - 膨胀类 android.support.design.widget.NavigationView 时出错

android - 在 AppBarLayout 折叠之前防止 RecyclerView 在 AppBarLayout 下滚动

java - 是否可以将数据发送到服务器而不下载响应?

javascript - 如何在我的组件上使用 "vuetify"转换?

android - 如何在 ConstraintLayout 中设置 View 的最小百分比宽度

android - 回收站 View : UnsupportedOperationException

android - 带有爱马仕反汇编 index.android.bundle 的 React-native 0.61.2

android - 在 Android 中启动时区列表以进行选择/结果

android - 如何使用 Material Dialog 进行两个输入

css - 如何在不破坏任何东西的情况下向 Material 组件添加自定义样式