|
| 1 | +/* |
| 2 | + * Copyright 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.material.catalog.carousel; |
| 18 | + |
| 19 | +import io.material.catalog.R; |
| 20 | + |
| 21 | +import android.os.Bundle; |
| 22 | +import androidx.recyclerview.widget.RecyclerView; |
| 23 | +import android.view.LayoutInflater; |
| 24 | +import android.view.View; |
| 25 | +import android.view.ViewGroup; |
| 26 | +import android.widget.AutoCompleteTextView; |
| 27 | +import androidx.annotation.NonNull; |
| 28 | +import androidx.annotation.Nullable; |
| 29 | +import com.google.android.material.carousel.CarouselLayoutManager; |
| 30 | +import com.google.android.material.carousel.CarouselSnapHelper; |
| 31 | +import com.google.android.material.carousel.UncontainedCarouselStrategy; |
| 32 | +import com.google.android.material.divider.MaterialDividerItemDecoration; |
| 33 | +import com.google.android.material.materialswitch.MaterialSwitch; |
| 34 | +import com.google.android.material.slider.Slider; |
| 35 | +import com.google.android.material.slider.Slider.OnSliderTouchListener; |
| 36 | +import io.material.catalog.feature.DemoFragment; |
| 37 | + |
| 38 | +/** A fragment that displays the uncontained variant of the Carousel. */ |
| 39 | +public class UncontainedCarouselDemoFragment extends DemoFragment { |
| 40 | + |
| 41 | + private MaterialDividerItemDecoration horizontalDivider; |
| 42 | + |
| 43 | + @NonNull |
| 44 | + @Override |
| 45 | + public View onCreateDemoView( |
| 46 | + @NonNull LayoutInflater layoutInflater, |
| 47 | + @Nullable ViewGroup viewGroup, |
| 48 | + @Nullable Bundle bundle) { |
| 49 | + return layoutInflater.inflate( |
| 50 | + R.layout.cat_carousel_uncontained_fragment, viewGroup, false /* attachToRoot */); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + @SuppressWarnings("RestrictTo") |
| 55 | + public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) { |
| 56 | + super.onViewCreated(view, bundle); |
| 57 | + |
| 58 | + horizontalDivider = |
| 59 | + new MaterialDividerItemDecoration( |
| 60 | + requireContext(), MaterialDividerItemDecoration.HORIZONTAL); |
| 61 | + |
| 62 | + MaterialSwitch debugSwitch = view.findViewById(R.id.debug_switch); |
| 63 | + MaterialSwitch drawDividers = view.findViewById(R.id.draw_dividers_switch); |
| 64 | + MaterialSwitch snapSwitch = view.findViewById(R.id.snap_switch); |
| 65 | + AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown); |
| 66 | + Slider positionSlider = view.findViewById(R.id.position_slider); |
| 67 | + |
| 68 | + RecyclerView uncontainedRecyclerView = |
| 69 | + view.findViewById(R.id.uncontained_carousel_recycler_view); |
| 70 | + CarouselLayoutManager uncontainedCarouselLayoutManager = |
| 71 | + new CarouselLayoutManager(new UncontainedCarouselStrategy()); |
| 72 | + uncontainedCarouselLayoutManager.setDebuggingEnabled( |
| 73 | + uncontainedRecyclerView, debugSwitch.isChecked()); |
| 74 | + uncontainedRecyclerView.setLayoutManager(uncontainedCarouselLayoutManager); |
| 75 | + uncontainedRecyclerView.setNestedScrollingEnabled(false); |
| 76 | + |
| 77 | + debugSwitch.setOnCheckedChangeListener( |
| 78 | + (buttonView, isChecked) -> { |
| 79 | + uncontainedRecyclerView.setBackgroundResource( |
| 80 | + isChecked ? R.drawable.dashed_outline_rectangle : 0); |
| 81 | + uncontainedCarouselLayoutManager.setDebuggingEnabled( |
| 82 | + uncontainedRecyclerView, isChecked); |
| 83 | + }); |
| 84 | + |
| 85 | + drawDividers.setOnCheckedChangeListener( |
| 86 | + (buttonView, isChecked) -> { |
| 87 | + if (isChecked) { |
| 88 | + uncontainedRecyclerView.addItemDecoration(horizontalDivider); |
| 89 | + } else { |
| 90 | + uncontainedRecyclerView.removeItemDecoration(horizontalDivider); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + CarouselSnapHelper snapHelper = new CarouselSnapHelper(); |
| 95 | + snapSwitch.setOnCheckedChangeListener( |
| 96 | + (buttonView, isChecked) -> { |
| 97 | + if (isChecked) { |
| 98 | + snapHelper.attachToRecyclerView(uncontainedRecyclerView); |
| 99 | + } else { |
| 100 | + snapHelper.attachToRecyclerView(null); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + CarouselAdapter adapter = |
| 105 | + new CarouselAdapter( |
| 106 | + (item, position) -> uncontainedRecyclerView.scrollToPosition(position), |
| 107 | + R.layout.cat_carousel_item_narrow); |
| 108 | + |
| 109 | + itemCountDropdown.setOnItemClickListener( |
| 110 | + (parent, view1, position, id) -> |
| 111 | + adapter.submitList( |
| 112 | + CarouselData.createItems().subList(0, position), |
| 113 | + updateSliderRange(positionSlider, adapter))); |
| 114 | + |
| 115 | + positionSlider.addOnSliderTouchListener( |
| 116 | + new OnSliderTouchListener() { |
| 117 | + @Override |
| 118 | + public void onStartTrackingTouch(@NonNull Slider slider) {} |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onStopTrackingTouch(@NonNull Slider slider) { |
| 122 | + uncontainedRecyclerView.smoothScrollToPosition(((int) slider.getValue()) - 1); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + uncontainedRecyclerView.setAdapter(adapter); |
| 127 | + adapter.submitList(CarouselData.createItems(), updateSliderRange(positionSlider, adapter)); |
| 128 | + } |
| 129 | + |
| 130 | + private static Runnable updateSliderRange(Slider slider, CarouselAdapter adapter) { |
| 131 | + return () -> { |
| 132 | + if (adapter.getItemCount() <= 1) { |
| 133 | + slider.setEnabled(false); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + slider.setValueFrom(1); |
| 138 | + slider.setValue(1); |
| 139 | + slider.setValueTo(adapter.getItemCount()); |
| 140 | + slider.setEnabled(true); |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments