【问题标题】:MotionLayout animation not working when DataBinding is used使用 DataBinding 时 MotionLayout 动画不起作用
【发布时间】:2021-02-14 08:02:55
【问题描述】:

所以我在我的应用程序中使用 dataBinding 和 MVVM,这意味着我使用 DataBindingUtil 类而不是 SetContentView,例如:

val binding = DataBindingUtil.setContentView(this, R.layout.activity_record)
 

在我的布局文件中,我使用了运动布局(用作根布局)来为 CalendarView 小部件设置动画以过渡到屏幕底部。

        <CalendarView
            android:id="@+id/datePickerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:background="#191919"
            android:gravity="center"
            android:orientation="vertical"
            android:theme="@style/CalendarViewTheme"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent" />

在我的 End 约束中,我有以下代码:

        <Constraint
            android:id="@+id/datePickerView">
            <Layout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                motion:layout_constraintBottom_toBottomOf="parent"
                motion:layout_constraintEnd_toEndOf="parent"
                motion:layout_constraintStart_toStartOf="parent"/>
        </Constraint>

当我调用ml.transitionToEnd() 时,在我的活动中,calendarView 应该从屏幕下方(在屏幕上不可见)到屏幕底部(整个视图在屏幕底部可见)进行动画处理。这个动画在我使用setContentView() 时发生,但在我使用 dataBinding 时不会发生。 val binding = DataBindingUtil.setContentView()

用一种更简单的方式来说,当我使用 DataBinding 时,如何让 MotionLayout 从我的活动中工作?

【问题讨论】:

    标签: android kotlin android-constraintlayout android-databinding android-motionlayout


    【解决方案1】:

    我对 Kotlin 不太熟悉,但是在 Java 中我是这样处理的:

    public class DashboardFragment extends Fragment {
    
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_dashboard, container, false);
            return mBinding.getRoot();
        }
    

    这适用于 MotionLayout。

    【讨论】:

    • 我基本上做了同样的事情,但在 Kotlin 中。除了运动布局外,其他一切都很好。当我使用 DataBindingUtil 类时,动画不起作用。
    • @PraantoSamadder 但你看到我使用了.inflate()而不是.setContentView()?或者这是 Kotlin 的等价物?
    • 这是一个活动而不是一个片段。在 Kotlin 中使用 DataBinding 时,我确实在片段中使用了 .inflate()
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    相关资源
    最近更新 更多