Skip to content

Commit

Permalink
[Catalog][Card] Fix CardSelectionModeActivity sample
Browse files Browse the repository at this point in the history
Resolves #2716
Resolves #2717

PiperOrigin-RevId: 502659028
  • Loading branch information
paulfthomas authored and raajkumars committed Jan 18, 2023
1 parent 7f1def6 commit 4bd5e61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
32 changes: 23 additions & 9 deletions catalog/java/io/material/catalog/card/SelectableCardsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.selection.ItemDetailsLookup;
import androidx.recyclerview.selection.ItemKeyProvider;
import androidx.recyclerview.selection.SelectionTracker;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** An Adapter that works with a collection of selectable card items */
Expand Down Expand Up @@ -99,17 +99,31 @@ private void bind(Item item, int position) {
if (selectionTracker != null) {
bindSelectedState();
}
// Set an OnLongClickListener for accessibility
materialCardView.setOnLongClickListener(
v -> {
selectionTracker.setItemsSelected(
Arrays.asList(details.getSelectionKey()), !materialCardView.isChecked());
return true;
});
}

private void bindSelectedState() {
materialCardView.setChecked(selectionTracker.isSelected(details.getSelectionKey()));
Long selectionKey = details.getSelectionKey();
materialCardView.setChecked(selectionTracker.isSelected(selectionKey));
if (selectionKey != null) {
addAccessibilityActions(selectionKey);
}
}

private void addAccessibilityActions(@NonNull Long selectionKey) {
ViewCompat.addAccessibilityAction(
materialCardView,
materialCardView.getContext().getString(R.string.cat_card_action_select),
(view, arguments) -> {
selectionTracker.select(selectionKey);
return true;
});
ViewCompat.addAccessibilityAction(
materialCardView,
materialCardView.getContext().getString(R.string.cat_card_action_deselect),
(view, arguments) -> {
selectionTracker.deselect(selectionKey);
return true;
});
}

ItemDetailsLookup.ItemDetails<Long> getItemDetails() {
Expand Down
4 changes: 4 additions & 0 deletions catalog/java/io/material/catalog/card/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
name="cat_card_action_move_bottom_right">Move to bottom right</string>
<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]"
name="cat_card_action_move_center">Move to center</string>
<string description="A description of an action that a user of an accessibility service can perform to select the card. [CHAR LIMIT=25]"
name="cat_card_action_select">Select</string>
<string description="A description of an action that a user of an accessibility service can perform to deselect the card. [CHAR LIMIT=25]"
name="cat_card_action_deselect">Deselect</string>

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

0 comments on commit 4bd5e61

Please sign in to comment.