Search code examples
javaandroidandroid-recyclerviewadaptertransition

Shared Element Transition from Recycelrview to Activity


Please tell me why this is happening? What am I doing wrong? screen video

Listener

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

}

Adapter

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

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

MainActivity

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

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

Solution

  • I decided in the following way, possibly clumsy, if there is another way please write. Added the following to 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;
        }
    }