【问题标题】:Resizing a view based on Appbar's offset or height根据 Appbar 的偏移量或高度调整视图大小
【发布时间】: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
        }
    })

Initial Height of the view

When scrolled up or down (slowly)

When fling or scrolled fast

我真的很难模仿 trivago 使视图的高度取决于应用栏的偏移量或高度的方式,如果我将它放在折叠工具栏内,它的顶部不会从它的位置停止,而是向上这不是我想要的......

任何帮助将不胜感激......

【问题讨论】:

    标签: android kotlin android-coordinatorlayout android-appbarlayout contentoffset


    【解决方案1】:

    我看到了一些可能的情况。 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>
    

    接下来我还注意到您没有将布局与父布局连接起来。您有开始的 Layout 标签,但我没有在您发布的代码中看到它,不知道这是否是故意的。

    最后但并非最不重要的一点是,我最近遇到的 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>
    

    【讨论】:

      【解决方案2】:

      这是我多次尝试和调整的,了解 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>
      

      两个appbarlayouts,它接近我想要实现的目标,但问题是appbarlayouts正在闪烁......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-18
        • 2013-05-08
        • 2016-05-12
        • 2015-01-16
        • 1970-01-01
        • 2021-08-02
        相关资源
        最近更新 更多