Skip to content

Commit 93913a4

Browse files
hunterstichdrchen
authored andcommitted
[Carousel] Updated MultiBrowseCarouselStrategy to find best arrangments using a cost function
This changes the way arrangements are found by: * Finding all possible arrangements of items * Sort the arrangement candidates using a cost function that optimizes for total space fit, large item size retention, and adherence to other input params * Fit and use the top arrangement to work within the carousel's available space PiperOrigin-RevId: 522568015 (cherry picked from commit 0184b5b)
1 parent 48fd6d2 commit 93913a4

File tree

7 files changed

+786
-125
lines changed

7 files changed

+786
-125
lines changed

catalog/java/io/material/catalog/carousel/CarouselAdapter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import androidx.recyclerview.widget.DiffUtil;
2323
import android.view.LayoutInflater;
2424
import android.view.ViewGroup;
25+
import androidx.annotation.LayoutRes;
2526
import androidx.annotation.NonNull;
2627

2728
/** An adapter that displays {@link CarouselItem}s for a Carousel. */
@@ -44,18 +45,24 @@ public boolean areContentsTheSame(
4445
};
4546

4647
private final CarouselItemListener listener;
48+
@LayoutRes private final int itemLayoutRes;
4749

4850
CarouselAdapter(CarouselItemListener listener) {
51+
this(listener, R.layout.cat_carousel_item);
52+
}
53+
54+
CarouselAdapter(CarouselItemListener listener, @LayoutRes int itemLayoutRes) {
4955
super(DIFF_CALLBACK);
5056
this.listener = listener;
57+
this.itemLayoutRes = itemLayoutRes;
5158
}
5259

5360
@NonNull
5461
@Override
5562
public CarouselItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int pos) {
5663
return new CarouselItemViewHolder(
5764
LayoutInflater.from(viewGroup.getContext())
58-
.inflate(R.layout.cat_carousel_item, viewGroup, false), listener);
65+
.inflate(itemLayoutRes, viewGroup, false), listener);
5966
}
6067

6168
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
9797

9898
CarouselAdapter adapter =
9999
new CarouselAdapter(
100-
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position));
100+
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position),
101+
R.layout.cat_carousel_item_narrow);
101102

102103
itemCountDropdown.setOnItemClickListener(
103104
(parent, view1, position, id) -> {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2023 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<com.google.android.material.carousel.MaskableFrameLayout
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
xmlns:tools="http://schemas.android.com/tools"
22+
android:id="@+id/carousel_item_container"
23+
android:layout_width="130dp"
24+
android:layout_height="match_parent"
25+
android:layout_marginStart="4dp"
26+
android:layout_marginEnd="4dp"
27+
android:foreground="?attr/selectableItemBackground"
28+
app:shapeAppearance="?attr/shapeAppearanceCornerExtraLarge">
29+
<ImageView
30+
android:id="@+id/carousel_image_view"
31+
android:layout_width="match_parent"
32+
android:layout_height="match_parent"
33+
android:scaleType="centerCrop"
34+
tools:ignore="ContentDescription" />
35+
</com.google.android.material.carousel.MaskableFrameLayout>

0 commit comments

Comments
 (0)