CollapsingToolbarLayout
:
这里的挑战在于我需要一个自定义的带有gif背景的工具栏,因此我需要将ImageView
作为Toolbar
的背景。您可以在以下XML中查看我的实现:<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="@color/colorAccent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="@color/colorAccent"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<!-- Container which should be scrolled parallax and contains the image gallery -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax">
<RelativeLayout
android:id="@+id/image_layer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/image_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:layout_marginTop="?attr/actionBarSize"/>
<LinearLayout
android:id="@+id/image_indicators"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/defaultPadding"
android:gravity="center"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
<!-- Container which contains the background for the toolbar and the toolbar itself -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
<ImageView
android:id="@+id/toolbar_background"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:scaleType="centerCrop"
android:layout_gravity="top"
android:background="@color/colorPrimaryLight"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<de.views.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FF0000"
android:textStyle="bold"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="center"
android:id="@+id/toolbar_title"
tools:text="Restauranttitel"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<!-- Tablayout -->
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabBackground="@drawable/selected_tab_background"
app:tabIndicatorColor="@android:color/transparent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabMaxWidth="2000dp"/> <!-- we need to set this value to a very big value so that a single tab gets displayed over the full width too -->
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
使用这种实现方式,整个视图都会滚动,既不会将Toolbar
固定在屏幕顶部,也不会将TabLayout
固定在屏幕顶部。
我已经学习了很多教程和stackoverflow的答案,例如:
- https://antonioleiva.com/collapsing-toolbar-layout/
- http://blog.iamsuleiman.com/parallax-scrolling-tabs-design-support-library/
CollapsingToolbarLayout
。我认为我的实现问题在于包含ImageView
和Toolbar
的RelativeLayout
。当删除RelativeLayout
和ImageView
并将Toolbar
的collapseMode设置为'pin'时,一切都按预期工作,并且如果用户滚动,则Toolbar
和Tablayout
都会固定在屏幕顶部。但不幸的是,我需要将ImageView
放在Toolbar
上方,以便能够将GIF加载为Toolbar
背景。
也许你们中的某个人有一个绝妙的想法来解决这个问题。或者您有另一个想法可以实现所需的行为?请告诉我 :)
更新:我已经创建了一个示例项目(https://drive.google.com/open?id=0B1aHkcAaWIA-dHBTZnUyeUt3eTQ),您可以使用它来重现错误的滚动行为。