如何强制 CollapsingToolbarLayout 只有两种状态(展开和折叠)?

How to force only two state (expanded and collapsed) of CollapsingToolbarLayour?

我想要 CollapsingToolbarLayout(在 AppBarLayout 内),当用户抬起手指 时 expands/collapses。当用户在屏幕上按住并移动手指时,CollapsingToolbarLayout 应该同时改变大小(标准行为)。

我的问题是部分:“CollapsingToolbarLayout expands/collapses 当用户抬起手指时 我有来自 material 支持库的标准解决方案( 在片段中 ):

(我去掉了不重要的属性):

<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>

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

    <include layout="@layout/content_scrolling" />

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

我尝试这样处理 onTouch 事件:

findViewById(R.id.app_bar).setOnTouchListener(new View.OnTouchListener() {

    int counter = 0;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                counter++;
                Log.d("tag toolbar_layout", "=== ACTION_DOWN "+ counter);
                break;
            case MotionEvent.ACTION_CANCEL:
                counter--;
                Log.d("tag toolbar_layout", " == ACTION_UP " +counter);
                break;
            case MotionEvent.ACTION_CANCEL:
                counter--;
                Log.d("tag toolbar_layout", " == ACTION_CANCEL "+ counter);

            break;
        default:
            Log.d("tag toolbar_layout", "onTouch "+ event.getAction());
        }

        return true;
    }

但我几乎收到 ACTION_CANCEL 并且非常罕见 ACTION_UP 并且 ACTION_CANCEL 太早了 当应用程序收到该事件时我仍然将手指放在屏幕上。在 ACTION_CANCEL 之后,该应用甚至不再收到任何消息。我无法在 appbar 上调用 setExpanded(true, true)

所以问题是:还有另一种方法可以获取用户起床手指形状的信息 CollapsingToolbarLayout ?

如果我对您的问题的理解正确,您是在尝试在用户停止滚动(用户已将手指从屏幕上移开)后完全 expand/collapse CollapsingToolbarLayoutAppBarLayout 提供滚动标志来捕捉内容 [1] 以提供此效果:

public static final int SCROLL_FLAG_SNAP

Upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to it's closest edge. For example, if the view only has it's bottom 25% displayed, it will be scrolled off screen completely. Conversely, if it's bottom 75% is visible then it will be scrolled fully into view.

[1]http://developer.android.com/reference/android/support/design/widget/AppBarLayout.LayoutParams.html#SCROLL_FLAG_SNAP