【问题标题】:Cheesesquare: enterAlways produces wrong layoutCheesesquare:enterAlways 产生错误的布局
【发布时间】:2015-12-08 23:22:09
【问题描述】:

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

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

导致布局错误:

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

是否有任何解决方法,或者我们必须等待它在库中修复?目前,对于任何需要此功能的应用来说,它似乎都是一个不错的选择。

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

【问题讨论】:

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


    【解决方案1】:

    我通过对 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 问题队列中吗? Chris Banes 可能会欢迎它并将其包含在下一个版本中......
    • 由于类依赖关系,必须修复 AppBarLayout 代码和 CollapsingToolbarLayout 才能使其正常工作。对于这样的问题,工作量太大。希望他们尽快解决。
    • 我不确定他们会不会,除非您发出信号并在那里提供解决方案。 :-)
    • 请原谅我的无知,但我该怎么做呢?
    • 实际上,我自己从来没有这样做过,但是很多时候你会像这样被发送到问题队列:code.google.com/p/android/issues/detail?id=190226。所以,看来你只是提交一个新的issue(不需要单独注册,在google下就可以了)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2012-11-16
    • 2012-04-01
    相关资源
    最近更新 更多