Stay organized with collections Save and categorize content based on your preferences.
added in version 22.1.0
belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1

LinearLayoutManager

public class LinearLayoutManager
extends RecyclerView.LayoutManager implements ItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider

java.lang.Object
   ↳ android.support.v7.widget.RecyclerView.LayoutManager
     ↳ android.support.v7.widget.LinearLayoutManager
Known Direct Subclasses


A RecyclerView.LayoutManager implementation which provides similar functionality to ListView.

Summary

Nested classes

class LinearLayoutManager.LayoutChunkResult

 

XML attributes

RecyclerView_android_orientation  
RecyclerView_reverseLayout  
RecyclerView_stackFromEnd  

Inherited XML attributes

From class android.support.v7.widget.RecyclerView.LayoutManager

Constants

int HORIZONTAL

int INVALID_OFFSET

int VERTICAL

Public constructors

LinearLayoutManager(Context context)

Creates a vertical LinearLayoutManager

LinearLayoutManager(Context context, int orientation, boolean reverseLayout)
LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Constructor used when layout manager is set in XML by RecyclerView attribute "layoutManager".

Public methods

void assertNotInLayoutOrScroll(String message)

Checks if RecyclerView is in the middle of a layout or scroll and throws an IllegalStateException if it is.

boolean canScrollHorizontally()

Query if horizontal scrolling is currently supported.

boolean canScrollVertically()

Query if vertical scrolling is currently supported.

void collectAdjacentPrefetchPositions(int dx, int dy, RecyclerView.State state, RecyclerView.LayoutManager.LayoutPrefetchRegistry layoutPrefetchRegistry)

Gather all positions from the LayoutManager to be prefetched, given specified momentum.

void collectInitialPrefetchPositions(int adapterItemCount, RecyclerView.LayoutManager.LayoutPrefetchRegistry layoutPrefetchRegistry)

Gather all positions from the LayoutManager to be prefetched in preperation for its RecyclerView to come on screen, due to the movement of another, containing RecyclerView.

int computeHorizontalScrollExtent(RecyclerView.State state)

Override this method if you want to support scroll bars.

int computeHorizontalScrollOffset(RecyclerView.State state)

Override this method if you want to support scroll bars.

int computeHorizontalScrollRange(RecyclerView.State state)

Override this method if you want to support scroll bars.

PointF computeScrollVectorForPosition(int targetPosition)

Should calculate the vector that points to the direction where the target position can be found.

int computeVerticalScrollExtent(RecyclerView.State state)

Override this method if you want to support scroll bars.

int computeVerticalScrollOffset(RecyclerView.State state)

Override this method if you want to support scroll bars.

int computeVerticalScrollRange(RecyclerView.State state)

Override this method if you want to support scroll bars.

int findFirstCompletelyVisibleItemPosition()

Returns the adapter position of the first fully visible view.

int findFirstVisibleItemPosition()

Returns the adapter position of the first visible view.

int findLastCompletelyVisibleItemPosition()

Returns the adapter position of the last fully visible view.

int findLastVisibleItemPosition()

Returns the adapter position of the last visible view.

View findViewByPosition(int position)

Finds the view which represents the given adapter position.

RecyclerView.LayoutParams generateDefaultLayoutParams()

Create a default LayoutParams object for a child of the RecyclerView.

int getInitialPrefetchItemCount()

Gets the number of items to prefetch in collectInitialPrefetchPositions(int, LayoutPrefetchRegistry), which defines how many inner items should be prefetched when this LayoutManager's RecyclerView is nested inside another RecyclerView.

int getOrientation()

Returns the current orientation of the layout.

boolean getRecycleChildrenOnDetach()

Returns whether LayoutManager will recycle its children when it is detached from RecyclerView.

boolean getReverseLayout()

Returns if views are laid out from the opposite direction of the layout.

boolean getStackFromEnd()
boolean isAutoMeasureEnabled()

Returns whether the measuring pass of layout should use the AutoMeasure mechanism of RecyclerView or if it should be done by the LayoutManager's implementation of onMeasure(Recycler, State, int, int).

boolean isSmoothScrollbarEnabled()

Returns the current state of the smooth scrollbar feature.

void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler)

Called when this LayoutManager is detached from its parent RecyclerView or when its parent RecyclerView is detached from its window.

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

Called when searching for a focusable view in the given direction has failed for the current content of the RecyclerView.

void onInitializeAccessibilityEvent(AccessibilityEvent event)
void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)

Lay out all relevant child views from the given adapter.

void onLayoutCompleted(RecyclerView.State state)

Called after a full layout calculation is finished.

void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Called when the LayoutManager should save its state.

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

Scroll horizontally by dx pixels in screen coordinates and return the distance traveled.

void scrollToPosition(int position)

Scroll the RecyclerView to make the position visible.

void scrollToPositionWithOffset(int position, int offset)

Scroll to the specified adapter position with the given offset from resolved layout start.

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

Scroll vertically by dy pixels in screen coordinates and return the distance traveled.

