CarouselLayoutManager

public class CarouselLayoutManager implements Carousel


A LayoutManager that can mask and offset items along the scrolling axis, creating a unique list optimized for a stylized viewing experience.

CarouselLayoutManager requires all children to use MaskableFrameLayout as their root ViewGroup.

Note that when Carousel measures and lays out items, the first item in the adapter will be measured and it's desired size will be used to determine an appropriate size for all items in the carousel.

For more information, see the component developer guidance and design guidelines.

Summary

Constants

static final int

Aligns large items to the center of the carousel.

static final int

Aligns large items to the start of the carousel.

static final int

Horizontal orientation for Carousel.

static final int

Vertical orientation for Carousel.

Public fields

int

Public constructors

CarouselLayoutManager(CarouselStrategy strategy, int orientation)
CarouselLayoutManager(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

boolean
boolean
int

Compute the extent of the horizontal scrollbar thumb.

int

Calculate the offset of the horizontal scrollbar thumb within the horizontal range.

int

Compute the horizontal range represented by the horizontal scroll bars.

PointF
computeScrollVectorForPosition(int targetPosition)
int
int
int
LayoutParams
int

Gets the alignment of the carousel.

int

Gets the height of the carousel container.

int

Gets the width of the carousel container.

void
getDecoratedBoundsWithMargins(View view, Rect outBounds)

Returns the masked, decorated bounds with margins for view.

int

Returns the current orientation of the layout.

boolean
boolean

Whether or not the orientation is horizontal.

void
measureChildWithMargins(View child, int widthUsed, int heightUsed)
void

Recalculates the internal state of the Carousel based on the size of the items.

void
onAttachedToWindow(RecyclerView view)
void
onDetachedFromWindow(RecyclerView view, Recycler recycler)
View
onFocusSearchFailed(
    View focused,
    int focusDirection,
    Recycler recycler,
    State state
)
void
onInitializeAccessibilityEvent(AccessibilityEvent event)
void
onItemsAdded(
    RecyclerView recyclerView,
    int positionStart,
    int itemCount
)
void
onItemsChanged(RecyclerView recyclerView)
void
onItemsRemoved(
    RecyclerView recyclerView,
    int positionStart,
    int itemCount
)
void
onLayoutChildren(Recycler recycler, State state)
void
onLayoutCompleted(State state)
boolean
requestChildRectangleOnScreen(
    RecyclerView parent,
    View child,
    Rect rect,
    boolean immediate,
    boolean focusedChildVisible
)
int
scrollHorizontallyBy(int dx, Recycler recycler, State state)
void
scrollToPosition(int position)
int
scrollVerticallyBy(int dy, Recycler recycler, State state)
void
setCarouselAlignment(int alignment)

Sets the alignment of the focal items in the carousel.

void

Sets the CarouselStrategy used by this layout manager to mask and offset child views as they move along the scrolling axis.

void
setOrientation(int orientation)

Sets the orientation of the layout.

void
smoothScrollToPosition(
    RecyclerView recyclerView,
    State state,
    int position
)

Inherited methods

From com.google.android.material.carousel.Carousel
abstract int

Gets the number of items in the carousel.

Constants

ALIGNMENT_CENTER

public static final int ALIGNMENT_CENTER = 1

Aligns large items to the center of the carousel.

ALIGNMENT_START

public static final int ALIGNMENT_START = 0

Aligns large items to the start of the carousel.

HORIZONTAL

public static final int HORIZONTAL

Horizontal orientation for Carousel.

VERTICAL

public static final int VERTICAL

Vertical orientation for Carousel.

Public fields

carouselAlignment

public int carouselAlignment

Public constructors

CarouselLayoutManager

public CarouselLayoutManager()

CarouselLayoutManager

public CarouselLayoutManager(CarouselStrategy strategy)

CarouselLayoutManager

public CarouselLayoutManager(CarouselStrategy strategy, int orientation)

CarouselLayoutManager

public CarouselLayoutManager(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)

Public methods

canScrollHorizontally

public boolean canScrollHorizontally()

canScrollVertically

public boolean canScrollVertically()

computeHorizontalScrollExtent

public int computeHorizontalScrollExtent(State state)

Compute the extent of the horizontal scrollbar thumb. This is the size of the thumb inside the scrollbar track.

This method can return an arbitrary unit as long as the unit is shared across computeHorizontalScrollExtent and computeHorizontalScrollOffset.

computeHorizontalScrollOffset

public int computeHorizontalScrollOffset(State state)

Calculate the offset of the horizontal scrollbar thumb within the horizontal range. This is the position of the thumb within the scrollbar track.

This is also used for accessibility when scrolling to give auditory feedback about the current scroll position within the total range.

This method can return an arbitrary unit as long as the unit is shared across computeHorizontalScrollExtent and computeHorizontalScrollRange.

computeHorizontalScrollRange

public int computeHorizontalScrollRange(State state)

Compute the horizontal range represented by the horizontal scroll bars. This is the total length of the scrollbar track within the range.

This method can return an arbitrary unit as long as the unit is shared across computeHorizontalScrollExtent and computeHorizontalScrollOffset.

computeScrollVectorForPosition

public PointF computeScrollVectorForPosition(int targetPosition)

computeVerticalScrollExtent

public int computeVerticalScrollExtent(State state)

computeVerticalScrollOffset

public int computeVerticalScrollOffset(State state)

computeVerticalScrollRange

public int computeVerticalScrollRange(State state)

generateDefaultLayoutParams

public LayoutParams generateDefaultLayoutParams()

getCarouselAlignment

public int getCarouselAlignment()

Gets the alignment of the carousel.

getContainerHeight

public int getContainerHeight()

Gets the height of the carousel container.

getContainerWidth

public int getContainerWidth()

Gets the width of the carousel container.

getDecoratedBoundsWithMargins

public void getDecoratedBoundsWithMargins(View view, Rect outBounds)

Returns the masked, decorated bounds with margins for view.

Note that this differs from the super method which returns the fully unmasked bounds of view.

Getting the masked, decorated bounds is useful for item decorations and other associated classes which need the actual visual bounds of an item in the RecyclerView. If the full, unmasked bounds is needed, see getDecoratedBoundsWithMargins.

Parameters
View view

the view element to check

Rect outBounds

a rect that will receive the bounds of the element including its maks, decoration, and margins.

getOrientation

public int getOrientation()

Returns the current orientation of the layout.

Returns
int

Current orientation, either HORIZONTAL or VERTICAL

See also
setOrientation

isAutoMeasureEnabled

public boolean isAutoMeasureEnabled()

isHorizontal

public boolean isHorizontal()

Whether or not the orientation is horizontal.

measureChildWithMargins

public void measureChildWithMargins(View child, int widthUsed, int heightUsed)

notifyItemSizeChanged

public void notifyItemSizeChanged()

Recalculates the internal state of the Carousel based on the size of the items. This should be called whenever the size of the items is changed.

onAttachedToWindow

public void onAttachedToWindow(RecyclerView view)

onDetachedFromWindow

public void onDetachedFromWindow(RecyclerView view, Recycler recycler)

onFocusSearchFailed

public View onFocusSearchFailed(
    View focused,
    int focusDirection,
    Recycler recycler,
    State state
)

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent(AccessibilityEvent event)

onItemsAdded

public void onItemsAdded(
    RecyclerView recyclerView,
    int positionStart,
    int itemCount
)

onItemsChanged

public void onItemsChanged(RecyclerView recyclerView)

onItemsRemoved

public void onItemsRemoved(
    RecyclerView recyclerView,
    int positionStart,
    int itemCount
)

onLayoutChildren

public void onLayoutChildren(Recycler recycler, State state)

onLayoutCompleted

public void onLayoutCompleted(State state)

requestChildRectangleOnScreen

public boolean requestChildRectangleOnScreen(
    RecyclerView parent,
    View child,
    Rect rect,
    boolean immediate,
    boolean focusedChildVisible
)

scrollHorizontallyBy

public int scrollHorizontallyBy(int dx, Recycler recycler, State state)

scrollToPosition

public void scrollToPosition(int position)

scrollVerticallyBy

public int scrollVerticallyBy(int dy, Recycler recycler, State state)

setCarouselAlignment

public void setCarouselAlignment(int alignment)

Sets the alignment of the focal items in the carousel.

setCarouselStrategy

public void setCarouselStrategy(CarouselStrategy carouselStrategy)

Sets the CarouselStrategy used by this layout manager to mask and offset child views as they move along the scrolling axis.

setOrientation

public void setOrientation(int orientation)

Sets the orientation of the layout.

Parameters
int orientation

HORIZONTAL or VERTICAL

smoothScrollToPosition

public void smoothScrollToPosition(
    RecyclerView recyclerView,
    State state,
    int position
)