Skip to content

Commit 4bd5e61

Browse files
paulfthomasraajkumars
authored andcommitted
[Catalog][Card] Fix CardSelectionModeActivity sample
Resolves #2716 Resolves #2717 PiperOrigin-RevId: 502659028
1 parent 7f1def6 commit 4bd5e61

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

catalog/java/io/material/catalog/card/SelectableCardsAdapter.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import android.widget.TextView;
2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.Nullable;
29+
import androidx.core.view.ViewCompat;
2930
import androidx.recyclerview.selection.ItemDetailsLookup;
3031
import androidx.recyclerview.selection.ItemKeyProvider;
3132
import androidx.recyclerview.selection.SelectionTracker;
3233
import com.google.android.material.card.MaterialCardView;
3334
import java.util.ArrayList;
34-
import java.util.Arrays;
3535
import java.util.List;
3636

3737
/** An Adapter that works with a collection of selectable card items */
@@ -99,17 +99,31 @@ private void bind(Item item, int position) {
9999
if (selectionTracker != null) {
100100
bindSelectedState();
101101
}
102-
// Set an OnLongClickListener for accessibility
103-
materialCardView.setOnLongClickListener(
104-
v -> {
105-
selectionTracker.setItemsSelected(
106-
Arrays.asList(details.getSelectionKey()), !materialCardView.isChecked());
107-
return true;
108-
});
109102
}
110103

111104
private void bindSelectedState() {
112-
materialCardView.setChecked(selectionTracker.isSelected(details.getSelectionKey()));
105+
Long selectionKey = details.getSelectionKey();
106+
materialCardView.setChecked(selectionTracker.isSelected(selectionKey));
107+
if (selectionKey != null) {
108+
addAccessibilityActions(selectionKey);
109+
}
110+
}
111+
112+
private void addAccessibilityActions(@NonNull Long selectionKey) {
113+
ViewCompat.addAccessibilityAction(
114+
materialCardView,
115+
materialCardView.getContext().getString(R.string.cat_card_action_select),
116+
(view, arguments) -> {
117+
selectionTracker.select(selectionKey);
118+
return true;
119+
});
120+
ViewCompat.addAccessibilityAction(
121+
materialCardView,
122+
materialCardView.getContext().getString(R.string.cat_card_action_deselect),
123+
(view, arguments) -> {
124+
selectionTracker.deselect(selectionKey);
125+
return true;
126+
});
113127
}
114128

115129
ItemDetailsLookup.ItemDetails<Long> getItemDetails() {

catalog/java/io/material/catalog/card/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
name="cat_card_action_move_bottom_right">Move to bottom right</string>
104104
<string description="A description of an action that a user of an accessibility service can perform to move the card to the center of the screen. [CHAR LIMIT=25]"
105105
name="cat_card_action_move_center">Move to center</string>
106+
<string description="A description of an action that a user of an accessibility service can perform to select the card. [CHAR LIMIT=25]"
107+
name="cat_card_action_select">Select</string>
108+
<string description="A description of an action that a user of an accessibility service can perform to deselect the card. [CHAR LIMIT=25]"
109+
name="cat_card_action_deselect">Deselect</string>
106110

107111
<string description="a transient text that appears on screen when a card is dismissed [CHAR LIMIT=30]"
108112
name="cat_card_dismissed">Card Dismissed</string>

0 commit comments

Comments
 (0)