|
26 | 26 | import android.widget.TextView; |
27 | 27 | import androidx.annotation.NonNull; |
28 | 28 | import androidx.annotation.Nullable; |
| 29 | +import androidx.core.view.ViewCompat; |
29 | 30 | import androidx.recyclerview.selection.ItemDetailsLookup; |
30 | 31 | import androidx.recyclerview.selection.ItemKeyProvider; |
31 | 32 | import androidx.recyclerview.selection.SelectionTracker; |
32 | 33 | import com.google.android.material.card.MaterialCardView; |
33 | 34 | import java.util.ArrayList; |
34 | | -import java.util.Arrays; |
35 | 35 | import java.util.List; |
36 | 36 |
|
37 | 37 | /** An Adapter that works with a collection of selectable card items */ |
@@ -99,17 +99,31 @@ private void bind(Item item, int position) { |
99 | 99 | if (selectionTracker != null) { |
100 | 100 | bindSelectedState(); |
101 | 101 | } |
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 | | - }); |
109 | 102 | } |
110 | 103 |
|
111 | 104 | 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 | + }); |
113 | 127 | } |
114 | 128 |
|
115 | 129 | ItemDetailsLookup.ItemDetails<Long> getItemDetails() { |
|
0 commit comments