Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MaterialContainerTransform] Add support for predictive back #4248

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.core.view.OneShotPreDrawListener;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
Expand All @@ -40,8 +42,8 @@
import androidx.core.view.ViewCompat;
import androidx.transition.Transition;
import androidx.transition.TransitionManager;
import com.google.android.material.transition.Hold;
import com.google.android.material.transition.MaterialContainerTransform;
import com.google.android.material.transition.MaterialFade;
import com.google.android.material.transition.MaterialFadeThrough;
import com.google.android.material.transition.MaterialSharedAxis;
import io.material.catalog.musicplayer.AlbumsAdapter.AlbumAdapterListener;
Expand Down Expand Up @@ -83,6 +85,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
toolbar.setOnMenuItemClickListener(this);
MaterialSharedAxis sharedAxis = new MaterialSharedAxis(MaterialSharedAxis.Z, true);
setList(listTypeGrid, listSorted, sharedAxis);

// Wait until all views in the entering fragment have been measured and laid out
postponeEnterTransition();
OneShotPreDrawListener.add((ViewGroup) view.getParent(), this::startPostponedEnterTransition);
}

@Override
Expand All @@ -101,16 +107,7 @@ public void onAlbumClicked(View view, Album album) {
MaterialContainerTransform transform =
new MaterialContainerTransform(requireContext(), /* entering= */ true);
fragment.setSharedElementEnterTransition(transform);

// Use a Hold transition to keep this fragment visible beneath the MaterialContainerTransform
// that transitions to the album details screen. Without a Hold, this fragment would disappear
// as soon its container is replaced with a new Fragment.
Hold hold = new Hold();
// Add root view as target for the Hold so that the entire view hierarchy is held in place as
// one instead of each child view individually. Helps keep shadows during the transition.
hold.addTarget(R.id.container);
hold.setDuration(transform.getDuration());
setExitTransition(hold);
fragment.setEnterTransition(new MaterialFade());

getParentFragmentManager()
.beginTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,21 +972,11 @@ public void onAnimationUpdate(ValueAnimator animation) {
}
});

addListener(
new TransitionListenerAdapter() {
@Override
public void onTransitionStart(@NonNull Transition transition) {
// Add the transition drawable to the root ViewOverlay
ViewUtils.getOverlay(drawingView).add(transitionDrawable);

// Hide the actual views at the beginning of the transition
startView.setAlpha(0);
endView.setAlpha(0);
}
animator.addListener(
new Animator.AnimatorListener() {

@Override
public void onTransitionEnd(@NonNull Transition transition) {
removeListener(this);
void showTransitionEnd() {
animator.removeListener(this);
if (holdAtEndEnabled) {
// Keep drawable showing and views hidden (useful for Activity return transitions)
return;
Expand All @@ -998,6 +988,29 @@ public void onTransitionEnd(@NonNull Transition transition) {
// Remove the transition drawable from the root ViewOverlay
ViewUtils.getOverlay(drawingView).remove(transitionDrawable);
}

@Override
public void onAnimationStart(@NonNull Animator animation) {
// Add the transition drawable to the root ViewOverlay
ViewUtils.getOverlay(drawingView).add(transitionDrawable);

// Hide the actual views at the beginning of the transition
startView.setAlpha(0);
endView.setAlpha(0);
}

@Override
public void onAnimationEnd(@NonNull Animator animation) {
showTransitionEnd();
}

@Override
public void onAnimationCancel(@NonNull Animator animation) {
showTransitionEnd();
}

@Override
public void onAnimationRepeat(@NonNull Animator animation) {}
});

return animator;
Expand Down Expand Up @@ -1573,4 +1586,9 @@ private ProgressThresholdsGroup(
this.shapeMask = shapeMask;
}
}

@Override
public boolean isSeekingSupported() {
return true;
}
}