Android中的灵活空间

Jja*_*ang 66 java android android-layout material-design android-5.0-lollipop

使用本教程实现灵活空间模式(具有折叠工具栏的模式).

我正在尝试实现与Lollipop Contacts活动类似的效果,该活动在进入活动时开始查看图像标题的唯一部分:

在此输入图像描述

然后,用户可以向下滚动图像下方的布局,以便显示更多内容,直到达到最大值:

在此输入图像描述

在我的应用程序中,我无法使其工作.

结果是,当进入活动时,图像标题以其最大尺寸(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));
Run Code Online (Sandbox Code Playgroud)

这是设置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
Run Code Online (Sandbox Code Playgroud)

这是布局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>
Run Code Online (Sandbox Code Playgroud)

注意:如果我手动向下滚动,RecyclerView会关闭并显示更多图像,它将无法通过代码工作.

我认为scrollToPosition不是解决方案,有没有人有任何想法?

考虑使用enterAlwaysCollapsed标志或许在这里提到的CoordinatorLayout和appbar部分与minHeight:

enterAlwaysCollapsed:当您的视图声明了minHeight并使用此标志时,您的视图将仅以其最小高度(即"折叠")进入,仅在滚动视图到达顶部时才重新扩展到其完整高度.

所以,我在我的工具栏中设置了scroll | enterAlwaysCollapsed标志,在我的RecyclerView中设置了minHeight,这不起作用.然后我尝试将minHeight移动到其他布局,例如AppBarLayout,没有任何效果.它只是在没有整个视图的情况下缩小图像.

luk*_*kas 1

提供AppBarComponent了一个名为 的方法.setExpanded(boolean expanded),它允许您扩展您的AppBarComponent.

但请记住,此方法依赖于此布局是CoordinatorLayout.

您可以阅读本文以获取更多信息。

如果您想将动画设置为自定义偏移,请尝试使用该setTopAndBottomOffset(int)方法。

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
final AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
    ValueAnimator valueAnimator = ValueAnimator.ofInt();
    valueAnimator.setInterpolator(new DecelerateInterpolator());
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            behavior.setTopAndBottomOffset((Integer) animation.getAnimatedValue());
            appBar.requestLayout();
        }
    });
    valueAnimator.setIntValues(0, -900);
    valueAnimator.setDuration(400);
    valueAnimator.start();
}
Run Code Online (Sandbox Code Playgroud)