Skip to content

Commit

Permalink
[Carousel] Updated MultiBrowseCarouselStrategy to find best arrangmen…
Browse files Browse the repository at this point in the history
…ts 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
  • Loading branch information
hunterstich authored and paulfthomas committed Apr 10, 2023
1 parent cf006c5 commit 0184b5b
Show file tree
Hide file tree
Showing 7 changed files with 786 additions and 125 deletions.
Expand Up @@ -22,6 +22,7 @@
import androidx.recyclerview.widget.DiffUtil;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

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

private final CarouselItemListener listener;
@LayoutRes private final int itemLayoutRes;

CarouselAdapter(CarouselItemListener listener) {
this(listener, R.layout.cat_carousel_item);
}

CarouselAdapter(CarouselItemListener listener, @LayoutRes int itemLayoutRes) {
super(DIFF_CALLBACK);
this.listener = listener;
this.itemLayoutRes = itemLayoutRes;
}

@NonNull
@Override
public CarouselItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int pos) {
return new CarouselItemViewHolder(
LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cat_carousel_item, viewGroup, false), listener);
.inflate(itemLayoutRes, viewGroup, false), listener);
}

@Override
Expand Down
Expand Up @@ -97,7 +97,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {

CarouselAdapter adapter =
new CarouselAdapter(
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position));
(item, position) -> multiBrowseStartRecyclerView.scrollToPosition(position),
R.layout.cat_carousel_item_narrow);

itemCountDropdown.setOnItemClickListener(
(parent, view1, position, id) -> {
Expand Down
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2023 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<com.google.android.material.carousel.MaskableFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/carousel_item_container"
android:layout_width="130dp"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:foreground="?attr/selectableItemBackground"
app:shapeAppearance="?attr/shapeAppearanceCornerExtraLarge">
<ImageView
android:id="@+id/carousel_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
tools:ignore="ContentDescription" />
</com.google.android.material.carousel.MaskableFrameLayout>

0 comments on commit 0184b5b

Please sign in to comment.