Skip to content

Commit

Permalink
[Catalog][Carousel] Fix slider value out of bounds crash in carousel …
Browse files Browse the repository at this point in the history
…demos caused by state restore happening after resetting slider values

Resolves #4037

Resolves #4036

GIT_ORIGIN_REV_ID=b41cac6c34c66ffccdb6a7696cd554e620b2e0bc
Co-authored-by: imhappi
PiperOrigin-RevId: 625484603
  • Loading branch information
manabu-nakamura authored and imhappi committed Apr 17, 2024
1 parent 8c541e6 commit c600b38
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
public class HeroCarouselDemoFragment extends DemoFragment {

private MaterialDividerItemDecoration horizontalDivider;
private CarouselAdapter adapter;
private Slider positionSlider;

@NonNull
@Override
Expand All @@ -67,7 +69,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
MaterialSwitch drawDividers = view.findViewById(R.id.draw_dividers_switch);
MaterialSwitch enableFlingSwitch = view.findViewById(R.id.enable_fling_switch);
AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown);
Slider positionSlider = view.findViewById(R.id.position_slider);
positionSlider = view.findViewById(R.id.position_slider);
RadioButton startAlignButton = view.findViewById(R.id.start_align);
RadioButton centerAlignButton = view.findViewById(R.id.center_align);

Expand Down Expand Up @@ -96,7 +98,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
}
});

CarouselAdapter adapter =
adapter =
new CarouselAdapter(
(item, position) -> {
heroStartRecyclerView.scrollToPosition(position);
Expand Down Expand Up @@ -141,6 +143,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
v -> heroStartCarouselLayoutManager.setCarouselAlignment(ALIGNMENT_CENTER));

heroStartRecyclerView.setAdapter(adapter);
}

@Override
public void onStart() {
super.onStart();
adapter.submitList(CarouselData.createItems(), updateSliderRange(positionSlider, adapter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
public class MultiBrowseCarouselDemoFragment extends DemoFragment {

private MaterialDividerItemDecoration horizontalDivider;
private CarouselAdapter adapter;
private Slider positionSlider;

@NonNull
@Override
Expand All @@ -61,7 +63,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
MaterialSwitch drawDividers = view.findViewById(R.id.draw_dividers_switch);
MaterialSwitch snapSwitch = view.findViewById(R.id.snap_switch);
AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown);
Slider positionSlider = view.findViewById(R.id.position_slider);
positionSlider = view.findViewById(R.id.position_slider);

// A start-aligned multi-browse carousel
RecyclerView multiBrowseStartRecyclerView =
Expand Down Expand Up @@ -99,7 +101,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
}
});

CarouselAdapter adapter =
adapter =
new CarouselAdapter(
(item, position) -> {
multiBrowseStartRecyclerView.scrollToPosition(position);
Expand All @@ -120,6 +122,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
CarouselDemoUtils.createScrollToPositionSliderTouchListener(multiBrowseStartRecyclerView));

multiBrowseStartRecyclerView.setAdapter(adapter);
}

@Override
public void onStart() {
super.onStart();
adapter.submitList(CarouselData.createItems(), updateSliderRange(positionSlider, adapter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
public class UncontainedCarouselDemoFragment extends DemoFragment {

private MaterialDividerItemDecoration horizontalDivider;
private CarouselAdapter adapter;
private Slider positionSlider;

@NonNull
@Override
Expand All @@ -62,7 +64,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
MaterialSwitch drawDividers = view.findViewById(R.id.draw_dividers_switch);
MaterialSwitch snapSwitch = view.findViewById(R.id.snap_switch);
AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown);
Slider positionSlider = view.findViewById(R.id.position_slider);
positionSlider = view.findViewById(R.id.position_slider);

RecyclerView uncontainedRecyclerView =
view.findViewById(R.id.uncontained_carousel_recycler_view);
Expand Down Expand Up @@ -99,7 +101,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
}
});

CarouselAdapter adapter =
adapter =
new CarouselAdapter(
(item, position) -> {
uncontainedRecyclerView.scrollToPosition(position);
Expand All @@ -119,6 +121,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
CarouselDemoUtils.createScrollToPositionSliderTouchListener(uncontainedRecyclerView));

uncontainedRecyclerView.setAdapter(adapter);
}

@Override
public void onStart() {
super.onStart();
adapter.submitList(CarouselData.createItems(), updateSliderRange(positionSlider, adapter));
}

Expand Down

0 comments on commit c600b38

Please sign in to comment.