Skip to content

Commit 3e5a7fd

Browse files
imhappipekingme
authored andcommitted
[Lists] Added onSwipeStateChanged callback, drawable swiped state for ListCardView, and alpha changes to swiped view as it reaches the swipe-to-action state
PiperOrigin-RevId: 833409689
1 parent 3c1a61c commit 3e5a7fd

File tree

7 files changed

+52
-6
lines changed

7 files changed

+52
-6
lines changed

lib/java/com/google/android/material/listitem/ListItemCardView.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
*/
3636
public class ListItemCardView extends MaterialCardView implements SwipeableListItem {
3737

38+
private static final int[] SWIPED_STATE_SET = {R.attr.state_swiped};
39+
40+
private boolean isSwiped = false;
41+
3842
private final int swipeMaxOvershoot;
3943
private boolean swipeToPrimaryActionEnabled;
4044

@@ -84,4 +88,19 @@ public void setSwipeToPrimaryActionEnabled(boolean swipeToPrimaryActionEnabled)
8488
public boolean isSwipeToPrimaryActionEnabled() {
8589
return swipeToPrimaryActionEnabled;
8690
}
91+
92+
@Override
93+
protected int[] onCreateDrawableState(int extraSpace) {
94+
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
95+
if (isSwiped) {
96+
mergeDrawableStates(drawableState, SWIPED_STATE_SET);
97+
}
98+
return drawableState;
99+
}
100+
101+
@Override
102+
public void onSwipeStateChanged(int swipeState) {
103+
isSwiped = swipeState != STATE_CLOSED;
104+
refreshDrawableState();
105+
}
87106
}

lib/java/com/google/android/material/listitem/ListItemLayout.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ public void onViewPositionChanged(
327327
- revealViewLp.rightMargin);
328328
((RevealableListItem) swipeToRevealLayout)
329329
.setRevealedWidth(revealViewDesiredWidth);
330+
331+
int fullSwipedOffset = getSwipeToActionOffset();
332+
int disappearingOffset =
333+
(fullSwipedOffset + getSwipeRevealViewRevealedOffset()) / 2;
334+
// If we're past the reveal view offset, we should start disappearing.
335+
if (revealViewOffset <= disappearingOffset) {
336+
contentView.setAlpha(
337+
(float) (revealViewOffset - fullSwipedOffset)
338+
/ (disappearingOffset - fullSwipedOffset));
339+
}
330340
}
331341

332342
@Override
@@ -475,11 +485,14 @@ private void startSettling(View contentView, @StableSwipeState int targetSwipeSt
475485
}
476486

477487
private void setSwipeStateInternal(@SwipeState int swipeState) {
488+
if (swipeState == this.swipeState) {
489+
return;
490+
}
478491
// If swipe to action is not supported but the swipe state to be set in
479492
// STATE_SWIPE_PRIMARY_ACTION, we do nothing.
480-
if (swipeState == STATE_SWIPE_PRIMARY_ACTION
481-
&& !(contentView instanceof SwipeableListItem
482-
&& ((SwipeableListItem) contentView).isSwipeToPrimaryActionEnabled())) {
493+
if (!(contentView instanceof SwipeableListItem)
494+
|| (swipeState == STATE_SWIPE_PRIMARY_ACTION
495+
&& !((SwipeableListItem) contentView).isSwipeToPrimaryActionEnabled())) {
483496
return;
484497
}
485498
this.swipeState = swipeState;
@@ -488,6 +501,8 @@ private void setSwipeStateInternal(@SwipeState int swipeState) {
488501
|| swipeState == STATE_SWIPE_PRIMARY_ACTION) {
489502
this.lastStableSwipeState = swipeState;
490503
}
504+
505+
((SwipeableListItem) contentView).onSwipeStateChanged(swipeState);
491506
}
492507

493508
@Override

lib/java/com/google/android/material/listitem/SwipeableListItem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public interface SwipeableListItem {
8080
@Retention(RetentionPolicy.SOURCE)
8181
@interface StableSwipeState {}
8282

83+
/** Called whenever the swipe state of the SwipeableListItem changes. */
84+
void onSwipeStateChanged(@SwipeState int swipeState);
85+
8386
/**
8487
* Returns the overshoot, in pixels, that the SwipeableListItem is able to be swiped past the
8588
* {@link #STATE_OPEN} or {@link #STATE_SWIPE_PRIMARY_ACTION} states by, before settling.

lib/java/com/google/android/material/listitem/res-public/values/public.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<public name="listItemCardViewStyle" type="attr"/>
2020
<public name="listItemCardViewSegmentedStyle" type="attr"/>
2121

22+
<public name="state_swiped" type="attr" />
23+
2224
<public name="Widget.Material3.ListItemLayout" type="style"/>
2325
<public name="ThemeOverlay.Material3.ListItemLayout.Segmented" type="style"/>
2426
<public name="Widget.Material3.ListItemCardView" type="style"/>

lib/java/com/google/android/material/listitem/res/color/m3_list_item_background_color_selector.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<selector xmlns:android="http://schemas.android.com/apk/res/android">
17+
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
1818
<item android:state_checkable="true" android:state_checked="true"
1919
android:color="?attr/colorSecondaryContainer"/>
20+
<item app:state_swiped="true" android:color="?attr/colorSurface"/>
2021
<item android:color="@android:color/transparent"/>
2122
</selector>

lib/java/com/google/android/material/listitem/res/color/m3_segmented_list_item_background_color_selector.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<selector xmlns:android="http://schemas.android.com/apk/res/android">
17+
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
1818
<item android:state_checkable="true" android:state_checked="true"
1919
android:color="?attr/colorSecondaryContainer"/>
20-
<item android:color="?attr/colorSurfaceBright"/>
20+
<item app:state_swiped="true" android:color="?attr/colorSurface"/>
21+
<item android:color="?attr/colorSurface"/>
2122
</selector>

lib/java/com/google/android/material/listitem/res/values/attrs.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<attr name="swipeToPrimaryActionEnabled" format="boolean"/>
2828
</declare-styleable>
2929

30+
<declare-styleable name="ListItemCardViewStates">
31+
<!-- State when a ListItemCardView is being swiped. -->
32+
<attr name="state_swiped" format="boolean" />
33+
</declare-styleable>
34+
3035
<!-- Shape appearance of a single item in the list. -->
3136
<attr name="listItemShapeAppearanceSingle" format="reference"/>
3237
<!-- Shape appearance of the first item in the list. -->

0 commit comments

Comments
 (0)