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

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

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

我的问题是部分:“CollapsingToolbarLayout 在用户抬起手指时展开/折叠 我有来自 Material 支持库的标准解决方案(在 fragment 中):

(我删除了不重要的属性):

<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 之后,应用程序不再收到任何消息。我无法在应用栏上调用 setExpanded(true, true)

所以问题是:还有另一种方法可以获取用户从 CollapsingToolbarLayout 中抬起手指的信息吗?

最佳答案

如果我正确理解了您的问题,您是在尝试在用户停止滚动(用户已将手指从屏幕上移开)后完全展开/折叠 CollapsingToolbarLayout 吗? AppBarLayout 提供了一个滚动标志来捕捉内容 [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

关于android - 如何强制 CollapsingToolbarLayour 只有两种状态(展开和折叠)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34213671/

相关文章:

android - 在android相机中上传捕获的图像的问题

android - flatMap 中的 NetworkOnMainThreadException

java - 使用 Eclipse for androidSDK,当我去运行时,我得到 WARNING : Data partition already in use. Changes will not persist!

android - 设计 Spinner、appcompat、material

java - 解决 com.android.support :support-compat and androidx. core:core 之间的依赖冲突

android - 首选项支持库 - SwitchPreference 不起作用

android - 如何在 Android 中创建自定义控件?

css - 是否可以使 md-button 更小?

android - 如何模仿谷歌地图的底页 3 阶段行为?

android - Gradle构建因编译时错误而失败,而Android Studio IDE未显示此类错误