停止CollapsingToolbarLayout的滚动,以便它不会完全折叠。

22

我设置了一个CollapsingToolbarLayout并在其中放置了壁纸。我希望能够停止它完全折叠。

我尝试过minheight和许多其他方法,但无法弄清楚如何做到这一点。

如何让它停止折叠到第二张截图的位置?

加载活动时的视图

Loaded View

期望停止点

Desired Stopping point

当前停止点

Current Stopping Point


9
美丽的版面值得点赞! - Mahm00d
3个回答

15

CollapsingToolbarLayoutToolbar 十分密切相关,因此折叠高度取决于工具栏。

我可以使用以下布局解决您的问题(注意:它适用于普通的 CoordinatorLayout/AppBarLayout 设置,带有 Fab 和一个 NestedScrollViewRecyclerView):

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:statusBarScrim="?attr/colorPrimaryDark"
    app:contentScrim="@android:color/transparent"
    app:titleEnabled="false"
    >
    <!-- There isnt a contentSCrim attribute so the toolbar is transparent after being
         collapsed
         Disabled the title also as you wont be needing it -->

    <ImageView
        android:id="@+id/image_v"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:layout_gravity="center"
        android:scaleType="centerCrop"
        android:src="@drawable/md2"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax"
        tools:ignore="ContentDescription"
        />
        <!-- Normal Imageview. Nothing interesting -->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="168dp"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />
        <!-- The toolbar is styled normally. However we disable the title also in code.
        Toolbar height is the main component that determines the collapsed height -->

    <TextView
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimaryDark"
        android:paddingLeft="72dp"
        android:paddingRight="0dp"
        android:paddingBottom="24dp"
        android:paddingTop="24dp"
        android:textColor="@android:color/white"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        />
        <!-- The title textView -->

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

相关的活动看起来像这样:

    ...
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // Disable toolbar title
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    ...

这里是交互视频:

输入图像描述


14

我曾经面临过相同的问题。

一开始,我按照之前的回答所述,只是设置了Toolbar的高度,这样就行了。

但是这又导致了另一个问题。Toolbar视图会吞噬触摸事件,所以我的折叠视图(即MapView)在与Toolbar重叠的部分不会接收到任何触摸事件。

最后,我的解决方案是将Toolbar从CollapsingToolbarLayout中移除。在我的情况下,这没关系,因为我只使用它来限制折叠。然后像这样在onCreateView中设置最小折叠高度:

CollapsingToolbarLayout layoutCollapsing = (CollapsingToolbarLayout) rootView.findViewById(R.id.layoutCollapsing);
layoutCollapsing.setMinimumHeight(120);

2
不错的想法!你可以在布局文件中设置最小高度,而不是在代码中。 - Mehrzad Chehraz
如果您在 CollapsingToolbarLayout 中放置了一个工具栏(Toolbar),您也可以为其添加边距。 :) - Milack27

1

只需将所需的停止高度添加到工具栏中,并为CollapsingToolbarLayout设置app:contentScrim="#00000000"

  <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="#00000000"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <ImageView
            android:id="@+id/ImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/image"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="100dp"
        /> <!-- set desired stop-height as height -->

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

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - JHH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接