CarouselLayoutManager

public class CarouselLayoutManager
extends RecyclerView.LayoutManager implements RecyclerView.SmoothScroller.ScrollVectorProvider

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.

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

Summary

Constants

int ALIGNMENT_CENTER

Aligns large items to the center of the carousel.

int ALIGNMENT_START

Aligns large items to the start of the carousel.

int HORIZONTAL

Horizontal orientation for Carousel.

int VERTICAL

Vertical orientation for Carousel.

Public constructors

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

Public methods

boolean canScrollHorizontally()
boolean canScrollVertically()
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.

PointF computeScrollVectorForPosition(int targetPosition)
int computeVerticalScrollExtent(RecyclerView.State state)
int computeVerticalScrollOffset(RecyclerView.State state)
int computeVerticalScrollRange(RecyclerView.State state)
RecyclerView.LayoutParams generateDefaultLayoutParams()
int getCarouselAlignment()
int getContainerHeight()
int getContainerWidth()
void getDecoratedBoundsWithMargins(View view, Rect outBounds)

Returns the masked, decorated bounds with margins for view.

int getOrientation()

Returns the current orientation of the layout.

boolean isAutoMeasureEnabled()
boolean isHorizontal()
void measureChildWithMargins(View child, int widthUsed, int heightUsed)
void onAttachedToWindow(RecyclerView view)
void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler)
View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state)
void onInitializeAccessibilityEvent(AccessibilityEvent event)
void onItemsAdded(RecyclerView recyclerView, int positionStart, int itemCount)
void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount)
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)
int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state)
void setCarouselAlignment(int alignment)

Sets the alignment of the focal items in the carousel.

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 setOrientation(int orientation)

Sets the orientation of the layout.

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

Inherited methods

Constants

ALIGNMENT_CENTER

public static final int ALIGNMENT_CENTER

Aligns large items to the center of the carousel.

Constant Value: 1 (0x00000001)

ALIGNMENT_START

public static final int ALIGNMENT_START

Aligns large items to the start of the carousel.

Constant Value: 0 (0x00000000)

HORIZONTAL

public static final int HORIZONTAL

Horizontal orientation for Carousel.

Constant Value: 0 (0x00000000)

VERTICAL

public static final int VERTICAL

Vertical orientation for Carousel.

Constant Value: 1 (0x00000001)

Public constructors

CarouselLayoutManager

public CarouselLayoutManager ()

CarouselLayoutManager

public CarouselLayoutManager (CarouselStrategy strategy)

Parameters
strategy CarouselStrategy

CarouselLayoutManager

public CarouselLayoutManager (CarouselStrategy strategy, 
                int orientation)

Parameters
strategy CarouselStrategy

orientation int

CarouselLayoutManager

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

canScrollHorizontally

public boolean canScrollHorizontally ()

Returns
boolean

canScrollVertically

public boolean canScrollVertically ()

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

computeScrollVectorForPosition

public PointF computeScrollVectorForPosition (int targetPosition)

Parameters
targetPosition int

Returns
PointF

computeVerticalScrollExtent

public int computeVerticalScrollExtent (RecyclerView.State state)

Parameters
state RecyclerView.State

Returns
int

computeVerticalScrollOffset

public int computeVerticalScrollOffset (RecyclerView.State state)

Parameters
state RecyclerView.State

Returns
int

computeVerticalScrollRange

public int computeVerticalScrollRange (RecyclerView.State state)

Parameters
state RecyclerView.State

Returns
int

generateDefaultLayoutParams

public RecyclerView.LayoutParams generateDefaultLayoutParams ()

Returns
RecyclerView.LayoutParams

getCarouselAlignment

public int getCarouselAlignment ()

Returns
int

getContainerHeight

public int getContainerHeight ()

Returns
int

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.

getOrientation

public int getOrientation ()

Returns the current orientation of the layout.

Returns
int Current orientation, either HORIZONTAL or VERTICAL

isAutoMeasureEnabled

public boolean isAutoMeasureEnabled ()

Returns
boolean

isHorizontal

public boolean isHorizontal ()

Returns
boolean

measureChildWithMargins

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

Parameters
child View

widthUsed int

heightUsed int

onAttachedToWindow

public void onAttachedToWindow (RecyclerView view)

Parameters
view RecyclerView

onDetachedFromWindow

public void onDetachedFromWindow (RecyclerView view, 
                RecyclerView.Recycler recycler)

Parameters
view RecyclerView

recycler RecyclerView.Recycler

onFocusSearchFailed

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

Parameters
focused View

focusDirection int

recycler RecyclerView.Recycler

state RecyclerView.State

Returns
View

onInitializeAccessibilityEvent

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Parameters
event AccessibilityEvent

onItemsAdded

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

Parameters
recyclerView RecyclerView

positionStart int

itemCount int

onItemsRemoved

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

Parameters
recyclerView RecyclerView

positionStart int

itemCount int

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

scrollVerticallyBy

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

Parameters
dy int

recycler RecyclerView.Recycler

state RecyclerView.State

Returns
int

setCarouselAlignment

public void setCarouselAlignment (int alignment)

Sets the alignment of the focal items in the carousel.

Parameters
alignment 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

setOrientation

public void setOrientation (int orientation)

Sets the orientation of the layout.

Parameters
orientation int: HORIZONTAL or VERTICAL

smoothScrollToPosition

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

Parameters
recyclerView RecyclerView

state RecyclerView.State

position int