Skip to content

[List] MultiSectionListDemo crashes in case SubHeader at first position #4987

@patrickfrei

Description

@patrickfrei

Description: The Catalog example MultiSectionListDemo crashes in case we define a SubHeader item or multiple CustomList items (followed by a SubHeader item or a single CustomList item) at the top of the overall list. The Catalog example is currently only working in case there is one single item at the first position, but there are further use cases than that (as just mentioned).

The exception looks as follows:
java.lang.ClassCastException: io.material.catalog.listitem.MultiSectionListDemoFragment$CustomItemViewHolder cannot be cast to io.material.catalog.listitem.MultiSectionListDemoFragment$SubheaderViewHolder

Minimal sample app repro:
Use cases in MultiSectionListDemo that cause the Catalog demo to crash:

-> Use case A (sub-header item at first position)

  1. Open file MultiSectionListDemoFragment.java
  2. Go to line 54
  3. Between lines 54 and 55, insert data.add(new CustomListItemData("Subheader TEST"));
  4. Final code snippet as of line 53:
List<CustomListItemData> data = new ArrayList<>();
int itemCount = 0;
data.add(new CustomListItemData("Subheader TEST"));
data.add(
        new CustomListItemData(
            String.format(view.getContext().getString(R.string.cat_list_item_text), 0), 0, 1));

This will make the app crashing with the aforementioned exception in onBindViewHolder. Probably, because the view type cannot be correctly derived as the index is wrong.

-> Use case B (multiple custom list items at first position, followed by a single custom list item)
The app is crashing as well with an exception if you insert the following at the first position (between lines 54 and 55):

for (int i = 0; i < 3; i++) {
   data.add(
      new CustomListItemData(
         String.format( view.getContext().getString(R.string.cat_list_item_text), itemCount + i), i,3));
}

followed e.g. by...

data.add(
        new CustomListItemData(
            String.format(view.getContext().getString(R.string.cat_list_item_text), 0), 0, 1));

Android API version: 16

Material Library version: 1.14.0-alpha08

Device: Google Pixel 9 Pro

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions