【发布时间】:2019-08-25 15:55:47
【问题描述】:
我一直在尝试模仿 Trivago 非常棒的 Appbar~Cardview UI 过渡,但几乎一周没有运气了,我知道它是一个 appbar/collapsing/coordinator 设计,但我已经筋疲力尽了,所以我最后计算了应用栏的偏移量,将它(它是负数,因此将被减去)添加到视图的布局高度,
// its a negative so if you add it, it will subtract
view.layoutParams.height = initialHeight + appbarOffset
它就像一个魅力,直到我扔它!..有时视图的高度不完整,罪魁祸首是appbar的偏移量,如果你扔它,它不会继续给你它的最小值和最大值...离开视图的高度取决于它不完整...请看看我的简单 xml..
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/appbar_layout_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="300dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:layout_margin="30dp"
app:cardBackgroundColor="#ffffff"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/coupons_lst"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintTop_toBottomOf="@id/coupons_lst"/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
这是我在 kotlin 方面的做法
appbar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener {
_, offset ->
val positiveOffset = offset * -1
if (positiveOffset > 10) {
val layoutParams = cardView.layoutParams
layoutParams.height = heightFromViewTreeObserver - positiveOffset
cardView.layoutParams = layoutParams
}
})
When scrolled up or down (slowly)
我真的很难模仿 trivago 使视图的高度取决于应用栏的偏移量或高度的方式,如果我将它放在折叠工具栏内,它的顶部不会从它的位置停止,而是向上这不是我想要的......
任何帮助将不胜感激......
【问题讨论】:
标签: android kotlin android-coordinatorlayout android-appbarlayout contentoffset