-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
lib/java/com/google/android/material/carousel/FullScreenCarouselStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.google.android.material.carousel; | ||
|
||
import static com.google.android.material.carousel.CarouselStrategyHelper.createLeftAlignedKeylineState; | ||
import static java.lang.Math.min; | ||
|
||
import androidx.recyclerview.widget.RecyclerView.LayoutParams; | ||
import android.view.View; | ||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* A {@link CarouselStrategy} that fits one full-width or full-height item into a container to | ||
* create a layout to browse one item at a time. | ||
* | ||
* <p>Note that the item masks will be based on the items encompassing the full width or full height | ||
* of the carousel. The carousel item dimensions should reflect this with `match_parent` dimensions. | ||
* | ||
* <p>This class will automatically be reversed by {@link CarouselLayoutManager} if being laid out | ||
* right-to-left and does not need to make any account for layout direction itself. | ||
*/ | ||
public class FullScreenCarouselStrategy extends CarouselStrategy { | ||
|
||
@Override | ||
@NonNull | ||
KeylineState onFirstChildMeasuredWithMargins(@NonNull Carousel carousel, @NonNull View child) { | ||
float availableSpace; | ||
LayoutParams childLayoutParams = (LayoutParams) child.getLayoutParams(); | ||
float childMargins; | ||
if (carousel.isHorizontal()) { | ||
availableSpace = carousel.getContainerWidth(); | ||
childMargins = childLayoutParams.leftMargin + childLayoutParams.rightMargin; | ||
} else { | ||
availableSpace = carousel.getContainerHeight(); | ||
childMargins = childLayoutParams.topMargin + childLayoutParams.bottomMargin; | ||
} | ||
float targetChildSize = min(availableSpace + childMargins, availableSpace); | ||
Arrangement arrangement = new Arrangement( | ||
/* priority= */ 0, | ||
/* targetSmallSize= */ 0, | ||
/* minSmallSize= */ 0, | ||
/* maxSmallSize= */ 0, | ||
/* smallCount= */ 0, | ||
/* targetMediumSize= */ 0, | ||
/* mediumCount= */ 0, | ||
targetChildSize, | ||
/* largeCount= */ 1, | ||
availableSpace); | ||
return createLeftAlignedKeylineState( | ||
child.getContext(), childMargins, availableSpace, arrangement); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
lib/javatests/com/google/android/material/carousel/FullScreenCarouselStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
package com.google.android.material.carousel; | ||
|
||
import com.google.android.material.test.R; | ||
|
||
import static com.google.android.material.carousel.CarouselHelper.createCarouselWithWidth; | ||
import static com.google.android.material.carousel.CarouselHelper.createViewWithSize; | ||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import androidx.recyclerview.widget.RecyclerView.LayoutParams; | ||
import android.view.View; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import com.google.android.material.carousel.KeylineState.Keyline; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
/** Tests for {@link FullScreenCarouselStrategy}. */ | ||
@RunWith(RobolectricTestRunner.class) | ||
public class FullScreenCarouselStrategyTest { | ||
|
||
@Test | ||
public void testItemSmallerThanContainer_showsOneLarge() { | ||
Carousel carousel = createCarouselWithWidth(400); | ||
FullScreenCarouselStrategy config = new FullScreenCarouselStrategy(); | ||
View view = createViewWithSize(ApplicationProvider.getApplicationContext(), 100, 400); | ||
|
||
KeylineState keylineState = config.onFirstChildMeasuredWithMargins(carousel, view); | ||
|
||
// A fullscreen layout should be [xSmall-large-xSmall] where the xSmall items are | ||
// outside the bounds of the carousel container and the large center item takes up the | ||
// container's full width. | ||
assertThat(keylineState.getKeylines()).hasSize(3); | ||
assertThat(keylineState.getKeylines().get(0).locOffset).isLessThan(0F); | ||
assertThat(keylineState.getKeylines().get(1).mask).isEqualTo(0F); | ||
assertThat(keylineState.getKeylines().get(2).locOffset) | ||
.isGreaterThan((float) carousel.getContainerHeight()); | ||
} | ||
|
||
@Test | ||
public void testKnownArrangement_correctlyCalculatesKeylineLocations() { | ||
View view = createViewWithSize(ApplicationProvider.getApplicationContext(), 400, 200); | ||
|
||
FullScreenCarouselStrategy config = new FullScreenCarouselStrategy(); | ||
float extraSmallSize = | ||
view.getResources().getDimension(R.dimen.m3_carousel_gone_size); | ||
// Keyline sizes for the fullscreen variant carousel are: | ||
// {extraSmallSize, largeSize, extraSmallSize} | ||
// The keyline loc offsets are placed so that an item centered on a keyline has the | ||
// keyline size described above. | ||
float[] locOffsets = new float[] {-extraSmallSize / 2f, 200, 400 + extraSmallSize / 2f}; | ||
|
||
List<Keyline> keylines = | ||
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(400), view).getKeylines(); | ||
for (int i = 0; i < keylines.size(); i++) { | ||
assertThat(keylines.get(i).locOffset).isEqualTo(locOffsets[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void testKnownArrangementWithMargins_correctlyCalculatesKeylineLocations() { | ||
View view = createViewWithSize(ApplicationProvider.getApplicationContext(), 400, 200); | ||
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); | ||
layoutParams.leftMargin += 50; | ||
layoutParams.rightMargin += 30; | ||
|
||
FullScreenCarouselStrategy config = new FullScreenCarouselStrategy(); | ||
float extraSmallSize = view.getResources().getDimension(R.dimen.m3_carousel_gone_size); | ||
// Keyline sizes for the fullscreen variant carousel are: | ||
// {extraSmallSize, largeSize, extraSmallSize} | ||
// The keyline loc offsets are placed so that an item centered on a keyline has the | ||
// keyline size described above. | ||
// The large size is based on whatever width is left over from the minimum small size. | ||
float[] locOffsets = | ||
new float[] {-(extraSmallSize + 80) / 2f, 200, 400 + (extraSmallSize + 80) / 2f}; | ||
|
||
List<Keyline> keylines = | ||
config.onFirstChildMeasuredWithMargins(createCarouselWithWidth(400), view).getKeylines(); | ||
for (int i = 0; i < keylines.size(); i++) { | ||
assertThat(keylines.get(i).locOffset).isEqualTo(locOffsets[i]); | ||
} | ||
} | ||
} |