Skip to content

Commit 7cfd30f

Browse files
imhappipekingme
authored andcommitted
[Catalog][Carousel] Add alignment option to hero carousel
PiperOrigin-RevId: 548725196
1 parent 547156e commit 7cfd30f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

catalog/java/io/material/catalog/carousel/HeroCarouselDemoFragment.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
import io.material.catalog.R;
2020

21+
import static com.google.android.material.carousel.CarouselLayoutManager.ALIGNMENT_CENTER;
22+
import static com.google.android.material.carousel.CarouselLayoutManager.ALIGNMENT_START;
23+
2124
import android.os.Bundle;
2225
import androidx.recyclerview.widget.RecyclerView;
2326
import androidx.recyclerview.widget.SnapHelper;
2427
import android.view.LayoutInflater;
2528
import android.view.View;
2629
import android.view.ViewGroup;
2730
import android.widget.AutoCompleteTextView;
31+
import android.widget.RadioButton;
2832
import androidx.annotation.NonNull;
2933
import androidx.annotation.Nullable;
3034
import com.google.android.material.carousel.CarouselLayoutManager;
@@ -65,8 +69,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
6569
MaterialSwitch enableFlingSwitch = view.findViewById(R.id.enable_fling_switch);
6670
AutoCompleteTextView itemCountDropdown = view.findViewById(R.id.item_count_dropdown);
6771
Slider positionSlider = view.findViewById(R.id.position_slider);
72+
RadioButton startAlignButton = view.findViewById(R.id.start_align);
73+
RadioButton centerAlignButton = view.findViewById(R.id.center_align);
6874

69-
// A start-aligned hero carousel
75+
// A hero carousel
7076
RecyclerView heroStartRecyclerView =
7177
view.findViewById(R.id.hero_start_carousel_recycler_view);
7278
CarouselLayoutManager heroStartCarouselLayoutManager =
@@ -135,6 +141,11 @@ public void onStopTrackingTouch(@NonNull Slider slider) {
135141
}
136142
});
137143

144+
startAlignButton.setOnClickListener(
145+
v -> heroStartCarouselLayoutManager.setCarouselAlignment(ALIGNMENT_START));
146+
centerAlignButton.setOnClickListener(
147+
v -> heroStartCarouselLayoutManager.setCarouselAlignment(ALIGNMENT_CENTER));
148+
138149
heroStartRecyclerView.setAdapter(adapter);
139150
adapter.submitList(CarouselData.createItems(), updateSliderRange(positionSlider, adapter));
140151
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@
6868
android:textAppearance="?attr/textAppearanceBodyLarge"
6969
android:text="@string/cat_carousel_enable_fling_label"/>
7070

71+
<RadioGroup
72+
android:checkedButton="@+id/start_align"
73+
android:orientation="horizontal"
74+
android:layout_width="match_parent"
75+
android:layout_marginHorizontal="16dp"
76+
android:layout_height="wrap_content">
77+
<RadioButton
78+
android:id="@+id/start_align"
79+
android:layout_width="wrap_content"
80+
android:layout_height="match_parent"
81+
android:enabled="true"
82+
android:text="@string/cat_carousel_start_align_label"/>
83+
<RadioButton
84+
android:id="@+id/center_align"
85+
android:layout_width="wrap_content"
86+
android:layout_height="match_parent"
87+
android:checked="false"
88+
android:enabled="true"
89+
android:text="@string/cat_carousel_center_align_label"/>
90+
</RadioGroup>
91+
7192
<com.google.android.material.textfield.TextInputLayout
7293
style="?attr/textInputFilledExposedDropdownMenuStyle"
7394
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
@@ -21,6 +21,8 @@
2121
<string name="cat_carousel_draw_dividers_label" translatable="false">Draw dividers</string>
2222
<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>
2323
<string name="cat_carousel_enable_fling_label" description="The title of a switch to enable the flinging gesture.[CHAR_LIMIT=NONE]">Enable fling</string>
24+
<string name="cat_carousel_start_align_label" description="The title of a button to make the carousel start-aligned.[CHAR_LIMIT=NONE]">Start align</string>
25+
<string name="cat_carousel_center_align_label" description="The title of a button to make the carousel center-aligned.[CHAR_LIMIT=NONE]">Center align</string>
2426

2527
<string name="cat_carousel_show_debug_options" description="The title of the button that shows carousel debug options.[CHAR_LIMIT=NONE]">Show debug options</string>
2628

0 commit comments

Comments
 (0)