【问题标题】:Is there any alternative for MotionLayout to create collapsible header with ConstraintLayout?MotionLayout 是否有任何替代方法可以使用 ConstraintLayout 创建可折叠标题?
【发布时间】:2020-12-03 23:40:23
【问题描述】:

我使用约束布局创建了一个布局,其中主体视图(片段的 viewpager)依赖于标题(例如,viewpager 具有属性 layout_constraintTop_toBottomOf="@+id/button1" 其中 button1 是标题最底部的视图)。现在我需要创建像可折叠标题这样的动画,但是 CollapsingToolbarLayout 出现了一些问题,因为 viewpager 和标题正在连接,我认为我需要 ConstraintLayout 来做到这一点,而不是 CoordinatorLayout。

我做了一些研究,发现 droidcon 视频解释了使用 ConstraintLayout 来替换 stackoverflow answer 中的 CollapsingToolbarLayout。我试图实现它,但这并不完全是我所需要的,因为它只是让我使用 ConstraintLayout 创建标题,而我需要 ConstraintLayout 作为根布局。我需要从活动中设置 onClickListener 按钮 1(在标题中),如果我将按钮放在 2 个不同的布局中(在 header_open.xml 和 header_close.xml 中),我不能这样做。 或者我错过了什么,而我实际上可以做到这一点?

我使用 MotionLayout 创建可折叠页眉的另一种方法,它就像我想要的那样完美,因为我只想更改页眉中某些视图的可见性和约束。但是 MotionLayout 需要依赖 ConstraintLayout 2.0,不幸的是我在我用来测试的设备之一中发现了 bug(使用 ConstraintLayout 的页面搞砸了)。

我可以使用任何替代方法,它就像 MotionLayout 但使用依赖项 ConstraintLayout 1.xx 吗? 可能正在使用约束集和转换?

附言我尝试使用约束集和过渡管理器,但我发现的所有示例和解释都使用 onClickListener 来触发动画或布局更改(从约束集 1 到 2),而我需要滚动来触发更改)我想我可以使用约束集和过渡如果我知道我可以使用什么来触发动作布局中 <OnSwipe> 等行为的更改。所以我的问题可能是“我可以使用任何替代方案,就像 MotionLayout 中的<OnSwipe> 一样工作吗?”

或者可能有人可以告诉我为什么使用 ConstraintLayout 的页面在使用依赖项 androidx.constraintlayout:constraintlayout:2.0.0-rc1 时会搞砸?

我的带有折叠约束布局的代码仍然有一些错误,所以我将把我的代码放在 MotionLayout 中。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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/constraintRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    app:layoutDescription="@xml/scene_activity_main"
    tools:context=".presentation.MainActivity">

    <ImageView
        android:id="@+id/imgHeader"
        android:layout_width="@dimen/dimen_0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@drawable/bg_header"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dimen_12dp"
        android:layout_marginTop="@dimen/dimen_35dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@drawable/ic_back"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/btnHistory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimen_8dp"
        android:text="@string/label_history"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imgBanner" />

    <TextView
        android:id="@+id/tvLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dimen_12dp"
        android:text="@string/label_text"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/imgBanner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/imgBanner"
        android:layout_width="@dimen/dimen_203dp"
        android:layout_height="@dimen/dimen_35dp"
        app:layout_constraintBottom_toBottomOf="@+id/imgHeader"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/imgHeader"
        android:src="@drawable/bg_rounded_primary"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="@dimen/dimen_47dp"
        android:layout_height="@dimen/dimen_47dp"
        android:src="@drawable/ic_point"
        app:layout_constraintBottom_toBottomOf="@id/imgBanner"
        app:layout_constraintStart_toStartOf="@id/imgBanner"
        app:layout_constraintTop_toTopOf="@id/imgBanner" />

    <TextView
        android:id="@+id/tvValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/dimen_10dp"
        android:textColor="@android:color/white"
        android:textSize="@dimen/text_size_20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/imgBanner"
        app:layout_constraintEnd_toEndOf="@+id/imgBanner"
        app:layout_constraintTop_toTopOf="@+id/imgBanner"
        tools:text="100" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/dimen_24dp"
        android:background="@drawable/bg_primary_tab"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnHistory">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorTransparent"
            android:paddingVertical="@dimen/dimen_8dp"
            app:tabBackground="@drawable/bg_primary_tab"
            app:tabGravity="fill"
            app:tabIndicator="@drawable/bg_indicator_tab"
            app:tabIndicatorColor="@color/colorGrey19"
            app:tabIndicatorFullWidth="true"
            app:tabIndicatorGravity="center"
            app:tabInlineLabel="true"
            app:tabMode="fixed"
            app:tabRippleColor="@null"
            app:tabTextAppearance="@style/CustomTextAppearanceTab">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tab1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/ic_play"
                android:text="@string/label_tab1" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tab2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/ic_coin"
                android:text="@string/label_tab2" />

        </com.google.android.material.tabs.TabLayout>
    </androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.motion.widget.MotionLayout>