void setInitialPrefetchItemCount(int itemCount)

Sets the number of items to prefetch in collectInitialPrefetchPositions(int, LayoutPrefetchRegistry), which defines how many inner items should be prefetched when this LayoutManager's RecyclerView is nested inside another RecyclerView.

void setOrientation(int orientation)

Sets the orientation of the layout.

void setRecycleChildrenOnDetach(boolean recycleChildrenOnDetach)

Set whether LayoutManager will recycle its children when it is detached from RecyclerView.

void setReverseLayout(boolean reverseLayout)

Used to reverse item traversal and layout order.

void setSmoothScrollbarEnabled(boolean enabled)

When smooth scrollbar is enabled, the position and size of the scrollbar thumb is computed based on the number of visible pixels in the visible items.

void setStackFromEnd(boolean stackFromEnd)

Compatibility support for setStackFromBottom(boolean)

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

Smooth scroll to the specified adapter position.

boolean supportsPredictiveItemAnimations()

Returns whether this LayoutManager supports "predictive item animations".

Protected methods

int getExtraLayoutSpace(RecyclerView.State state)

Returns the amount of extra space that should be laid out by LayoutManager.

boolean isLayoutRTL()

Inherited methods

From class android.support.v7.widget.RecyclerView.LayoutManager
From class java.lang.Object
From interface android.support.v7.widget.helper.ItemTouchHelper.ViewDropHandler
From interface android.support.v7.widget.RecyclerView.SmoothScroller.ScrollVectorProvider

XML attributes

RecyclerView_android_orientation

Related methods:

RecyclerView_reverseLayout

Related methods:

RecyclerView_stackFromEnd

Related methods:

Constants

HORIZONTAL

added in version 22.1.0
int HORIZONTAL

Constant Value: 0 (0x00000000)

INVALID_OFFSET

added in version 22.1.0
int INVALID_OFFSET

Constant Value: -2147483648 (0x80000000)

VERTICAL

added in version 22.1.0
int VERTICAL

Constant Value: 1 (0x00000001)

Public constructors

LinearLayoutManager

added in version 22.1.0
LinearLayoutManager (Context context)

Creates a vertical LinearLayoutManager

Parameters
context Context: Current context, will be used to access resources.

LinearLayoutManager

added in version 22.1.0
LinearLayoutManager (Context context, 
                int orientation, 
                boolean reverseLayout)

Parameters
context Context: Current context, will be used to access resources.

orientation int: Layout orientation. Should be HORIZONTAL or VERTICAL.

reverseLayout boolean: When set to true, layouts from end to start.

LinearLayoutManager

