CarouselLayoutManager

public class CarouselLayoutManager
extends RecyclerView.LayoutManager

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.LayoutManager
     ↳ com.google.android.material.carousel.CarouselLayoutManager


A RecyclerView.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.

Summary

Public constructors

CarouselLayoutManager()

Public methods

boolean canScrollHorizontally()
int computeHorizontalScrollExtent(RecyclerView.State state)

Compute the extent of the horizontal scrollbar thumb.

int computeHorizontalScrollOffset(RecyclerView.State state)

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

int computeHorizontalScrollRange(RecyclerView.State state)

Compute the horizontal range represented by the horizontal scroll bars.

RecyclerView.LayoutParams generateDefaultLayoutParams()
int getContainerWidth()
void getDecoratedBoundsWithMargins(View view, Rect outBounds)

Returns the masked, decorated bounds with margins for view.

void measureChildWithMargins(View child, int widthUsed, int heightUsed)
void onInitializeAccessibilityEvent(AccessibilityEvent event)
void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
void onLayoutCompleted(RecyclerView.State state)
boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible)
int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state)
void scrollToPosition(int position)
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.

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

Inherited methods

Public constructors

CarouselLayoutManager

public CarouselLayoutManager ()

Public methods

canScrollHorizontally

public boolean canScrollHorizontally ()

Returns
boolean

computeHorizontalScrollExtent

public int computeHorizontalScrollExtent (RecyclerView.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(State) and computeHorizontalScrollOffset(State).

Parameters
state RecyclerView.State

Returns
int

computeHorizontalScrollOffset

public int computeHorizontalScrollOffset (RecyclerView.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(State) and computeHorizontalScrollRange(State).

Parameters
state RecyclerView.State

Returns
int

computeHorizontalScrollRange

public int computeHorizontalScrollRange (RecyclerView.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(State) and computeHorizontalScrollOffset(State).

Parameters
state RecyclerView.State

Returns
int

generateDefaultLayoutParams

public RecyclerView.LayoutParams generateDefaultLayoutParams ()

Returns
RecyclerView.LayoutParams

getContainerWidth

public int getContainerWidth ()

Returns
int

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 RecyclerView.getDecoratedBoundsWithMargins(View, Rect).

Parameters
view View: the view element to check

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

measureChildWithMargins

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

Parameters
child View

widthUsed int

heightUsed int

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Parameters
event AccessibilityEvent

onLayoutChildren

public void onLayoutChildren (RecyclerView.Recycler recycler, 
                RecyclerView.State state)

Parameters
recycler RecyclerView.Recycler

state RecyclerView.State

onLayoutCompleted

public void onLayoutCompleted (RecyclerView.State state)

Parameters
state RecyclerView.State

requestChildRectangleOnScreen

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

Parameters
parent RecyclerView

child View

rect Rect

immediate boolean

focusedChildVisible boolean

Returns
boolean

scrollHorizontallyBy

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

Parameters
dx int

recycler RecyclerView.Recycler

state RecyclerView.State

Returns
int

scrollToPosition

public void scrollToPosition (int position)

Parameters
position int

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.

Parameters
carouselStrategy CarouselStrategy

smoothScrollToPosition

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

Parameters
recyclerView RecyclerView

state RecyclerView.State

position int