android - 增加滚动时折叠工具栏布局的大小
问题描述
我正在尝试实现一种效果,其中图像视图在collapsingToolbarLayout
开始时位于250dp
并且向上滚动时图像视图变为400dp
. 这是要求的示例:
我现在正在使用以下代码,但只能将视图折叠到工具栏中,不能扩展大小:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/htab_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">
<ImageView
android:id="@+id/profile_avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/placeholder_user"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.3"
android:background="@android:color/black"
android:fitsSystemWindows="true"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/htab_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
android:layout_marginBottom="48dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
解决方案
在中使用了以下代码onCreate()
:
val wm = context!!.getSystemService(Context.WINDOW_SERVICE) as WindowManager
dWidth = wm.defaultDisplay
layout = inflater.inflate(R.layout.fragment_profile_main_expanded, container, false)
layout.appbar.post(Runnable {
val heightPx: Int = dWidth.width * 1 / 3
setAppBarOffset(heightPx)
})
layout.profile_avatar.layoutParams.height = dWidth.width
setAppBarOffset(heightpx)
如下:
private fun setAppBarOffset(offsetPx: Int) {
val params = layout.appbar.layoutParams as CoordinatorLayout.LayoutParams
val behavior = params.behavior as AppBarLayout.Behavior?
behavior!!.onNestedPreScroll(layout.coordinator,layout.appbar,this!!.view!!,0,offsetPx,intArrayOf(0, 0),0)
}
推荐阅读
- javascript - 试图让 HTML Radio 触发显示图像的功能
- python - 有什么方法可以在不使用私有函数的情况下将 pandas DataFrame itertuples 对象转换为字典?
- git - 当远程仓库发生变化时自动同步本地仓库
- boto - 使用 s3fs 将 sklearn 模型上传到 S3 存储桶时出现问题
- java - 无法为 Apache Commons-csv 添加 Maven 依赖项
- spring-boot - 为什么 CascadeType.MERGE、CascadeType.PERSIST 无助于获取内部数据
- javascript - 使用 javascript 中的此选择器从很多特定对象中制作动画
- vb.net - 单元格未更新 - VB.net,oledb
- javascript - 如何处理导致 React 应用程序延迟的大型原始图像重新渲染?
- vb.net - 如何将位图转换为 mat VB.net