这是我的场景代码

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Transition
        app:constraintSetEnd="@+id/end"
        app:constraintSetStart="@+id/start"
        app:duration="500"
        app:motionInterpolator="linear">

        <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@id/viewPager"
            app:touchAnchorSide="top" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@+id/btnHistory">
            <PropertySet android:visibility="visible" />
        </Constraint>

        <Constraint android:id="@+id/tvLabel">
            <PropertySet android:visibility="visible" />
        </Constraint>

        <Constraint android:id="@+id/imgBanner">
            <Layout
                android:layout_width="@dimen/dimen_203dp"
                android:layout_height="@dimen/dimen_35dp"
                app:layout_constraintBottom_toBottomOf="@+id/imgHeader"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/imgHeader" />
        </Constraint>

        <Constraint android:id="@+id/imgIcon">
            <Layout
                android:layout_width="@dimen/dimen_47dp"
                android:layout_height="@dimen/dimen_47dp"
                app:layout_constraintBottom_toBottomOf="@id/imgBanner"
                app:layout_constraintStart_toStartOf="@id/imgBanner"
                app:layout_constraintTop_toTopOf="@id/imgBanner" />
        </Constraint>

        <Constraint android:id="@+id/viewPager">
            <Layout
                android:layout_marginTop="@dimen/dimen_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/btnHistory" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@+id/btnHistory">
            <PropertySet android:visibility="invisible" />
        </Constraint>

        <Constraint android:id="@+id/tvLabel">
            <PropertySet android:visibility="invisible" />
        </Constraint>

        <Constraint android:id="@+id/imgBanner">
            <Layout
                android:layout_width="@dimen/dimen_176dp"
                android:layout_height="@dimen/dimen_35dp"
                app:layout_constraintBottom_toBottomOf="@+id/btnBack"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/btnBack" />
        </Constraint>

        <Constraint android:id="@+id/imgIcon">
            <Layout
                android:layout_width="@dimen/dimen_35dp"
                android:layout_height="@dimen/dimen_35dp"
                app:layout_constraintBottom_toBottomOf="@id/imgBanner"
                app:layout_constraintStart_toStartOf="@id/imgBanner"
                app:layout_constraintTop_toTopOf="@id/imgBanner" />
        </Constraint>

        <Constraint android:id="@+id/viewPager">
            <Layout
                android:layout_marginTop="@dimen/dimen_16dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imgBanner" />
        </Constraint>
    </ConstraintSet>
</MotionScene>

我的问题使用粗体,以防我的解释令人困惑。对不起我的语法,英语不是我的第一语言。我愿意接受与此问题相关的所有建议(即使只是指向一些参考资料的链接也将不胜感激)。提前谢谢你。

【问题讨论】:

  • CollapsingToolbarLayout(您的 1. 和 2.)的两个问题都是可解决的问题(第一个根本不是问题 - 任何布局都可以在 CollapsingToolbarLayout 中)。您应该在问题中包含足够的代码来重现您的问题。
  • 感谢您的反馈,我意识到我的第一个问题并不是真正的问题,因此我编辑了我的问题并添加了一些代码以帮助您更好地理解我的问题

标签: android android-constraintlayout android-motionlayout


【解决方案1】:

因为还没有人回答这个问题,所以我决定把我的解决方案放在这里,如果你们有任何关于这个问题的建议或更好的方法,请告诉我。