added in version 24.1.0
LinearLayoutManager (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Constructor used when layout manager is set in XML by RecyclerView attribute "layoutManager". Defaults to vertical orientation.

Related XML Attributes:

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

assertNotInLayoutOrScroll

added in version 22.1.0
void assertNotInLayoutOrScroll (String message)

Checks if RecyclerView is in the middle of a layout or scroll and throws an IllegalStateException if it is.

Parameters
message String: The message for the exception. Can be null.

canScrollHorizontally

added in version 22.1.0
boolean canScrollHorizontally ()

Query if horizontal scrolling is currently supported. The default implementation returns false.

Returns
boolean true if getOrientation() is HORIZONTAL

canScrollVertically

added in version 22.1.0
boolean canScrollVertically ()

Query if vertical scrolling is currently supported. The default implementation returns false.

Returns
boolean true if getOrientation() is VERTICAL

collectAdjacentPrefetchPositions

added in version 25.1.0
void collectAdjacentPrefetchPositions (int dx, 
                int dy, 
                RecyclerView.State state, 
                RecyclerView.LayoutManager.LayoutPrefetchRegistry layoutPrefetchRegistry)

Gather all positions from the LayoutManager to be prefetched, given specified momentum.

If item prefetch is enabled, this method is called in between traversals to gather which positions the LayoutManager will soon need, given upcoming movement in subsequent traversals.

The LayoutManager should call addPosition(int, int) for each item to be prepared, and these positions will have their ViewHolders created and bound, if there is sufficient time available, in advance of being needed by a scroll or layout.

Parameters
dx int: X movement component.

dy int: Y movement component.

state RecyclerView.State: State of RecyclerView

layoutPrefetchRegistry RecyclerView.LayoutManager.LayoutPrefetchRegistry: PrefetchRegistry to add prefetch entries into.

collectInitialPrefetchPositions

added in version 25.1.0
void collectInitialPrefetchPositions (int adapterItemCount, 
                RecyclerView.LayoutManager.LayoutPrefetchRegistry layoutPrefetchRegistry)

Gather all positions from the LayoutManager to be prefetched in preperation for its RecyclerView to come on screen, due to the movement of another, containing RecyclerView.

This method is only called when a RecyclerView is nested in another RecyclerView.

If item prefetch is enabled for this LayoutManager, as well in another containing LayoutManager, this method is called in between draw traversals to gather which positions this LayoutManager will first need, once it appears on the screen.

For example, if this LayoutManager represents a horizontally scrolling list within a vertically scrolling LayoutManager, this method would be called when the horizontal list is about to come onscreen.

The LayoutManager should call addPosition(int, int) for each item to be prepared, and these positions will have their ViewHolders created and bound, if there is sufficient time available, in advance of being needed by a scroll or layout.

Parameters
adapterItemCount int: number of items in the associated adapter.

layoutPrefetchRegistry RecyclerView.LayoutManager.LayoutPrefetchRegistry: PrefetchRegistry to add prefetch entries into.

computeHorizontalScrollExtent

added in version 22.1.0
int computeHorizontalScrollExtent (RecyclerView.State state)

Override this method if you want to support scroll bars.

Read computeHorizontalScrollExtent() for details.

Default implementation returns 0.

Parameters
state RecyclerView.State: Current state of RecyclerView

Returns
int The horizontal extent of the scrollbar's thumb

computeHorizontalScrollOffset

added in version 22.1.0
int computeHorizontalScrollOffset (RecyclerView.State state)

Override this method if you want to support scroll bars.

Read computeHorizontalScrollOffset() for details.

Default implementation returns 0.

Parameters
state RecyclerView.State: Current State of RecyclerView where you can find total item count

Returns
int The horizontal offset of the scrollbar's thumb

computeHorizontalScrollRange

added in version 22.1.0
int computeHorizontalScrollRange (RecyclerView.State state)

Override this method if you want to support scroll bars.

Read computeHorizontalScrollRange() for details.

Default implementation returns 0.

Parameters
state RecyclerView.State: Current State of RecyclerView where you can find total item count

Returns
int The total horizontal range represented by the vertical scrollbar

computeScrollVectorForPosition

added in version 22.1.0
PointF computeScrollVectorForPosition (int targetPosition)

Should calculate the vector that points to the direction where the target position can be found.

This method is used by the LinearSmoothScroller to initiate a scroll towards the target position.

The magnitude of the vector is not important. It is always normalized before being used by the LinearSmoothScroller.

LayoutManager should not check whether the position exists in the adapter or not.

Parameters
targetPosition int: the target position to which the returned vector should point

Returns
PointF the scroll vector for a given position.

computeVerticalScrollExtent

added in version 22.1.0
int computeVerticalScrollExtent (RecyclerView.State state)

Override this method if you want to support scroll bars.

Read computeVerticalScrollExtent() for details.

Default implementation returns 0.

Parameters
state RecyclerView.State: Current state of RecyclerView

Returns
int The vertical extent of the scrollbar's thumb

computeVerticalScrollOffset

added in version 22.1.0
int computeVerticalScrollOffset (RecyclerView.State state)

Override this method if you want to support scroll bars.

Read computeVerticalScrollOffset() for details.

Default implementation returns 0.

Parameters
state RecyclerView.State: Current State of RecyclerView where you can find total item count

Returns
int The vertical offset of the scrollbar's thumb

computeVerticalScrollRange

added in version 22.1.0
int computeVerticalScrollRange (RecyclerView.State state)

Override this method if you want to support scroll bars.

Read computeVerticalScrollRange() for details.

Default implementation returns 0.

Parameters
state RecyclerView.State: Current State of RecyclerView where you can find total item count

Returns
int The total vertical range represented by the vertical scrollbar

findFirstCompletelyVisibleItemPosition

added in version 22.1.0
int findFirstCompletelyVisibleItemPosition ()

Returns the adapter position of the first fully visible view. This position does not include adapter changes that were dispatched after the last layout pass.

Note that bounds check is only performed in the current orientation. That means, if LayoutManager is horizontal, it will only check the view's left and right edges.

Returns
int The adapter position of the first fully visible item or NO_POSITION if there aren't any visible items.

findFirstVisibleItemPosition

added in version 22.1.0
int findFirstVisibleItemPosition ()

Returns the adapter position of the first visible view. This position does not include adapter changes that were dispatched after the last layout pass.

Note that, this value is not affected by layout orientation or item order traversal. (setReverseLayout(boolean)). Views are sorted by their positions in the adapter, not in the layout.

If RecyclerView has item decorators, they will be considered in calculations as well.

LayoutManager may pre-cache some views that are not necessarily visible. Those views are ignored in this method.

Returns
int The adapter position of the first visible item or NO_POSITION if there aren't any visible items.

findLastCompletelyVisibleItemPosition

added in version 22.1.0
int findLastCompletelyVisibleItemPosition ()

Returns the adapter position of the last fully visible view. This position does not include adapter changes that were dispatched after the last layout pass.

Note that bounds check is only performed in the current orientation. That means, if LayoutManager is horizontal, it will only check the view's left and right edges.

Returns
int The adapter position of the last fully visible view or NO_POSITION if there aren't any visible items.

findLastVisibleItemPosition

added in version 22.1.0
int findLastVisibleItemPosition ()

Returns the adapter position of the last visible view. This position does not include adapter changes that were dispatched after the last layout pass.

Note that, this value is not affected by layout orientation or item order traversal. (