我一直试图模仿Trivago非常棒的Appbar~Cardview UI转换,但是差不多一周都没有运气,我知道它是appbar / collapsing / coordinator设计,但我已经筋疲力尽了,所以我最终得到了计算appbar的偏移量,将它(它的负数减去它将被减去)添加到视图的布局高度,
// 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的视角高度取决于appbar的偏移或高度,如果我把它放在折叠工具栏中,它的顶部不会停在它的位置,而是上升而不是我想要什么......
任何帮助将不胜感激...
我看到了一些可能的事情。 1.每当使用ConstraintLayout时,必须实现子视图的垂直和水平约束。如果不这样做会导致奇怪的ui内容。像这样的例子。
<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"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="someId"/>
<TextView
android:id="@+id/someId"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:background="@color/colorPrimaryDark"
app:layout_constraintTop_toBottomOf="@id/coupons_lst"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRigh_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
接下来我还注意到您没有使用父布局连接布局。你有开放的布局标签,但我没有看到你发布的代码中不知道这是否是故意的。
最后但并非最不重要的是我最近遇到的Quirky UI问题是不将我的工具栏布局高度设置为“?attr / actionBarSize”。尝试在你的上面设置
<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="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
这是我多次尝试和调整,了解trivago如何做到这一点..
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#cececece"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cecece"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout3"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="50dp"
android:background="#cececece">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolba3r"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cecece"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.9"
app:cardBackgroundColor="#ffffff00"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedShit"
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>
两个appbarlayout,它接近我想要实现的目标,但问题是appbarlayout正在闪烁......