我正在使用下一个代码制作一个显示图像的collpasing工具栏,然后在用户向下滚动时折叠工具栏:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="240dp"
android:fitsSystemWindows="true"
app:contentScrim="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<ImageView
android:id="@+id/iv_cover"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:contentDescription="@string/app_name"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:titleTextAppearance="@style/Toolbar.TitleText"
app:subtitleTextAppearance="@style/Toolbar.SubtitleText" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_commerce" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_promotions_white_24dp"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" />
问题是折叠工具栏无法顺畅滚动。当工具栏完全折叠时,其余的布局滚动得很好,但如果工具栏没有完全折叠,则滚动运动不自然。 “content_commerce.xml”布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Presentation-->
<include
layout="@layout/content_commerce_presentation"/>
<!--Description-->
<include
layout="@layout/content_commerce_description"/>
<!--Entrys - categories-->
<include
layout="@layout/content_commerce_entries_categories"/>
<!--Payment Methods-->
<include
layout="@layout/content_commerce_payment_methods"/>
<!--Additional Information-->
<include
layout="@layout/content_commerce_additional_information"/>
<!--Contact Information-->
<include
layout="@layout/content_commerce_contact"/>
</LinearLayout>
</LinearLayout>
其余布局不包含任何可滚动视图,只包含LinearLayouts,TextViews和ImageViews。