gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:20:08 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com