Skip to content

Commit 6310274

Browse files
imhappidrchen
authored andcommitted
[List][Catalog] Address a11y issues
PiperOrigin-RevId: 834524481
1 parent 0153138 commit 6310274

6 files changed

Lines changed: 102 additions & 22 deletions

File tree

catalog/java/io/material/catalog/listitem/CustomListItemData.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package io.material.catalog.listitem;
1717

18-
/** A sample data class used to represent List Items **/
19-
class CustomListItemData {
18+
import android.os.Parcel;
19+
import android.os.Parcelable;
20+
21+
/** A sample data class used to represent List Items * */
22+
class CustomListItemData implements Parcelable {
2023
String text;
2124
boolean checked;
2225
int indexInSection;
@@ -32,4 +35,39 @@ public CustomListItemData(String text, int indexInSection, int sectionCount) {
3235
public CustomListItemData(String subheading) {
3336
this.subheading = subheading;
3437
}
38+
39+
protected CustomListItemData(Parcel in) {
40+
text = in.readString();
41+
checked = in.readByte() != 0;
42+
indexInSection = in.readInt();
43+
sectionCount = in.readInt();
44+
subheading = in.readString();
45+
}
46+
47+
@Override
48+
public void writeToParcel(Parcel dest, int flags) {
49+
dest.writeString(text);
50+
dest.writeByte((byte) (checked ? 1 : 0));
51+
dest.writeInt(indexInSection);
52+
dest.writeInt(sectionCount);
53+
dest.writeString(subheading);
54+
}
55+
56+
@Override
57+
public int describeContents() {
58+
return 0;
59+
}
60+
61+
public static final Creator<CustomListItemData> CREATOR =
62+
new Creator<CustomListItemData>() {
63+
@Override
64+
public CustomListItemData createFromParcel(Parcel in) {
65+
return new CustomListItemData(in);
66+
}
67+
68+
@Override
69+
public CustomListItemData[] newArray(int size) {
70+
return new CustomListItemData[size];
71+
}
72+
};
3573
}

catalog/java/io/material/catalog/listitem/ListsMainDemoFragment.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646

4747
/** A fragment that displays the main List demos for the Catalog app. */
4848
public class ListsMainDemoFragment extends DemoFragment {
49+
50+
private static final String KEY_LIST_DATA = "key_list_data";
51+
private ArrayList<CustomListItemData> listData;
52+
4953
@NonNull
5054
@Override
5155
public View onCreateDemoView(
@@ -56,16 +60,20 @@ public View onCreateDemoView(
5660
(RecyclerView) layoutInflater.inflate(R.layout.cat_lists_fragment, viewGroup, false);
5761

5862
view.setLayoutManager(new LinearLayoutManager(getContext()));
59-
List<CustomListItemData> data = new ArrayList<>();
60-
for (int i = 0; i < 20; i++) {
61-
data.add(
62-
new CustomListItemData(
63-
String.format(view.getContext().getString(R.string.cat_list_item_text), i + 1),
64-
i,
65-
20));
63+
if (bundle != null) {
64+
listData = bundle.getParcelableArrayList(KEY_LIST_DATA);
65+
} else {
66+
listData = new ArrayList<>();
67+
for (int i = 0; i < 20; i++) {
68+
listData.add(
69+
new CustomListItemData(
70+
String.format(view.getContext().getString(R.string.cat_list_item_text), i + 1),
71+
i,
72+
20));
73+
}
6674
}
6775

68-
view.setAdapter(new ListsAdapter(data));
76+
view.setAdapter(new ListsAdapter(listData));
6977
view.addItemDecoration(new MarginItemDecoration(getContext()));
7078

7179
return view;
@@ -172,4 +180,10 @@ public void bind(@NonNull CustomListItemData data) {
172180
});
173181
}
174182
}
183+
184+
@Override
185+
public void onSaveInstanceState(@NonNull Bundle outState) {
186+
super.onSaveInstanceState(outState);
187+
outState.putParcelableArrayList(KEY_LIST_DATA, listData);
188+
}
175189
}

catalog/java/io/material/catalog/listitem/MultiSectionListDemoFragment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import android.view.View;
3030
import android.view.ViewGroup;
3131
import android.widget.TextView;
32+
import android.widget.Toast;
3233
import androidx.annotation.NonNull;
3334
import androidx.annotation.Nullable;
35+
import com.google.android.material.listitem.ListItemCardView;
3436
import com.google.android.material.listitem.ListItemViewHolder;
3537
import io.material.catalog.feature.DemoFragment;
3638
import java.util.ArrayList;
@@ -172,15 +174,20 @@ public void getItemOffsets(@NonNull Rect outRect,
172174
public static class CustomItemViewHolder extends ListItemViewHolder {
173175

174176
private final TextView textView;
177+
private final ListItemCardView cardView;
175178

176179
public CustomItemViewHolder(@NonNull View itemView) {
177180
super(itemView);
178181
textView = itemView.findViewById(R.id.cat_list_item_text);
182+
cardView = itemView.findViewById(R.id.cat_list_item_card_view);
179183
}
180184

181185
public void bind(@NonNull CustomListItemData data) {
182186
super.bind(data.indexInSection, data.sectionCount);
183187
textView.setText(data.text);
188+
cardView.setOnClickListener(
189+
v -> Toast.makeText(v.getContext(), R.string.mtrl_list_item_clicked, Toast.LENGTH_SHORT)
190+
.show());
184191
}
185192
}
186193

catalog/java/io/material/catalog/listitem/SegmentedListDemoFragment.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
/** A fragment that displays a segmented List demos for the Catalog app. */
4242
public class SegmentedListDemoFragment extends ListsMainDemoFragment {
4343

44+
private static final String KEY_LIST_DATA = "key_list_data";
45+
private static final String KEY_SELECTED_POSITION = "key_selected_position";
46+
47+
private ArrayList<CustomListItemData> listData;
4448
private ListsAdapter adapter;
49+
4550
@NonNull
4651
@Override
4752
public View onCreateDemoView(
@@ -53,16 +58,23 @@ public View onCreateDemoView(
5358
layoutInflater.inflate(R.layout.cat_lists_bright_background_fragment, viewGroup, false);
5459

5560
view.setLayoutManager(new LinearLayoutManager(getContext()));
56-
List<CustomListItemData> data = new ArrayList<>();
57-
for (int i = 0; i < 20; i++) {
58-
data.add(
59-
new CustomListItemData(
60-
String.format(view.getContext().getString(R.string.cat_list_item_text), i + 1),
61-
i,
62-
20));
61+
if (bundle != null) {
62+
listData = bundle.getParcelableArrayList(KEY_LIST_DATA);
63+
} else {
64+
listData = new ArrayList<>();
65+
for (int i = 0; i < 20; i++) {
66+
listData.add(
67+
new CustomListItemData(
68+
String.format(view.getContext().getString(R.string.cat_list_item_text), i + 1),
69+
i,
70+
20));
71+
}
6372
}
6473

65-
adapter = new ListsAdapter(data);
74+
adapter = new ListsAdapter(listData);
75+
if (bundle != null) {
76+
adapter.setSelectedPosition(bundle.getInt(KEY_SELECTED_POSITION, NO_SELECTION));
77+
}
6678
view.setAdapter(adapter);
6779
view.addItemDecoration(new MarginItemDecoration(getContext()));
6880

@@ -149,4 +161,11 @@ public void bind(@NonNull CustomListItemData data) {
149161
});
150162
}
151163
}
164+
165+
@Override
166+
public void onSaveInstanceState(@NonNull Bundle outState) {
167+
super.onSaveInstanceState(outState);
168+
outState.putParcelableArrayList(KEY_LIST_DATA, listData);
169+
outState.putInt(KEY_SELECTED_POSITION, adapter.getSelectedPosition());
170+
}
152171
}

catalog/java/io/material/catalog/listitem/res/layout/cat_list_multisection_viewholder.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<com.google.android.material.listitem.ListItemCardView
2626
style="?attr/listItemCardViewSegmentedStyle"
2727
android:id="@+id/cat_list_item_card_view"
28-
android:checkable="true"
2928
android:clickable="true"
3029
android:focusable="true"
3130
android:layout_height="wrap_content"

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@
8080
<!-- Styles for List item content. -->
8181
<style name="Widget.Material3.ImageView.ListItem.LeadingIcon" parent="android:Widget">
8282
<item name="tint">@color/m3_list_item_leading_icon_color_selector</item>
83+
<item name="android:importantForAccessibility">no</item>
84+
<item name="android:focusable">false</item>
85+
<item name="android:clickable">false</item>
8386
</style>
8487

8588
<style name="Widget.Material3.ImageView.ListItem.TrailingIcon" parent="android:Widget">
8689
<item name="tint">@color/m3_list_item_trailing_icon_color_selector</item>
90+
<item name="android:importantForAccessibility">no</item>
91+
<item name="android:focusable">false</item>
92+
<item name="android:clickable">false</item>
8793
</style>
8894

8995
<style name="Widget.Material3.ShapeableImageView.ListItem.LeadingImage" parent="Widget.MaterialComponents.ShapeableImageView">
@@ -114,23 +120,20 @@
114120
<style name="Widget.Material3.Checkbox.ListItem" parent="Widget.Material3.CompoundButton.CheckBox">
115121
<item name="android:focusable">false</item>
116122
<item name="android:clickable">false</item>
117-
<item name="android:importantForAccessibility">no</item>
118123
<item name="android:background">@null</item>
119124
<item name="buttonTint">@color/m3_list_item_checkbox_button_tint_selector</item>
120125
</style>
121126

122127
<style name="Widget.Material3.Switch.ListItem" parent="Widget.Material3.CompoundButton.MaterialSwitch">
123128
<item name="android:focusable">false</item>
124129
<item name="android:clickable">false</item>
125-
<item name="android:importantForAccessibility">no</item>
126130
<item name="android:background">@null</item>
127131
<item name="trackTint">@color/m3_list_item_switch_track_tint_selector</item>
128132
</style>
129133

130134
<style name="Widget.Material3.RadioButton.ListItem" parent="Widget.Material3.CompoundButton.RadioButton">
131135
<item name="android:focusable">false</item>
132136
<item name="android:clickable">false</item>
133-
<item name="android:importantForAccessibility">no</item>
134137
<item name="android:background">@null</item>
135138
<item name="buttonTint">@color/m3_list_item_radio_button_tint_selector</item>
136139
</style>

0 commit comments

Comments
 (0)