从 Recyclerview 到 Activity 的共享元素转换

Shared Element Transition from Recycelrview to Activity

请告诉我为什么会这样?我究竟做错了什么? screen video

听众

public interface NoteListener {
void onNoteClicked(Note note, int position, RelativeLayout noteLayout);

}

适配器

NoteViewHolder(@NonNull View itemView) {
        ...
        relativeLayoutNote = itemView.findViewById(R.id.rl_note);

        itemView.setOnClickListener(v -> noteListener.onNoteClicked(sortedList.get(getAdapterPosition()),
                getAdapterPosition(),  relativeLayoutNote));
    }

主要Activity

@Override
public void onNoteClicked(Note note, int position, RelativeLayout noteLayout) {
    if (!isClick) {
        DetailNoteActivity.start(this, note, findViewById(R.id.rl_note));
        isClick = true;
    }
}

详情Activity

public static void start(Activity caller, Note note, RelativeLayout noteLayout) {
    Intent intent = new Intent(caller, DetailNoteActivity.class);
    ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(
            caller, noteLayout, "note");
    if (note != null) {
        intent.putExtra(NOTE, note);
    }
    caller.startActivity(intent, options.toBundle());
}

item_note

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:background="@drawable/background_note"
android:transitionName="note">...

activity_detail

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:transitionName="note"
        android:animateLayoutChanges="true">...

我决定采用以下方式,可能比较笨拙,如果有其他方式请写下。向 MainActivity 添加了以下内容:

public class MainActivity extends AppCompatActivity implements NoteListener {
...
private RelativeLayout noteLayout;
...
@SuppressLint("SupportAnnotationUsage")
@AnimRes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    noteLayout = findViewById(R.id.rl_note);
    ...

@Override
public void onNoteClicked(Note note, RelativeLayout noteLayout) {
    this.noteLayout = noteLayout;
    if (!isClick) {
        DetailNoteActivity.start(this, note, noteLayout);
        isClick = true;
    }
}