【问题标题】:Are ConstraintLayout Animations In ScrollView Possible?ScrollView 中的 ConstraintLayout 动画是否可行?
【发布时间】:2020-10-07 13:04:30
【问题描述】:

每当我尝试在可滚动的 constraintLayout 上应用 ConstraintLayout 动画时,我都会收到无法将 ScrollView 转换为 ConstraintLayout 的错误,这听起来很合理。

有没有办法在不使用 ScrollView 作为父级的情况下使约束布局可滚动?这样我就可以使用它的甜美动画了。

谢谢。

【问题讨论】:

  • 从您所说的“我无法将 ScrollView 转换为 ConstraintLayout”来看,这听起来更像是您试图将 ScrollView 转换为 ConstraintLayout,如下所示:(Kotlin)myScrollView as ConstraintLayout, (Java)(ConstraintLayout) myScrollView。如果是这种情况,那么您的错误与动画无关。请提供您的代码以供进一步调查!
  • 感谢您的回答,但是 IDE 告诉我“演员永远不会成功”

标签: android animation layout constraints scrollview


【解决方案1】:

这是ConstraintLayoutScrollView / NestedScrollView 的常见问题。您需要将您的ScrollView 设置为ConstraintLayout 的父级,并且在进行转换之前,您需要克隆您的ConstraintLayout

类似这样的:

科特林:

 ConstraintSet().apply {
        clone(constraintLayoutView);
        clone(this, R.layout.activity_animation) // your transition
    }

或者如果您使用的是 java:

  ConstraintSet constraintSet = new ConstraintSet();
  constraintSet.clone(constraintLayoutView);
  constraintSet.clone(this, R.layout.activity_animation); 

【讨论】:

  • 谢谢!虽然我似乎无法克隆我的约束布局。 android.content.res.Resources$NotFoundException。根据谷歌我不能传递视图的 ID,我只需要传递一个布局。 ConstraintSet().apply { clone(context, R.id.login_constraint) //ERROR clone(context, R.layout.activity_login_transition) applyTo(resources.getLayout(R.layout.activity_login) as ConstraintLayout) }
  • 我正在检查文档,有一个方法 clone 接收 contextconstraintLayoutId 作为参数,您也可以仅通过参考布局视图来调用该方法. clone(yourConstraintLayoutView),你可以试试。这是文档。我敢肯定developer.android.com/reference/androidx/constraintlayout/…
  • 哦,是的,我刚刚更新了答案,并使用了正确的实现:D
【解决方案2】:

为非父约束布局设置的约束

我想通了。这很有效。我认为这可能对其他开发人员有用,因为我认为在 ScrollView 中使用 ConstraintLayout 是很正常的,例如在登录屏幕中。

所以现在我有 2 个 Xml 对吗? “原版”和过渡版。

Original: <ScrollView> <ConstraintLayout><ConstraintLayout> <ScrollView>
Transition: <ConstraintLayout></ConstraintLayout>

Transition XML 没有 ScrollView,因为我需要在 ViewGroup(ScrollView 的子项,我的约束布局)内克隆一个完整的布局(我的转换布局)。如果您尝试将子约束布局克隆到另一个子约束布局中,它会因 .getChild() 方法而崩溃,不知道为什么。但是您可以将 ConstraintLayout 布局克隆到 ConstraintLayout 视图。

val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.activity_login_transition) // here I'm cloning the whole transition layout
TransitionManager.beginDelayedTransition(login_constraint) //this is only for animation
constraintSet.applyTo(login_constraint) // and here I'm applying it only to my constraint layout. 'login_constraint' is my constraint's ID

就是这样。希望它也对你有用。

感谢 Ezequiel Zanetta

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多