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

void addDisappearingView(View arg0, int arg1)
void addDisappearingView(View arg0)
void addView(View arg0, int arg1)
void addView(View arg0)
void assertInLayoutOrScroll(String arg0)
void assertNotInLayoutOrScroll(String arg0)
void attachView(View arg0)
void attachView(View arg0, int arg1, RecyclerView.LayoutParams arg2)
void attachView(View arg0, int arg1)
void calculateItemDecorationsForChild(View arg0, Rect arg1)
boolean canScrollHorizontally()
boolean canScrollVertically()
boolean checkLayoutParams(RecyclerView.LayoutParams arg0)
static int chooseSize(int arg0, int arg1, int arg2)
void collectAdjacentPrefetchPositions(int arg0, int arg1, RecyclerView.State arg2, RecyclerView.LayoutManager.LayoutPrefetchRegistry arg3)
void collectInitialPrefetchPositions(int arg0, RecyclerView.LayoutManager.LayoutPrefetchRegistry arg1)
int computeHorizontalScrollExtent(RecyclerView.State arg0)
int computeHorizontalScrollOffset(RecyclerView.State arg0)
int computeHorizontalScrollRange(RecyclerView.State arg0)
int computeVerticalScrollExtent(RecyclerView.State arg0)
int computeVerticalScrollOffset(RecyclerView.State arg0)
int computeVerticalScrollRange(RecyclerView.State arg0)
void detachAndScrapAttachedViews(RecyclerView.Recycler arg0)
void detachAndScrapView(View arg0, RecyclerView.Recycler arg1)
void detachAndScrapViewAt(int arg0, RecyclerView.Recycler arg1)
void detachView(View arg0)
void detachViewAt(int arg0)
void endAnimation(View arg0)
View findContainingItemView(View arg0)
View findViewByPosition(int arg0)
abstract RecyclerView.LayoutParams generateDefaultLayoutParams()
RecyclerView.LayoutParams generateLayoutParams(Context arg0, AttributeSet arg1)
RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams arg0)
int getBaseline()
int getBottomDecorationHeight(View arg0)
View getChildAt(int arg0)
int getChildCount()
static int getChildMeasureSpec(int arg0, int arg1, int arg2, int arg3, boolean arg4)
static int getChildMeasureSpec(int arg0, int arg1, int arg2, boolean arg3)
boolean getClipToPadding()
int getColumnCountForAccessibility(RecyclerView.Recycler arg0, RecyclerView.State arg1)
int getDecoratedBottom(View arg0)
void getDecoratedBoundsWithMargins(View arg0, Rect arg1)
int getDecoratedLeft(View arg0)
int getDecoratedMeasuredHeight(View arg0)
int getDecoratedMeasuredWidth(View arg0)
int getDecoratedRight(View arg0)
int getDecoratedTop(View arg0)
View getFocusedChild()
int getHeight()
int getHeightMode()
int getItemCount()
int getItemViewType(View arg0)
int getLayoutDirection()
int getLeftDecorationWidth(View arg0)
int getMinimumHeight()
int getMinimumWidth()
int getPaddingBottom()
int getPaddingEnd()
int getPaddingLeft()
int getPaddingRight()
int getPaddingStart()
int getPaddingTop()
int getPosition(View arg0)
static RecyclerView.LayoutManager.Properties getProperties(Context arg0, AttributeSet arg1, int arg2, int arg3)
int getRightDecorationWidth(View arg0)
int getRowCountForAccessibility(RecyclerView.Recycler arg0, RecyclerView.State arg1)
int getSelectionModeForAccessibility(RecyclerView.Recycler arg0, RecyclerView.State arg1)
int getTopDecorationHeight(View arg0)
void getTransformedBoundingBox(View arg0, boolean arg1, Rect arg2)
int getWidth()
int getWidthMode()
boolean hasFocus()
void ignoreView(View arg0)
boolean isAttachedToWindow()
boolean isAutoMeasureEnabled()
boolean isFocused()
final boolean isItemPrefetchEnabled()
boolean isLayoutHierarchical(RecyclerView.Recycler arg0, RecyclerView.State arg1)
boolean isMeasurementCacheEnabled()
boolean isSmoothScrolling()
boolean isViewPartiallyVisible(View arg0, boolean arg1, boolean arg2)
void layoutDecorated(View arg0, int arg1, int arg2, int arg3, int arg4)
void layoutDecoratedWithMargins(View arg0, int arg1, int arg2, int arg3, int arg4)
void measureChild(View arg0, int arg1, int arg2)
void measureChildWithMargins(View arg0, int arg1, int arg2)
void moveView(int arg0, int arg1)
void offsetChildrenHorizontal(int arg0)
void offsetChildrenVertical(int arg0)
void onAdapterChanged(Adapter arg0, Adapter arg1)
boolean onAddFocusables(RecyclerView arg0, ArrayList<View> arg1, int arg2, int arg3)
void onAttachedToWindow(RecyclerView arg0)
void onDetachedFromWindow(RecyclerView arg0)
void onDetachedFromWindow(RecyclerView arg0, RecyclerView.Recycler arg1)
View onFocusSearchFailed(View arg0, int arg1, RecyclerView.Recycler arg2, RecyclerView.State arg3)
void onInitializeAccessibilityEvent(AccessibilityEvent arg0)
void onInitializeAccessibilityEvent(RecyclerView.Recycler arg0, RecyclerView.State arg1, AccessibilityEvent arg2)
void onInitializeAccessibilityNodeInfo(RecyclerView.Recycler arg0, RecyclerView.State arg1, AccessibilityNodeInfoCompat arg2)
void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler arg0, RecyclerView.State arg1, View arg2, AccessibilityNodeInfoCompat arg3)
View onInterceptFocusSearch(View arg0, int arg1)
void onItemsAdded(RecyclerView arg0, int arg1, int arg2)
void onItemsChanged(RecyclerView arg0)
void onItemsMoved(RecyclerView arg0, int arg1, int arg2, int arg3)
void onItemsRemoved(RecyclerView arg0, int arg1, int arg2)
void onItemsUpdated(RecyclerView arg0, int arg1, int arg2)
void onItemsUpdated(RecyclerView arg0, int arg1, int arg2, Object arg3)
void onLayoutChildren(RecyclerView.Recycler arg0, RecyclerView.State arg1)
void onLayoutCompleted(RecyclerView.State arg0)
void onMeasure(RecyclerView.Recycler arg0, RecyclerView.State arg1, int arg2, int arg3)
boolean onRequestChildFocus(RecyclerView arg0, View arg1, View arg2)
boolean onRequestChildFocus(RecyclerView arg0, RecyclerView.State arg1, View arg2, View arg3)
void onRestoreInstanceState(Parcelable arg0)
Parcelable onSaveInstanceState()
void onScrollStateChanged(int arg0)
boolean performAccessibilityAction(RecyclerView.Recycler arg0, RecyclerView.State arg1, int arg2, Bundle arg3)
boolean performAccessibilityActionForItem(RecyclerView.Recycler arg0, RecyclerView.State arg1, View arg2, int arg3, Bundle arg4)
void postOnAnimation(Runnable arg0)
void removeAllViews()
void removeAndRecycleAllViews(RecyclerView.Recycler arg0)
void removeAndRecycleView(View arg0, RecyclerView.Recycler arg1)
void removeAndRecycleViewAt(int arg0, RecyclerView.Recycler arg1)
boolean removeCallbacks(Runnable arg0)
void removeDetachedView(View arg0)
void removeView(View arg0)
void removeViewAt(int arg0)
boolean requestChildRectangleOnScreen(RecyclerView arg0, View arg1, Rect arg2, boolean arg3, boolean arg4)
boolean requestChildRectangleOnScreen(RecyclerView arg0, View arg1, Rect arg2, boolean arg3)
void requestLayout()
void requestSimpleAnimationsInNextLayout()
int scrollHorizontallyBy(int arg0, RecyclerView.Recycler arg1, RecyclerView.State arg2)
void scrollToPosition(int arg0)
int scrollVerticallyBy(int arg0, RecyclerView.Recycler arg1, RecyclerView.State arg2)
void setAutoMeasureEnabled(boolean arg0)
final void setItemPrefetchEnabled(boolean arg0)
void setMeasuredDimension(int arg0, int arg1)
void setMeasuredDimension(Rect arg0, int arg1, int arg2)
void setMeasurementCacheEnabled(boolean arg0)
void smoothScrollToPosition(RecyclerView arg0, RecyclerView.State arg1, int arg2)
void startSmoothScroll(RecyclerView.SmoothScroller arg0)
void stopIgnoringView(View arg0)
boolean supportsPredictiveItemAnimations()
Object clone()
boolean equals(Object arg0)
void finalize()
final Class<?> getClass()
int hashCode()
final void notify()
final void notifyAll()
String toString()
final void wait(long arg0, int arg1)
final void wait(long arg0)
final void wait()
abstract PointF computeScrollVectorForPosition(int arg0)

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