我找不到 MotionLayout 的替代方案,因此我最终为此使用了 CollapsingToolbarLayout。幸运的是,经过多次尝试,我可以得到我想要的 UI,并找到了创建具有我想要的布局的平滑滚动动画的代码。我在 stackoverflow 中找到了这段代码,抱歉,因为我忘记在链接中添加书签,所以无法放参考。

val offsetChangedListener =
        AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
            if (collapsingToolbar.height + verticalOffset < collapsingToolbar.scrimVisibleHeightTrigger) {
                //COLLAPSE
                //set layout params
            } else {
                //EXPAND
                //set layout params                    
            }
        }
    appBarLayout.addOnOffsetChangedListener(offsetChangedListener)

这是我的布局的最终​​代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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/constraintRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".presentation.MainActivity">
<ImageView
    android:id="@+id/imgHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/bg_header"
    tools:ignore="ContentDescription,VectorDrawableCompat" />

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorTransparent"
    android:fitsSystemWindows="true"
    app:elevation="@dimen/dimen_0dp">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:minHeight="@dimen/dimen_60dp"
        app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed">

        <ImageView
            android:id="@+id/btnBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dimen_12dp"
            android:layout_marginTop="@dimen/dimen_35dp"
            app:layout_collapseMode="pin"
            app:srcCompat="@drawable/ic_back"
            tools:ignore="ContentDescription,VectorDrawableCompat" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            app:layout_collapseMode="pin">

            <TextView
                android:id="@+id/btnHistory"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_8dp"
                android:text="@string/label_history"
                android:textColor="@android:color/white"
                android:textSize="@dimen/text_size_12sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imgBanner" />

            <TextView
                android:id="@+id/tvLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/dimen_12dp"
                android:text="@string/label_text"
                android:textColor="@android:color/white"
                android:textSize="@dimen/text_size_18sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@id/imgBanner"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

            <ImageView
                android:id="@+id/imgBanner"
                android:layout_width="@dimen/dimen_203dp"
                android:layout_height="@dimen/dimen_35dp"
                android:layout_marginTop="@dimen/dimen_121dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/bg_rounded_primary"
                tools:ignore="ContentDescription" />

            <ImageView
                android:id="@+id/imgIcon"
                android:layout_width="@dimen/dimen_47dp"
                android:layout_height="@dimen/dimen_47dp"
                android:src="@drawable/ic_point"
                app:layout_constraintBottom_toBottomOf="@id/imgBanner"
                app:layout_constraintStart_toStartOf="@id/imgBanner"
                app:layout_constraintTop_toTopOf="@id/imgBanner" />

            <TextView
                android:id="@+id/tvValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/dimen_10dp"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/text_size_20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/imgBanner"
                app:layout_constraintEnd_toEndOf="@+id/imgBanner"
                app:layout_constraintTop_toTopOf="@+id/imgBanner"
                tools:text="100" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/dimen_24dp"
    android:background="@drawable/bg_primary_tab"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorTransparent"
        android:paddingVertical="@dimen/dimen_8dp"
        app:tabBackground="@drawable/bg_primary_tab"
        app:tabGravity="fill"
        app:tabIconTint="@color/colorGray40"
        app:tabIndicator="@drawable/bg_grey19_tab"
        app:tabIndicatorColor="@color/colorGrey19"
        app:tabIndicatorFullWidth="true"
        app:tabIndicatorGravity="center"
        app:tabInlineLabel="true"
        app:tabMode="fixed"
        app:tabRippleColor="@null"
        app:tabTextAppearance="@style/CustomTextAppearanceTab">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_play"
            android:text="@string/label_redeem_voucher" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_coin"
            android:text="@string/label_challenge" />

    </com.google.android.material.tabs.TabLayout>
</androidx.viewpager.widget.ViewPager>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我认为这比为标题折叠和展开创建 2 个布局文件要好,因为正如我所提到的,我需要 1 个布局文件,因此我可以将 onClickListener 设置为标题中的按钮。

【讨论】:

  • 你能把这段代码的输出贴出来吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 2011-06-19
  • 1970-01-01
  • 2012-02-20
  • 2020-02-16
相关资源
最近更新 更多