首页 > 解决方案 > MotionLayout Android中的RecyclerView问题

问题描述

我面临的问题是回收器视图在转换后无法正确呈现。

请在这里找到视频

ConstraintLayout 版本:2.0.4 RecyclerView 版本:1.1.0

活动主页.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
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"
android:id="@+id/homeParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layoutDescription="@xml/activity_home_scene">

<FrameLayout
    android:id="@+id/homeSearchContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/all_margin_16dp"
    android:layout_marginTop="@dimen/all_margin_16dp"
    android:layout_marginEnd="@dimen/all_margin_16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/collegeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/homeSearchContainer">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/homeCollegeNameTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/all_padding_8dp"
        android:fontFamily="@font/product_sans_regular"
        android:textColor="@color/colorBlack"
        android:textSize="@dimen/all_font_16sp"
        android:maxLines="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/homeCategoriesRv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/collegeLayout"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/tabContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/homeCategoriesRv">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/homeTabs"
        style="@style/AppTheme.TabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="start"
        android:background="@color/colorWhite"
        app:tabGravity="fill"
        app:tabMode="fixed" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/homeContainer"
    android:layout_width="match_parent"
    android:layout_height="@dimen/all_height_0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tabContainer">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/refreshLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/all_height_0dp"
            android:visibility="gone"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
            
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/noNetIv"
                    android:layout_width="@dimen/all_width_200dp"
                    android:layout_height="@dimen/all_height_200dp"
                    android:layout_margin="@dimen/all_margin_8dp"
                    app:tint="@color/colorPrimaryDark"
                    app:srcCompat="@drawable/ic_wifi_off"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"/>

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/home_no_internet"
                    android:textColor="@color/colorMain"
                    android:textSize="@dimen/all_font_16sp"
                    android:fontFamily="@font/product_sans_regular"
                    android:gravity="center"
                    android:layout_margin="@dimen/all_margin_8dp"
                    app:layout_constraintTop_toBottomOf="@+id/noNetIv"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"/>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/homeVp"
            android:layout_width="match_parent"
            android:layout_height="@dimen/all_height_0dp"
            android:orientation="horizontal"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/btnAddPost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_marginEnd="@dimen/all_margin_16dp"
    android:layout_marginBottom="@dimen/all_margin_56dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/ic_add"
    tools:ignore="ContentDescription" />

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

activity_home_scene.xml

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

  <Transition
      motion:constraintSetStart="@+id/start"
      motion:constraintSetEnd="@id/end"
      motion:duration="250"
      motion:motionInterpolator="linear" />

  <ConstraintSet android:id="@+id/start">
  </ConstraintSet>

  <ConstraintSet android:id="@+id/end">
    <Constraint
        android:id="@id/homeSearchContainer"
        android:visibility="gone"/>
    <Constraint
        android:id="@id/collegeLayout"
        android:visibility="gone"/>
    <Constraint
        android:id="@id/homeCategoriesRv"
        android:visibility="gone"/>
    <Constraint
      android:id="@+id/tabContainer"
      android:visibility="gone"/>
    <Constraint
        android:id="@+id/btnAddPost"
        android:visibility="gone"/>
    <Constraint
        android:id="@id/homeContainer"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"/>
    <Constraint
        android:id="@id/homeVp"
        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent"/>
  </ConstraintSet>
  </MotionScene>

在我的 HomeActivity.kt 我调用motionlayout转换如下

fun hideViews() {
    binding.homeParent.transitionToEnd()
}

fun showViews() {
    binding.homeParent.transitionToStart()
}

我尝试了以下两件事:

motion:layoutDuringTransition="honorRequest"
binding.homeCategoriesRv.scrollBy(0,0)

请帮忙。任何想法,将不胜感激!

标签: androidkotlinandroid-recyclerviewandroid-motionlayout

解决方案


推荐阅读