Skip to content

Commit

Permalink
[Carousel][Catalog] Add option for snapping with multi-browse carouse…
Browse files Browse the repository at this point in the history
…l demo

PiperOrigin-RevId: 529818471
  • Loading branch information
imhappi authored and leticiarossi committed May 8, 2023
1 parent 8938da8 commit 26c3779
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Expand Up @@ -27,6 +27,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.carousel.CarouselLayoutManager;
import com.google.android.material.carousel.CarouselSnapHelper;
import com.google.android.material.carousel.MultiBrowseCarouselStrategy;
import com.google.android.material.divider.MaterialDividerItemDecoration;
import com.google.android.material.materialswitch.MaterialSwitch;
Expand Down Expand Up @@ -61,6 +62,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
MaterialSwitch debugSwitch = view.findViewById(R.id.debug_switch);
MaterialSwitch forceCompactSwitch = view.findViewById(R.id.force_compact_arrangement_switch);
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);

Expand Down Expand Up @@ -95,6 +97,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
}
});

CarouselSnapHelper snapHelper = new CarouselSnapHelper();
snapSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
if (isChecked) {
snapHelper.attachToRecyclerView(multiBrowseStartRecyclerView);
} else {
snapHelper.attachToRecyclerView(null);
}
});

CarouselAdapter adapter =
new CarouselAdapter(
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position),
Expand Down
Expand Up @@ -69,6 +69,17 @@
android:textAppearance="?attr/textAppearanceBodyLarge"
android:text="@string/cat_carousel_draw_dividers_label"/>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/snap_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="8dp"
android:textAppearance="?attr/textAppearanceBodyLarge"
android:contentDescription="@string/cat_carousel_snap_switch_description"
android:text="@string/cat_carousel_enable_snap_label"/>

<com.google.android.material.textfield.TextInputLayout
style="?attr/textInputFilledExposedDropdownMenuStyle"
android:layout_width="match_parent"
Expand Down
Expand Up @@ -19,8 +19,10 @@
<string name="cat_carousel_debug_mode_label" translatable="false">Debug mode</string>
<string name="cat_carousel_force_compact_arrangement_label" translatable="false">Force compact arrangement</string>
<string name="cat_carousel_draw_dividers_label" translatable="false">Draw dividers</string>
<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>
<string name="cat_carousel_adapter_item_count_hint_label" translatable="false">Item count</string>
<string name="cat_carousel_position_slider_label" translatable="false">Scroll position</string>
<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>
<string name="cat_carousel_position_slider_content_description" translatable="false">Scroll position slider</string>

<string-array name="cat_carousel_adapter_count_content" translatable="false">
Expand Down

0 comments on commit 26c3779

Please sign in to comment.