Skip to content

Commit b984e64

Browse files
afohrmanleticiarossi
authored andcommitted
[Predictive Back] [Side Sheet] Added handleBackInvoked() predictive back support for coplanar side sheets.
Added an AnimatorUpdateListener that runs in finishBackProgress() to make the coplanar sibling's margins update as the sheet slides back to its origin edge. PiperOrigin-RevId: 527749916
1 parent 824d75e commit b984e64

File tree

5 files changed

+68
-18
lines changed

5 files changed

+68
-18
lines changed

catalog/java/io/material/catalog/sidesheet/SideSheetMainDemoFragment.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ public View onCreateDemoView(
100100
view,
101101
R.id.standard_side_sheet_container,
102102
R.id.show_standard_side_sheet_button,
103-
R.id.close_icon_button,
104-
/* shouldHandleBack= */ true);
103+
R.id.close_icon_button);
105104

106105
setSideSheetCallback(
107106
standardRightSideSheet, R.id.side_sheet_state_text, R.id.side_sheet_slide_offset_text);
@@ -112,8 +111,7 @@ public View onCreateDemoView(
112111
view,
113112
R.id.standard_detached_side_sheet_container,
114113
R.id.show_standard_detached_side_sheet_button,
115-
R.id.detached_close_icon_button,
116-
/* shouldHandleBack= */ true);
114+
R.id.detached_close_icon_button);
117115

118116
setSideSheetCallback(
119117
detachedStandardSideSheet,
@@ -126,8 +124,7 @@ public View onCreateDemoView(
126124
view,
127125
R.id.vertically_scrolling_side_sheet_container,
128126
R.id.show_vertically_scrolling_side_sheet_button,
129-
R.id.vertically_scrolling_side_sheet_close_icon_button,
130-
/* shouldHandleBack= */ true);
127+
R.id.vertically_scrolling_side_sheet_close_icon_button);
131128

132129
setSideSheetCallback(
133130
verticallyScrollingSideSheet,
@@ -148,8 +145,7 @@ public View onCreateDemoView(
148145
view,
149146
R.id.coplanar_side_sheet_container,
150147
R.id.show_coplanar_side_sheet_button,
151-
R.id.coplanar_side_sheet_close_icon_button,
152-
/* shouldHandleBack= */ false);
148+
R.id.coplanar_side_sheet_close_icon_button);
153149

154150
setSideSheetCallback(
155151
coplanarSideSheet,
@@ -162,8 +158,7 @@ public View onCreateDemoView(
162158
view,
163159
R.id.coplanar_detached_side_sheet_container,
164160
R.id.show_coplanar_detached_side_sheet_button,
165-
R.id.coplanar_detached_side_sheet_close_icon_button,
166-
/* shouldHandleBack= */ false);
161+
R.id.coplanar_detached_side_sheet_close_icon_button);
167162

