【问题标题】:motionlayout - view gets hide when the transition completedmotionlayout - 转换完成时视图隐藏
【发布时间】:2020-01-02 06:55:51
【问题描述】:

我正在将我的活动的视图从上到下移动,问题是,它工作正常并将我的视图从下到上移动但是当转换完成时,它隐藏了视图

这是我的布局:

   <androidx.constraintlayout.motion.widget.MotionLayout
            android:id="@+id/fist_motionlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutDescription="@xml/fistactivity_motions"
            app:showPaths="true">

            <include
                android:id="@+id/appbar"
                layout="@layout/appbar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.cardview.widget.CardView
                android:id="@+id/marque"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                app:cardBackgroundColor="#395471"
                app:cardCornerRadius="7dp"
                app:cardElevation="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                app:layout_constraintTop_toBottomOf="@+id/appbar"
                tools:visibility="visible">

            </androidx.cardview.widget.CardView>

            <FrameLayout
                android:id="@+id/fm_firstact_content"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/marque" >

</FrameLayout>

        </androidx.constraintlayout.motion.widget.MotionLayout>

这是我的 xml 代码:

  <?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:motion="http://schemas.android.com/tools">

    <ConstraintSet android:id="@+id/start_set">
        <Constraint
            android:id="@+id/fm_firstact_content"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"></Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end_set">
        <Constraint
            android:id="@+id/fm_firstact_content"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/marque"></Constraint>
    </ConstraintSet>

    <Transition
        android:id="@+id/trans_firstact_fm_content"
        app:constraintSetEnd="@id/end_set"
        app:constraintSetStart="@id/start_set"
        app:duration="2000"></Transition>
</MotionScene>

在活动中,我只是启动它:

 MotionLayout ml=vf.findViewById(R.id.fist_motionlayout);
    ml.transitionToEnd();

当我运行应用程序时,它将视图从底部移动到顶部并突然变得不可见,奇怪的是当我从另一个活动再次运行活动时,它工作正常并且它不会变得不可见但是如果我关闭应用程序并再次运行它,它会隐藏。每次应用第一次运行时,视图在转换完成后隐藏

这是我的毕业典礼:

implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
implementation 'androidx.cardview:cardview:1.0.0-alpha01'
implementation 'com.google.android.material:material:1.2.0-alpha01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'

如何解决?

【问题讨论】:

    标签: android android-constraintlayout android-transitions android-motionlayout


    【解决方案1】:

    在你的布局中为 marque cardview 删除约束并在motionscenes xml 中为它创建一个约束集。当使用运动布局时,你不能在activity_layout.xml 中为视图定义约束。它将从motion.xml.so 中获取约束,在运动布局中为该卡片视图创建约束集,并在活动布局中删除所有约束。

    【讨论】:

      【解决方案2】:

      如果您要将motionScene 约束基于视图,则它也应该在motionScene 中定义。我已将motionLayout 中所有视图的约束信息移动到motionScene 中,因此现在它应该可以按预期工作。它可能需要一些小的调整,但试试这个:

      你的布局:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.motion.MotionLayout 
          android:id="@+id/fist_motionlayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layoutDescription="@xml/fistactivity_motions"
          app:showPaths="true"
          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">
      
          <include
              android:id="@+id/appbar"
              layout="@layout/appbar"/>
      
          <androidx.cardview.widget.CardView
              android:id="@+id/marque"
              android:layout_width="match_parent"
              android:layout_height="45dp"
              app:cardBackgroundColor="#395471"
              app:cardCornerRadius="7dp"
              app:cardElevation="1dp"
              android:layout_marginLeft="1dp"
              android:layout_marginRight="1dp"
              tools:visibility="visible"/>
      
          <FrameLayout
              android:id="@+id/fm_firstact_content"
              android:layout_width="0dp"
              android:layout_height="0dp" />
      </android.support.constraint.motion.MotionLayout>
      

      还有你的运动场景文件:

      <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:motion="http://schemas.android.com/tools">
      
      <ConstraintSet android:id="@+id/start_set">
          <Constraint
              android:id="@+id/fm_firstact_content"
              android:layout_width="0dp"
              android:layout_height="0dp"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="parent"/>
      
          <Constraint
              android:id="@+id/marque"
              android:layout_width="match_parent"
              android:layout_height="45dp"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/appbar"/>
      
          <Constraint
              android:id="@+id/appbar"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toTopOf="parent"/>
      </ConstraintSet>
      
      <ConstraintSet android:id="@+id/end_set">
          <Constraint
              android:id="@+id/fm_firstact_content"
              android:layout_width="0dp"
              android:layout_height="0dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/marque"/>
      
          <Constraint
              android:id="@+id/marque"
              android:layout_width="match_parent"
              android:layout_height="45dp"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/appbar"/>
      
          <Constraint
              android:id="@+id/appbar"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toTopOf="parent"/>
      </ConstraintSet>
      
      <Transition
          android:id="@+id/trans_firstact_fm_content"
          app:constraintSetEnd="@id/end_set"
          app:constraintSetStart="@id/start_set"
          app:duration="2000"/>
      </MotionScene>
      

      同样,它可能需要一些小的调整,但这是您应该解决问题的方向

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 2011-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多