Skip to content

Commit 26c3779

Browse files
imhappileticiarossi
authored andcommitted
[Carousel][Catalog] Add option for snapping with multi-browse carousel demo
PiperOrigin-RevId: 529818471
1 parent 8938da8 commit 26c3779

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

catalog/java/io/material/catalog/carousel/MultiBrowseDemoFragment.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.Nullable;
2929
import com.google.android.material.carousel.CarouselLayoutManager;
30+
import com.google.android.material.carousel.CarouselSnapHelper;
3031
import com.google.android.material.carousel.MultiBrowseCarouselStrategy;
3132
import com.google.android.material.divider.MaterialDividerItemDecoration;
3233
import com.google.android.material.materialswitch.MaterialSwitch;
@@ -61,6 +62,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
6162
MaterialSwitch debugSwitch = view.findViewById(R.id.debug_switch);
6263
MaterialSwitch forceCompactSwitch = view.findViewById(R.id.force_compact_arrangement_switch);
6364
MaterialSwitch drawDividers = view.findViewById(R.id.draw_dividers_switch);
65+
MaterialSwitch snapSwitch = view.findViewById(R.id.snap_switch);
6466
AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown);
6567
Slider positionSlider = view.findViewById(R.id.position_slider);
6668

@@ -95,6 +97,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
9597
}
9698
});
9799

100+
CarouselSnapHelper snapHelper = new CarouselSnapHelper();
101+
snapSwitch.setOnCheckedChangeListener(
102+
(buttonView, isChecked) -> {
103+
if (isChecked) {
104+
snapHelper.attachToRecyclerView(multiBrowseStartRecyclerView);
105+
} else {
106+
snapHelper.attachToRecyclerView(null);
107+
}
108+
});
109+
98110
CarouselAdapter adapter =
99111
new CarouselAdapter(
100112
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position),

catalog/java/io/material/catalog/carousel/res/layout/cat_carousel_multi_browse_fragment.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@
6969
android:textAppearance="?attr/textAppearanceBodyLarge"
7070
android:text="@string/cat_carousel_draw_dividers_label"/>
7171

72+
<com.google.android.material.materialswitch.MaterialSwitch
73+
android:id="@+id/snap_switch"
74+
android:layout_width="match_parent"
75+
android:layout_height="wrap_content"
76+
android:checked="false"
77+
android:layout_marginHorizontal="16dp"
78+
android:layout_marginVertical="8dp"
79+
android:textAppearance="?attr/textAppearanceBodyLarge"
80+
android:contentDescription="@string/cat_carousel_snap_switch_description"
81+
android:text="@string/cat_carousel_enable_snap_label"/>
82+
7283
<com.google.android.material.textfield.TextInputLayout
7384
style="?attr/textInputFilledExposedDropdownMenuStyle"
7485
android:layout_width="match_parent"

catalog/java/io/material/catalog/carousel/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
<string name="cat_carousel_debug_mode_label" translatable="false">Debug mode</string>
2020
<string name="cat_carousel_force_compact_arrangement_label" translatable="false">Force compact arrangement</string>
2121
<string name="cat_carousel_draw_dividers_label" translatable="false">Draw dividers</string>
22+
<string name="cat_carousel_enable_snap_label" description="The title of a switch to enable snapping to carousel items.[CHAR_LIMIT=NONE]">Enable Snap</string>
2223
<string name="cat_carousel_adapter_item_count_hint_label" translatable="false">Item count</string>
2324
<string name="cat_carousel_position_slider_label" translatable="false">Scroll position</string>
25+
<string name="cat_carousel_snap_switch_description" description="Content description of a switch to enable snapping.[CHAR_LIMIT=NONE]">Enable snapping to carousel items</string>
2426
<string name="cat_carousel_position_slider_content_description" translatable="false">Scroll position slider</string>
2527

2628
<string-array name="cat_carousel_adapter_count_content" translatable="false">

0 commit comments

Comments
 (0)