168163
setSideSheetCallback(
169164
detachedCoplanarSideSheet,
@@ -224,8 +219,7 @@ private View setUpSideSheet(
224219
@NonNull View view,
225220
@IdRes int sideSheetContainerId,
226221
@IdRes int showSideSheetButtonId,
227-
@IdRes int closeIconButtonId,
228-
boolean shouldHandleBack) {
222+
@IdRes int closeIconButtonId) {
229223
View sideSheet = view.findViewById(sideSheetContainerId);
230224
SideSheetBehavior<View> sideSheetBehavior = SideSheetBehavior.from(sideSheet);
231225

@@ -235,9 +229,7 @@ private View setUpSideSheet(
235229
View standardSideSheetCloseIconButton = sideSheet.findViewById(closeIconButtonId);
236230
standardSideSheetCloseIconButton.setOnClickListener(v -> hideSideSheet(sideSheetBehavior));
237231

238-
if (shouldHandleBack) {
239-
setupBackHandling(sideSheetBehavior);
240-
}
232+
setupBackHandling(sideSheetBehavior);
241233

242234
sideSheetViews.add(sideSheet);
243235

lib/java/com/google/android/material/sidesheet/LeftSheetDelegate.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ void updateCoplanarSiblingLayoutParams(
102102
}
103103
}
104104

105+
@Override
106+
void updateCoplanarSiblingAdjacentMargin(
107+
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int coplanarSiblingAdjacentMargin) {
108+
coplanarSiblingLayoutParams.leftMargin = coplanarSiblingAdjacentMargin;
109+
}
110+
111+
@Override
112+
int getCoplanarSiblingAdjacentMargin(@NonNull MarginLayoutParams coplanarSiblingLayoutParams) {
113+
return coplanarSiblingLayoutParams.leftMargin;
114+
}
115+
105116
@Override
106117
public int getParentInnerEdge(@NonNull CoordinatorLayout parent) {
107118
return parent.getLeft();

lib/java/com/google/android/material/sidesheet/RightSheetDelegate.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ void updateCoplanarSiblingLayoutParams(
103103
}
104104
}
105105

106+
@Override
107+
void updateCoplanarSiblingAdjacentMargin(
108+
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int coplanarSiblingAdjacentMargin) {
109+
coplanarSiblingLayoutParams.rightMargin = coplanarSiblingAdjacentMargin;
110+
}
111+
112+
@Override
113+
int getCoplanarSiblingAdjacentMargin(@NonNull MarginLayoutParams coplanarSiblingLayoutParams) {
114+
return coplanarSiblingLayoutParams.rightMargin;
115+
}
116+
106117
@Override
107118
public int getParentInnerEdge(@NonNull CoordinatorLayout parent) {
108119
return parent.getRight();

lib/java/com/google/android/material/sidesheet/SheetDelegate.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ abstract class SheetDelegate {
7171
abstract void updateCoplanarSiblingLayoutParams(
7272
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int sheetLeft, int sheetRight);
7373

74+
/** Sets the coplanar sheet's margin that's adjacent to the side sheet to the provided value. */
75+
abstract void updateCoplanarSiblingAdjacentMargin(
76+
@NonNull MarginLayoutParams coplanarSiblingLayoutParams, int coplanarSiblingAdjacentMargin);
77+
78+
/**
79+
* Returns the coplanar sibling's margin that's adjacent to the sheet's left or right edge,
80+
* depending on the sheet edge.
81+
*/
82+
abstract int getCoplanarSiblingAdjacentMargin(
83+
@NonNull MarginLayoutParams coplanarSiblingLayoutParams);
84+
7485
/**
7586
* Calculates the margin on the inner side of the sheet based on the {@link MarginLayoutParams}.
7687
* For right based sheets, the inner margin would be the right margin.

lib/java/com/google/android/material/sidesheet/SideSheetBehavior.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import android.animation.Animator;
2525
import android.animation.AnimatorListenerAdapter;
26+
import android.animation.ValueAnimator.AnimatorUpdateListener;
2627
import android.content.Context;
2728
import android.content.res.ColorStateList;
2829
import android.content.res.TypedArray;
@@ -59,6 +60,7 @@
5960
import androidx.core.view.accessibility.AccessibilityViewCommand;
6061
import androidx.customview.view.AbsSavedState;
6162
import androidx.customview.widget.ViewDragHelper;
63+
import com.google.android.material.animation.AnimationUtils;
6264
import com.google.android.material.motion.MaterialSideContainerBackHelper;
6365
import com.google.android.material.resources.MaterialResources;
6466
import com.google.android.material.shape.MaterialShapeDrawable;
@@ -988,8 +990,7 @@ public void updateBackProgress(@NonNull BackEvent backEvent) {
988990
if (sideContainerBackHelper == null) {
989991
return;
990992
}
991-
sideContainerBackHelper.updateBackProgress(
992-
backEvent, getGravityFromSheetEdge());
993+
sideContainerBackHelper.updateBackProgress(backEvent, getGravityFromSheetEdge());
993994
}
994995

995996
@Override
@@ -1015,7 +1016,31 @@ public void onAnimationEnd(Animator animation) {
10151016
}
10161017
}
10171018
},
1018-
/* finishAnimatorUpdateListener= */ null);
1019+
getCoplanarFinishAnimatorUpdateListener());
1020+
}
1021+
1022+
@Nullable
1023+
private AnimatorUpdateListener getCoplanarFinishAnimatorUpdateListener() {
1024+
View coplanarSiblingView = getCoplanarSiblingView();
1025+
if (coplanarSiblingView == null) {
1026+
return null;
1027+
}
1028+
1029+
MarginLayoutParams coplanarSiblingLayoutParams =
1030+
(MarginLayoutParams) coplanarSiblingView.getLayoutParams();
1031+
if (coplanarSiblingLayoutParams == null) {
1032+
return null;
1033+
}
1034+
1035+
int coplanarSiblingAdjacentMargin =
1036+
sheetDelegate.getCoplanarSiblingAdjacentMargin(coplanarSiblingLayoutParams);
1037+
1038+
return animation -> {
1039+
sheetDelegate.updateCoplanarSiblingAdjacentMargin(
1040+
coplanarSiblingLayoutParams,
1041+
AnimationUtils.lerp(coplanarSiblingAdjacentMargin, 0, animation.getAnimatedFraction()));
1042+
coplanarSiblingView.requestLayout();
1043+
};
10191044
}
10201045

10211046
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)

0 commit comments

Comments
 (0)