java - 如何在android中实现自定义可折叠工具栏?

标签 java android android-layout material-design android-collapsingtoolbarlayout

使用 this tutorial 实现灵活空间模式(带有折叠工具栏的模式)。

我正在尝试达到与 类似的效果。 Lollipop 联系人 Activity ,在开始时进入 Activity , View 只是图像标题的一部分:

enter image description here

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

enter image description here

在我的应用程序中,我无法让它工作。

发生的情况是,当进入 Activity 时,图像标题显示在它的 最大尺寸,AppBarLayout 的尺寸,和上面的布局一样,和 Lollipop Contacts Activity 中的不同 ,它只显示图像的一部分。

这是设置 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的 View ,但它根本没有效果:
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 标志可能为 mentioned here在带有 minHeight 的 CoordinatorLayout 和 Appbar 部分中:

enterAlwaysCollapsed: When your view has declared a minHeight and you use this flag, your View will only enter at its minimum height (i.e., ‘collapsed’), only re-expanding to its full height when the scrolling view has reached its top.



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

最佳答案

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

但请记住,此方法依赖于此布局,它是 CoordinatorLayout 的直接子级。 .

您可以阅读this了解更多信息。

如果您想动画到自定义偏移,请尝试使用 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();
}

关于java - 如何在android中实现自定义可折叠工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32260727/

相关文章:

java - 如何调整 JavaFX 图像的大小?

java - 如何有效地搜索 HashMap 中所有值的子字符串?

java - 为什么这个 Clojure 程序这么慢?如何让它跑得快?

android - viewModelScope 默认为 MainThread 的原因是什么?

android - 如何为动态创建的 View 设置 OnTouchListener?

android - 在 Android 中以编程方式创建包含具有自定义样式的 MaterialButton 的 MaterialButtonToggleGroup

java - Java 中的 ConcurrentHashMap 锁定机制用于 computeIfPresent

android - 尝试使用SimpleAdapter从ListView检索 View 内容时,应用程序崩溃

android - 在android中查询 "dumpsys meminfo"

java - 让 linearLayout 从屏幕上消失?