首页 > 解决方案 > NestedScrollView 不能强制转换为 android.support.constraint.ConstraintLayout

问题描述

我在 ScrollView 中有 ConstraintLayout。我正在尝试使用 ConstraintLayout 动画。它在 ScrollView 之外完美运行,但是当我想在 ScrollView 中使用它时,AndroidStudio 会说:

android.support.v4.widget.NestedScrollView cannot be cast to android.support.constraint.ConstraintLayout

我知道它是因为我的根布局是 ScrollView 但我不知道如何解决这个问题。

我尝试在 ScrollView 之前添加另一个 ConstraintLayout,这次 APP 没有崩溃,但是当我按下按钮时没有任何反应。

<android.support.v4.widget.NestedScrollView 
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<android.support.constraint.ConstraintLayout
    android:id="@+id/const1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    tools:context=".MainActivity">

这就是我在 MainActivity 中所做的

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(this, R.layout.activity_main_animation);
ChangeBounds transition = new ChangeBounds();
transition.setInterpolator(new AnticipateInterpolator(1.0f));
transition.setDuration(1200);
TransitionManager.beginDelayedTransition(cc1, transition);
constraintSet.applyTo(cc1);

标签: javaandroidandroid-layout

解决方案


如果您不介意在视图中对所有布局更改进行动画处理,可以尝试以下方法。添加并查看它是否适用于您的用例很容易。添加:

    android:animateLayoutChanges="true"

到您的 NestedScrollView 并关闭所有转换。每当您更改视图的边界或在屏幕上或屏幕外带来新的东西时,android 都会自动处理动画。同样是 YMMV,因为您无法控制动画的速度等,但值得一试。


推荐阅读