从Recycelview到Activity的共享元素转换



请告诉我为什么会发生这种情况?我做错了什么?屏幕视频

侦听器

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));
}

主要活动

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

详细活动

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;
}
}

最新更新