java - 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);
解决方案
如果您不介意在视图中对所有布局更改进行动画处理,可以尝试以下方法。添加并查看它是否适用于您的用例很容易。添加:
android:animateLayoutChanges="true"
到您的 NestedScrollView 并关闭所有转换。每当您更改视图的边界或在屏幕上或屏幕外带来新的东西时,android 都会自动处理动画。同样是 YMMV,因为您无法控制动画的速度等,但值得一试。
推荐阅读
- python - 用户选择 emailProvider,但程序不会定义变量
- c# - ASP.NET Core 3.1 + Open ID Connect + AD 不会将 id 令牌持久化到 User.Identity
- python - Pythonpipbuilder:解决依赖关系 - 列表索引超出范围
- ios - 取消隐藏 iPad 上的导航视图
- spring-boot - 六边形架构中的层
- python - Pandas 或 python,遍历数据框中列中的 SQL 查询,将结果附加到数据框中
- amazon-cognito - 是否可以创建一个信任,允许来自 cognito 的联合用户根据自定义属性担任角色?
- flutter - 带有特殊字符 FLUTTER 的 HIVE openBox 问题
- javascript - 输入框的简单赋值返回 NaN
- node.js - 使用 SQL Server 处理多对多对一关系 - 分配数据的配置文件