MotionLayout 我无法将我的 RecyclerView 滚动到第一个位置

MotionLayout i can't scroll my RecyclerView to first poisiton

我有一个 activity_motion_sec.xml 文件如下。与此示例https://riggaroo.dev/android-motionlayout-tutorial-collapsing-view/ 几乎相同。我添加的是一个简单的向上箭头按钮。我希望我的 recyclerview 在按下向上箭头按钮时滚动到 0. 位置。

activity_motion_sec.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutDescription="@xml/base_scroll_scene"
app:showPaths="false">

<ImageView
    android:id="@+id/imageViewAvatar"
    android:layout_width="140dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:scaleType="centerCrop"
    app:layout_constraintDimensionRatio="h,1:1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:src="@drawable/bursa" />


<TextView
    android:id="@+id/textViewName"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    android:fontFamily="@font/raleway_semibold"
    android:text="Veruca Salt "
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    app:layout_constraintBottom_toTopOf="@+id/space"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageViewAvatar"
    app:layout_constraintTop_toTopOf="@+id/imageViewAvatar" />


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewStatus"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageViewAvatar"
    >

</androidx.recyclerview.widget.RecyclerView>


<View
    android:id="@+id/my_view"
    android:layout_width="0dp"
    android:layout_height="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@id/space"
    app:layout_constraintStart_toStartOf="parent"
    android:background="@drawable/bottom_gradient_mask"
    android:visibility="gone"/>

<ImageView
    android:id="@+id/up_arrow"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_arrow_upward_black_24dp"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="@+id/space"
    app:layout_constraintTop_toTopOf="@+id/space"/>


<Space
    android:id="@+id/space"
    android:layout_width="match_parent"
    android:layout_height="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/imageViewAvatar"
    app:layout_constraintTop_toBottomOf="@id/imageViewAvatar" />

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

在base_scroll_scene.xml文件中,我可以处理向上箭头按钮的点击事件。单击按钮时动画运行,但我无法设置 recyclerview 位置。

base_scroll_scene.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">

<Transition
    app:constraintSetEnd="@id/end"
    app:constraintSetStart="@id/start"
    app:duration="1000">
    <OnSwipe
        app:dragDirection="dragUp"
        app:touchAnchorId="@+id/recyclerViewStatus"
        app:touchAnchorSide="top" />
    <OnClick
        app:targetId="@id/up_arrow"
        app:clickAction="transitionToStart"/>
</Transition>

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

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

    <Constraint
        android:id="@id/imageViewAvatar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <Constraint
        android:id="@id/space"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/imageViewAvatar"
        app:layout_constraintTop_toBottomOf="@id/imageViewAvatar"/>

    <Constraint
        android:id="@+id/up_arrow"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_arrow_upward_black_24dp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="@+id/space"
        app:layout_constraintTop_toTopOf="@+id/space"/>

    <Constraint
        android:id="@+id/my_view"
        android:layout_width="0dp"
        android:layout_height="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/space"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@drawable/bottom_gradient_mask"
        android:visibility="visible"/>

</ConstraintSet>

</MotionScene>

我试图在我的 java 文件中设置一个 ClickListener,但没有成功。该视图不监听我在 MotionActivitySec.java 中设置的事件监听器 和 MotionActivitySec.java

public class MotionActivitySec extends Activity {


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_motion_sec);

    MyListData[] myListData = new MyListData[] {
            new MyListData("Email", android.R.drawable.ic_dialog_email),
            new MyListData("Info", android.R.drawable.ic_dialog_info),
            new MyListData("Delete", android.R.drawable.ic_delete),
            new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
            new MyListData("Alert", android.R.drawable.ic_dialog_alert),
            new MyListData("Map", android.R.drawable.ic_dialog_map),
            new MyListData("Email", android.R.drawable.ic_dialog_email),
            new MyListData("Info", android.R.drawable.ic_dialog_info),
            new MyListData("Delete", android.R.drawable.ic_delete),
            new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
            new MyListData("Alert", android.R.drawable.ic_dialog_alert),
            new MyListData("Map", android.R.drawable.ic_dialog_map),
    };

    final RecyclerView recyclerView = findViewById(R.id.recyclerViewStatus);
    MyAdapter adapter = new MyAdapter(myListData);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    ImageView imageView = findViewById(R.id.up_arrow);

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MotionActivitySec.this, "clicked", Toast.LENGTH_SHORT).show();
            recyclerView.scrollToPosition(0);
        }
    });
    
}
}

进行如下操作:

1.Remove

<OnClick
    app:targetId="@id/up_arrow"
    app:clickAction="transitionToStart"/>

2.In activity,处理按钮点击本身

 upArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                root.transitionToEnd();
                //root.transitionToStart();
                recyclerViewStatus.getLayoutManager().scrollToPosition(0);
            }
        });