Cre*_*tar 6 android android-coordinatorlayout android-motionlayout
我正在使用implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1',并启用了数据绑定。
当我的看法
<ImageView
android:id="@+id/im_lightning"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="4dp"
android:contentDescription="@string/charging"
android:src="@drawable/ic_lightning"
android:visibility="@{batteryViewModel.liveData.charging ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="@id/tv_percent"
app:layout_constraintRight_toLeftOf="@id/tv_percent"
app:layout_constraintTop_toTopOf="@id/tv_percent"
app:layout_constraintVertical_bias="1.0" />
Run Code Online (Sandbox Code Playgroud)
围绕协调器布局,其可见性变化按预期发生。一旦我将它包装在 MotionLayout 中,可见性更改就不会像以前一样工作。准确地说,视图在应该可见的时候不可见。它在触发事件后变为可见一秒钟,然后恢复为不可见。这是一个已知的错误?
需要时的代码:
协调器布局:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground">
<ImageView (same as above) />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
运动布局:
<layout 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"
tools:context=".ui.MainActivity">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
app:layoutDescription="@xml/home_scene_0"
app:showPaths="true">
<ImageView (same as above) />
</androidx.constraintlayout.motion.widget.MotionLayout>
<data>
<import type="android.view.View" />
<variable
name="batteryViewModel"
type="rish.crearo.minimalphone.viewmodels.BatteryViewModel" />
</data>
</layout>
Run Code Online (Sandbox Code Playgroud)
我遇到同样的问题。看起来,如果您在数据绑定和运动场景中操作相同视图的属性(在本例中为可见性),则运动场景优于绑定值,因此您需要在场景上绑定值。
TL;DR 绑定视图的属性运动场景的constraintSet(而不是layout.xml)
我使用@BindingAdapter来实现这一点。
@BindingAdapter("goneUnlessOnMotionLayout")
public static void goneUnlessOnMotionLayout(View view, boolean visible) {
if (view.getParent() instanceof MotionLayout) {
final MotionLayout layout = (MotionLayout) view.getParent();
final int setToVisibility = visible ? View.VISIBLE : View.GONE;
final float setToAlpha = visible ? 1f : 0f;
for (int constraintId : layout.getConstraintSetIds()) {
final ConstraintSet constraint = layout.getConstraintSet(constraintId);
if (constraint != null) {
constraint.setVisibility(view.getId(), setToVisibility);
constraint.setAlpha(view.getId(), setToAlpha);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意当motionLayout正在进行时,不要在视图模型中notifyPropertyChanged,即motionLayout.progress != 0或1,它将导致约束之间的自然运动中断
| 归档时间: |
|
| 查看次数: |
2797 次 |
| 最近记录: |