android - 是否可以禁用 MotionLayout?

标签 android android-motionlayout

我有一个 Fragment,它在 MotionLayout 中承载了一个 RecyclerView。在回收站 View 之上,我有一个可以折叠和展开的 View ,所有这些都是在我的运动场景中完成的。它由单击触发以及响应拖动回收器 View 。到目前为止,这一切都按预期工作。

现在是症结所在:我想在我的应用程序中完全隐藏某些状态的折叠 View 。 但是如果我设置 View 的可见性或改变它的高度,当我拖动 recyclerview 时它仍然会回到 View 中。

那么是否可以完全禁用 MotionLayout(锁定约束)直到我再次启用它。禁用拖动识别器也是一个可行的解决方案。

现在一些简化的 XML 来演示这种情况。

包含带标题 View 和回收 View 的运动布局的布局

<?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/favouritesMotionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/favourites_motion_scene"
    android:animateLayoutChanges="true">

    <ImageView
        android:id="@+id/headerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        />

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        ...
    >

        <androidx.recyclerview.widget.RecyclerVieww
            android:id="@+id/favoritesList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.motion.widget.MotionLayout>

对应的运动场景:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
>

    <Transition
        motion:constraintSetStart="@id/expanded"
        motion:constraintSetEnd="@id/collapsed"
        motion:duration="300">
        <OnSwipe
            motion:touchAnchorId="@id/swipeRefreshLayout"
            motion:touchAnchorSide="top"
            motion:dragDirection="dragUp" />
    </Transition>

    <ConstraintSet android:id="@+id/expanded">
        ... Constraints for expanded header ...
    </ConstraintSet>

    <ConstraintSet android:id="@+id/collapsed">
        ... ... Constraints for collapsed header ...
    </ConstraintSet>

</MotionScene>

使用约束布局版本“2.0.0-alpha3”

实现“com.android.support.constraint:constraint-layout:2.0.0-alpha3”

回顾一下:我希望能够禁用运动布局,这样我就可以在不展开标题的情况下滚动回收 View ,直到我告诉它再次启用为止。 不幸的是,它不像 favouritesMotionLayout.isEnabled = false 那样简单,但我就是这么想的。

最佳答案

现在您可以使用 ConstraintLayout beta 2!这是官方的做法:

motionLayout.getTransition(R.id.yourTransition).setEnable(false)

关于android - 是否可以禁用 MotionLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55119362/

相关文章:

android - Parcelable 用于 inproc 通信的效率

java - 使用 Android 登录 Google 连接时出现错误

android - 选择器 react 缓慢 - 有时在开始下一个 Activity 之前根本不触发

android - parseSdkContent 失败无法初始化类 android.graphics.Typeface

java - 在圆形位图上添加圆框圆

android - MotionLayout - 日志中恼人的调试错误 : "no widget for <view id> (<view type>)"

android - MotionLayout没有动画

android - 如何在单个 View 上创建多个转换

android - 如何检查MotionScene是否处于过渡 “start”或 “end”上?

从 alpha-2 迁移到 alpha-3 时,Android MotionLayout 崩溃