Android ScrollView中的ConstraintLayout动画是否可行?

Android ScrollView中的ConstraintLayout动画是否可行?,android,animation,layout,constraints,scrollview,Android,Animation,Layout,Constraints,Scrollview,每当我尝试在可滚动的ConstraintLayout上应用ConstraintLayout动画时,我都会遇到一个错误,即无法将滚动视图强制转换为ConstraintLayout,这听起来很合理 有没有一种方法可以使约束布局在不使用ScrollView作为父项的情况下可滚动?这样我就可以使用它甜美的动画 谢谢。这是ConstraintLayout和ScrollView/NestedScrollView的常见问题。您需要将您的ScrollView设置为ConstraintLayout的父级,在执行转

每当我尝试在可滚动的ConstraintLayout上应用ConstraintLayout动画时,我都会遇到一个错误,即无法将滚动视图强制转换为ConstraintLayout,这听起来很合理

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


谢谢。

这是
ConstraintLayout
ScrollView/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); 

这是
ConstraintLayout
ScrollView/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); 

非父级约束布局的约束集

我想出来了。这真是奇迹。我认为这对其他开发人员可能有用,因为我假设在ScrollView中使用ConstraintLayout是非常正常的,例如在登录屏幕中

现在我有了2个XML,对吗?“原始”和过渡

Original: <ScrollView> <ConstraintLayout><ConstraintLayout> <ScrollView>
Transition: <ConstraintLayout></ConstraintLayout>
就这样。希望它对你也有用


感谢Ezequiel Zanetta为非父级的约束布局设置的约束集

我想出来了。这真是奇迹。我认为这对其他开发人员可能有用,因为我假设在ScrollView中使用ConstraintLayout是非常正常的,例如在登录屏幕中

现在我有了2个XML,对吗?“原始”和过渡

Original: <ScrollView> <ConstraintLayout><ConstraintLayout> <ScrollView>
Transition: <ConstraintLayout></ConstraintLayout>
就这样。希望它对你也有用


感谢Ezequiel Zanetta

从您所说的“我不能将ScrollView转换为ConstraintLayout”判断,这听起来更像是您试图将ScrollView转换为ConstraintLayout,如下所示:(Kotlin)
myScrollView作为ConstraintLayout
,(Java)
(ConstraintLayout)myScrollView
。如果是这种情况,那么您的错误与动画无关。请提供您的代码以便进一步调查!谢谢你的回答,伙计,但是IDE告诉我“强制转换永远不会成功”,从你说的“我不能将ScrollView强制转换为ConstraintLayout”来看,这听起来更像是你试图将ScrollView强制转换为ConstraintLayout,就像这样:(Kotlin)
myScrollView as ConstraintLayout
,(Java)
(ConstraintLayout)myScrollView
。如果是这种情况,那么您的错误与动画无关。请提供您的代码以便进一步调查!谢谢你的回答,伙计,但是IDE告诉我“演员永远不会成功”谢谢!虽然我似乎无法克隆我的约束布局。android.content.res.Resources$NotFoundException。根据谷歌的说法,我不能传递视图的ID,我只需要传递一个布局。ConstraintSet().apply{clone(context,R.id.login\u constraint)//错误克隆(context,R.layout.activity\u login\u transition)applyTo(resources.getLayout(R.layout.activity\u login)as ConstraintLayout)}我正在查看文档,有一个方法
clone
接收
context
constraintLayoutId
作为参数,您也可以仅通过对布局视图的引用调用该方法<代码>克隆(yourConstraintLayoutView),您可以尝试。这是医生。我确信这是有道理的哦,是的,我刚刚用正确的实现更新了答案:DThanks!虽然我似乎无法克隆我的约束布局。android.content.res.Resources$NotFoundException。根据谷歌的说法,我不能传递视图的ID,我只需要传递一个布局。ConstraintSet().apply{clone(context,R.id.login\u constraint)//错误克隆(context,R.layout.activity\u login\u transition)applyTo(resources.getLayout(R.layout.activity\u login)as ConstraintLayout)}我正在查看文档,有一个方法
clone
接收
context
constraintLayoutId
作为参数,您也可以仅通过对布局视图的引用调用该方法<代码>克隆(yourConstraintLayoutView),您可以尝试。这是医生。我确信这是有道理的哦,是的,我刚刚用正确的实现更新了答案:D