Using 本教程 http://blog.grafixartist.com/toolbar-animation-with-android-design-support-library/实现灵活空间模式(带有折叠工具栏的模式)。
我正在尝试实现类似的效果棒棒糖联系人活动,在进入活动开始时,视图只是图像标题的一部分:
然后,用户可以向下滚动图像下方的布局以显示更多内容,直到达到最大值:
在我的应用程序中,我无法使其正常工作。
发生的情况是,当进入活动时,图像标题出现在它的位置最大大小,AppBarLayout 的大小,就像上面的布局一样,与 Lollipop Contacts 活动不同,它仅显示图像的一部分。
这是设置 AppBarLayout 高度的代码(我希望屏幕宽度为最大高度):
int widthPx = getResources().getDisplayMetrics().widthPixels;
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
appbar.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, widthPx));
这是设置 RecyclerView 的代码。尝试使用scrollToPosition,认为它会提升RecyclerView的视图,但它根本没有效果:
mRecyclerView = (RecyclerView) findViewById(R.id.activity_profile_bottom_recyclerview);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
if(mAdapter == null){
mAdapter = new ProfileAdapter(this, user, inEditMode);
mRecyclerView.setAdapter(mAdapter);
}
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1); // itemCount is 4
这是布局 xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="0dp" // set programatically
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/header"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_profile_bottom_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<include layout="@layout/navigation_view"/>
</android.support.v4.widget.DrawerLayout>
注意:如果我手动向下滚动,RecyclerView 会向下滚动并显示更多图像,它无法通过代码工作。
我认为scrollToPosition 不是解决方案,有人有任何想法吗?
考虑使用 EnterAlwaysCollapsed 标志也许是这里提到 http://android-developers.blogspot.co.il/2015/05/android-design-support-library.html在 CoordinatorLayout 和 Appbar 部分中使用 minHeight:
EnterAlwaysCollapsed:当您的视图声明了 minHeight 并且您
使用此标志,您的视图将仅以其最小高度进入(即
‘collapsed’),只有在滚动时才重新展开到其完整高度
视野已经达到顶峰。
因此,我在工具栏上设置了scroll|enterAlwaysCollapsed 标志,并在RecyclerView 中设置了minHeight,但这不起作用。然后我尝试将 minHeight 移动到其他布局,例如 AppBarLayout,但没有任何效果。有时它只是缩小图像而没有整个视图。