Skip to content

Commit

Permalink
[Predictive Transitions] Added predictive transition support for frag…
Browse files Browse the repository at this point in the history
…ments and views to the Material motion library, and enabled it in shared axis fragment transition demo.

This allows all MaterialVisibility transitions to be predictive, although each individual transition must be opted-in in order to be predictive/seekable.

Resolves #3619

PiperOrigin-RevId: 587108521
  • Loading branch information
afohrman committed Dec 4, 2023
1 parent dfa474f commit 8ccec33
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ext {
recyclerViewSelection : '1.0.0',
resourceInspectionAnnotation : '1.0.1',
resourceInspectionProcessor : '1.0.1',
transition : '1.2.0',
transition : '1.5.0-alpha04',
vectorDrawable : '1.1.0',
viewpager2 : '1.0.0',
dynamicanimation : '1.0.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.transition.Transition;
import androidx.transition.TransitionListenerAdapter;
import com.google.android.material.transition.MaterialSharedAxis;
import io.material.catalog.feature.DemoFragment;

Expand All @@ -36,14 +38,6 @@ public class TransitionSharedAxisDemoFragment extends DemoFragment {
private static final int LAYOUT_RES_ID_START = R.layout.cat_transition_shared_axis_start;
private static final int LAYOUT_RES_ID_END = R.layout.cat_transition_shared_axis_end;

private final OnBackPressedCallback onBackPressedCallback =
new OnBackPressedCallback(/* enabled= */ false) {
@Override
public void handleOnBackPressed() {
replaceFragment(LAYOUT_RES_ID_START);
}
};

private SharedAxisHelper sharedAxisHelper;

@Override
Expand All @@ -58,12 +52,18 @@ public View onCreateDemoView(
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
sharedAxisHelper = new SharedAxisHelper(view.findViewById(R.id.controls_layout));
requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);

replaceFragment(LAYOUT_RES_ID_START);
Fragment fragment = TransitionSimpleLayoutFragment.newInstance(LAYOUT_RES_ID_START);

requireActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();

sharedAxisHelper.setBackButtonOnClickListener(v -> replaceFragment(LAYOUT_RES_ID_START));
sharedAxisHelper.setNextButtonOnClickListener(v -> replaceFragment(LAYOUT_RES_ID_END));
sharedAxisHelper.updateButtonsEnabled(true);
}

private void replaceFragment(@LayoutRes int layoutResId) {
Expand All @@ -73,14 +73,28 @@ private void replaceFragment(@LayoutRes int layoutResId) {
// Set the transition as the Fragment's enter transition. This will be used when the fragment
// is added to the container and re-used when the fragment is removed from the container.
fragment.setEnterTransition(createTransition(entering));
if (entering) {
fragment.setReturnTransition(createTransition(false));
} else {
// Pop the backstack if manually transitioning to the start fragment to remove the end
// fragment from the back stack without a back event.
requireActivity().getSupportFragmentManager().popBackStack();
}

getFragmentTransaction(fragment, entering).commit();
}

getChildFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
private FragmentTransaction getFragmentTransaction(@NonNull Fragment fragment, boolean entering) {
return entering
? getFragmentTransaction(fragment).addToBackStack(/* name= */ null)
: getFragmentTransaction(fragment);
}

sharedAxisHelper.updateButtonsEnabled(!entering);
onBackPressedCallback.setEnabled(entering);
private FragmentTransaction getFragmentTransaction(@NonNull Fragment fragment) {
return requireActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment);
}

private MaterialSharedAxis createTransition(boolean entering) {
Expand All @@ -92,6 +106,18 @@ private MaterialSharedAxis createTransition(boolean entering) {
// Fragment's layout.
transition.addTarget(R.id.start_root);
transition.addTarget(R.id.end_root);
transition.addListener(
new TransitionListenerAdapter() {
@Override
public void onTransitionStart(@NonNull Transition transition) {
sharedAxisHelper.updateButtonsEnabled(!entering);
}

@Override
public void onTransitionCancel(@NonNull Transition transition) {
sharedAxisHelper.updateButtonsEnabled(entering);
}
});

return transition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,10 @@ int getEasingThemeAttrResId(boolean appearing) {
TimeInterpolator getDefaultEasingInterpolator(boolean appearing) {
return AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR;
}

// STRIP-FROM-PLATFORM-TRANSITIONS-PACKAGE
@Override // STRIP-FROM-PLATFORM-TRANSITIONS-PACKAGE
public boolean isSeekingSupported() { // STRIP-FROM-PLATFORM-TRANSITIONS-PACKAGE
return true; // STRIP-FROM-PLATFORM-TRANSITIONS-PACKAGE
} // STRIP-FROM-PLATFORM-TRANSITIONS-PACKAGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,10 @@ int getEasingThemeAttrResId(boolean appearing) {
TimeInterpolator getDefaultEasingInterpolator(boolean appearing) {
return AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR;
}






}

0 comments on commit 8ccec33

Please sign in to comment.