Skip to content

Commit 6092a7d

Browse files
afohrmanhunterstich
authored andcommittedMar 21, 2024
[Predictive Transitions] Add predictive fade through fragment transition demo to Catalog.
PiperOrigin-RevId: 602495276 (cherry picked from commit 5557ec3)
1 parent ccf1e7a commit 6092a7d

File tree

1 file changed

+61
-13
lines changed

1 file changed

+61
-13
lines changed
 

‎catalog/java/io/material/catalog/transition/TransitionFadeThroughDemoFragment.java

+61-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import android.os.Bundle;
2222
import androidx.fragment.app.Fragment;
23+
import androidx.fragment.app.FragmentManager;
24+
import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks;
25+
import androidx.fragment.app.FragmentTransaction;
2326
import android.util.SparseIntArray;
2427
import android.view.LayoutInflater;
2528
import android.view.View;
@@ -29,6 +32,7 @@
2932
import androidx.annotation.NonNull;
3033
import androidx.annotation.Nullable;
3134
import com.google.android.material.bottomnavigation.BottomNavigationView;
35+
import com.google.android.material.navigation.NavigationBarView;
3236
import com.google.android.material.transition.MaterialFadeThrough;
3337
import io.material.catalog.feature.DemoFragment;
3438

@@ -37,6 +41,12 @@ public class TransitionFadeThroughDemoFragment extends DemoFragment {
3741

3842
private static final SparseIntArray LAYOUT_RES_MAP = new SparseIntArray();
3943

44+
private final NavigationBarView.OnItemSelectedListener onItemSelectedListener =
45+
item -> {
46+
replaceFragment(item.getItemId(), /* addToBackStack= */ true);
47+
return true;
48+
};
49+
4050
static {
4151
LAYOUT_RES_MAP.append(R.id.action_albums, R.layout.cat_transition_fade_through_albums_fragment);
4252
LAYOUT_RES_MAP.append(R.id.action_photos, R.layout.cat_transition_fade_through_photos_fragment);
@@ -51,35 +61,73 @@ public View onCreateDemoView(
5161

5262
@Override
5363
public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
64+
BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomnavigation);
65+
bottomNavigationView.setOnItemSelectedListener(onItemSelectedListener);
66+
67+
requireActivity()
68+
.getSupportFragmentManager()
69+
.registerFragmentLifecycleCallbacks(
70+
new FragmentLifecycleCallbacks() {
71+
@Override
72+
public void onFragmentStarted(
73+
@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment) {
74+
super.onFragmentStarted(fragmentManager, fragment);
75+
Integer itemId = getItemIdFromFragmentTag(fragment.getTag());
76+
if (itemId != null && bottomNavigationView.getSelectedItemId() != itemId) {
77+
// Workaround to avoid breaking the demo by recreating the fragment on back,
78+
// since the FragmentManager handles replacing the fragment instead.
79+
bottomNavigationView.setOnItemSelectedListener(null);
80+
bottomNavigationView.setSelectedItemId(itemId);
81+
bottomNavigationView.setOnItemSelectedListener(onItemSelectedListener);
82+
}
83+
}
84+
},
85+
true);
86+
replaceFragment(R.id.action_albums, /* addToBackStack= */ false);
87+
}
5488

55-
replaceFragment(R.id.action_albums);
89+
@Nullable
90+
private Integer getItemIdFromFragmentTag(@Nullable String fragmentTag) {
91+
try {
92+
if (fragmentTag != null) {
93+
return Integer.parseInt(fragmentTag);
94+
}
95+
} catch (NumberFormatException numberFormatException) {
96+
// Ignore; we only care about TransitionSimpleLayoutFragments with item id tags.
97+
}
98+
return null;
99+
}
56100

57-
BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomnavigation);
58-
bottomNavigationView.setOnNavigationItemSelectedListener(
59-
item -> {
60-
replaceFragment(item.getItemId());
61-
return true;
62-
});
101+
@NonNull
102+
private String convertItemIdToFragmentTag(@IdRes int itemId) {
103+
return String.valueOf(itemId);
63104
}
64105

65106
@LayoutRes
66107
private static int getLayoutForItemId(@IdRes int itemId) {
67108
return LAYOUT_RES_MAP.get(itemId);
68109
}
69110

70-
private void replaceFragment(@IdRes int itemId) {
111+
private void replaceFragment(@IdRes int itemId, boolean addToBackStack) {
71112
Fragment fragment = TransitionSimpleLayoutFragment.newInstance(getLayoutForItemId(itemId));
72113
// Set the transition as the Fragment's enter transition. This will be used when the fragment
73114
// is added to the container and re-used when the fragment is removed from the container.
74115
fragment.setEnterTransition(createTransition());
75116

76-
getChildFragmentManager()
77-
.beginTransaction()
78-
.replace(R.id.fragment_container, fragment)
79-
.commit();
117+
FragmentTransaction fragmentTransaction =
118+
requireActivity()
119+
.getSupportFragmentManager()
120+
.beginTransaction()
121+
.setReorderingAllowed(true)
122+
.replace(R.id.fragment_container, fragment, convertItemIdToFragmentTag(itemId));
123+
124+
if (addToBackStack) {
125+
fragmentTransaction.addToBackStack(convertItemIdToFragmentTag(itemId));
126+
}
127+
fragmentTransaction.commit();
80128
}
81129

82-
private MaterialFadeThrough createTransition(){
130+
private MaterialFadeThrough createTransition() {
83131
MaterialFadeThrough fadeThrough = new MaterialFadeThrough();
84132

85133
// Add targets for this transition to explicitly run transitions only on these views. Without

0 commit comments

Comments
 (0)
Please sign in to comment.