【问题标题】:Set initial height of parallax image in CollapsingToolbarLayout在 CollapsingToolbarLayout 中设置视差图像的初始高度
【发布时间】:2015-11-03 21:48:30
【问题描述】:

我有一个 Android 活动,它使用 CoordinatorLayout 内的 CollapsingToolbarLayout 来实现以图像作为背景/横幅的滚动/折叠工具栏。

图片是从互联网上加载的,我事先不知道它的大小。

我希望工具栏最初具有一定的高度 (160dp),但如果图像大于此高度,我希望允许用户进一步向下滚动以显示图像的其余部分。但是,这绝不应该自动发生。初始状态为 160dp 高度,但用户应该能够向下滚动以进一步增加图像的高度。

我似乎找不到 height / minheight 的正确组合来实现这一点。这甚至可能吗?

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/toolbar_text">

    <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinatorLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    app:contentScrim="?attr/colorPrimary"
                    android:fitsSystemWindows="true">

                <!-- The background banner -->
                <ImageView
                        android:id="@+id/imgBanner"
                        android:layout_width="match_parent"
                        android:layout_height="160dp"
                        android:scaleType="centerCrop"
                        android:fitsSystemWindows="true"
                        app:layout_collapseMode="parallax"/>

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

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

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

        <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill_vertical"
                android:clipToPadding="false"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" >

                ...
        </android.support.v4.widget.NestedScrollView>

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

</LinearLayout>

横幅图像的高度设置为 160dp,它控制工具栏最初的大小,但显然这样我不能将其扩展到 160dp 之外,因为那是视图的高度。

我尝试将 160dp 高度设置为 CollapsingToolbarLayoutAppBarLayout 但没有任何帮助,它始终是最大 160dp 高度,我只能向上滚动(较小的图像)而不是向下滚动,即使图像很大更大,ImageView 设置为wrap_content

以下是我想要达到的效果图,以防不清楚:

【问题讨论】:

  • 我正试图拥有完全相同的东西。你成功了吗?

标签: android parallax android-collapsingtoolbarlayout


【解决方案1】:

这是实现此目的的一种 hacky 方法,我不确定是否有更好的方法,但很想听听。查看ControllableAppBarLayout

将该代码放在您的项目中并替换:

    android.support.design.widget.AppBarLayout

与:

   ControllableAppBarLayout

将 ControllableAppBarLayout 的 android:layout_height 设置为工具栏的最大尺寸,即“向下滚动”图片中所需的尺寸。

然后,在您的代码中的 onCreate() 方法中,您可以访问 ControllableAppBarLayout:

appBarLayout = (ControllableAppBarLayout) findViewById(R.id.appBarLayout);
appBarLayout.collapseToolbar();

appBarLayout.collapseToolbar() 将完全关闭工具栏。但是,您可以在 ControllableAppBarLayout 中修改此方法:

private void performCollapsingWithoutAnimation() {
    if (mParent.get() != null) {
        mBehavior.onNestedPreScroll(mParent.get(), this, null, 0, getHeight(), new int[]{0, 0});
    }
}

具体来说,将 getHeight() 更改为 dp 中的数字,表示工具栏图像的最大高度与您最初要显示的高度之间的差异。也就是说,如上所示,“初始状态”和“向下滚动”之间的区别。

这个 hack 应该可以完成你想要的。

【讨论】:

  • 很好,工作正常,还没有发现任何副作用。谢谢!
  • 我很高兴听到它奏效了,太好了。不客气。
  • 也许有一个问题:有没有办法确保“完全展开”的高度永远不会超过设备上的可用屏幕空间?例如,如果我将高度设置为 420dp,在我的设备上,底部仍然有一些空间。但是,如果某人的设备更短,他们可以将内容一直滚动到屏幕外(图像占据整个屏幕),然后他们就无法再向上滚动了。当您将设备切换到横向模式时也是如此。我尝试对 ControllableAppBarLayout 应用底部边距,但这似乎没有任何作用。
  • 上述代码不起作用。进一步向下滚动时仍不影响视差图像
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2019-11-01
相关资源
最近更新 更多