added in version 1.0.0
belongs to Maven artifact androidx.swiperefreshlayout:swiperefreshlayout:1.0.0-beta01

SwipeRefreshLayout

public class SwipeRefreshLayout
extends ViewGroup implements NestedScrollingParent, NestedScrollingChild

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.swiperefreshlayout.widget.SwipeRefreshLayout


The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.

Summary

Nested classes

interface SwipeRefreshLayout.OnChildScrollUpCallback

Classes that wish to override SwipeRefreshLayout.canChildScrollUp() method behavior should implement this interface. 

interface SwipeRefreshLayout.OnRefreshListener

Classes that wish to be notified when the swipe gesture correctly triggers a refresh should implement this interface. 

Constants

int DEFAULT

int DEFAULT_SLINGSHOT_DISTANCE

int LARGE

Inherited constants

Fields

protected int mFrom

protected int mOriginalOffsetTop

Inherited fields

Public constructors

SwipeRefreshLayout(Context context)

Simple constructor to use when creating a SwipeRefreshLayout from code.

SwipeRefreshLayout(Context context, AttributeSet attrs)

Constructor that is called when inflating SwipeRefreshLayout from XML.

Public methods

boolean canChildScrollUp()
boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean dispatchNestedPreFling(float velocityX, float velocityY)
boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow)
boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow)
int getNestedScrollAxes()
int getProgressCircleDiameter()

Get the diameter of the progress circle that is displayed as part of the swipe to refresh layout.

int getProgressViewEndOffset()
int getProgressViewStartOffset()
boolean hasNestedScrollingParent()
boolean isNestedScrollingEnabled()
boolean isRefreshing()
boolean onInterceptTouchEvent(MotionEvent ev)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed)
boolean onNestedPreFling(View target, float velocityX, float velocityY)
void onNestedPreScroll(View target, int dx, int dy, int[] consumed)
void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)
void onNestedScrollAccepted(View child, View target, int axes)
boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)
void onStopNestedScroll(View target)
boolean onTouchEvent(MotionEvent ev)
void requestDisallowInterceptTouchEvent(boolean b)
void setColorScheme(int... colors)

This method was deprecated in API level 1.0.0. Use setColorSchemeResources(int)

void setColorSchemeColors(int... colors)

Set the colors used in the progress animation.

void setColorSchemeResources(int... colorResIds)

Set the color resources used in the progress animation from color resources.

void setDistanceToTriggerSync(int distance)

Set the distance to trigger a sync in dips

void setEnabled(boolean enabled)
void setNestedScrollingEnabled(boolean enabled)
void setOnChildScrollUpCallback(SwipeRefreshLayout.OnChildScrollUpCallback callback)

Set a callback to override canChildScrollUp() method.

void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)

Set the listener to be notified when a refresh is triggered via the swipe gesture.

void setProgressBackgroundColor(int colorRes)

This method was deprecated in API level 1.0.0. Use setProgressBackgroundColorSchemeResource(int)

void setProgressBackgroundColorSchemeColor(int color)

Set the background color of the progress spinner disc.

void setProgressBackgroundColorSchemeResource(int colorRes)

Set the background color of the progress spinner disc.

void setProgressViewEndTarget(boolean scale, int end)

The refresh indicator resting position is always positioned near the top of the refreshing content.

void setProgressViewOffset(boolean scale, int start, int end)

The refresh indicator starting and resting position is always positioned near the top of the refreshing content.

void setRefreshing(boolean refreshing)

Notify the widget that refresh state has changed.

void setSize(int size)

One of DEFAULT, or LARGE.

void setSlingshotDistance(int slingshotDistance)

Sets a custom slingshot distance.

boolean startNestedScroll(int axes)
void stopNestedScroll()

Protected methods

int getChildDrawingOrder(int childCount, int i)
void onDetachedFromWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)

Inherited methods

Constants

DEFAULT

added in version 1.0.0
public static final int DEFAULT

Constant Value: 1 (0x00000001)

DEFAULT_SLINGSHOT_DISTANCE

added in version 1.0.0
public static final int DEFAULT_SLINGSHOT_DISTANCE

Constant Value: -1 (0xffffffff)

LARGE

added in version 1.0.0
public static final int LARGE

Constant Value: 0 (0x00000000)

Fields

mFrom

added in version 1.0.0
protected int mFrom

mOriginalOffsetTop

added in version 1.0.0
protected int mOriginalOffsetTop

Public constructors

SwipeRefreshLayout

added in version 1.0.0
public SwipeRefreshLayout (Context context)

Simple constructor to use when creating a SwipeRefreshLayout from code.

SwipeRefreshLayout

added in version 1.0.0
public SwipeRefreshLayout (Context context, 
                AttributeSet attrs)

Constructor that is called when inflating SwipeRefreshLayout from XML.

Public methods

canChildScrollUp

added in version 1.0.0
public boolean canChildScrollUp ()

Returns
boolean Whether it is possible for the child view of this layout to scroll up. Override this if the child view is a custom view.

dispatchNestedFling

public boolean dispatchNestedFling (float velocityX, 
                float velocityY, 
                boolean consumed)

Parameters
velocityX float

velocityY float

consumed boolean

Returns
boolean

dispatchNestedPreFling

public boolean dispatchNestedPreFling (float velocityX, 
                float velocityY)

Parameters
velocityX float

velocityY float

Returns
boolean

dispatchNestedPreScroll

public boolean dispatchNestedPreScroll (int dx, 
                int dy, 
                int[] consumed, 
                int[] offsetInWindow)

Parameters
dx int

dy int

consumed int

offsetInWindow int

Returns
boolean

dispatchNestedScroll

public boolean dispatchNestedScroll (int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed, 
                int[] offsetInWindow)

Parameters
dxConsumed int

dyConsumed int

dxUnconsumed int

dyUnconsumed int

offsetInWindow int

Returns
boolean

getNestedScrollAxes

public int getNestedScrollAxes ()

Returns
int

getProgressCircleDiameter

added in version 1.0.0
public int getProgressCircleDiameter ()

Get the diameter of the progress circle that is displayed as part of the swipe to refresh layout.

Returns
int Diameter in pixels of the progress circle view.

getProgressViewEndOffset

added in version 1.0.0
public int getProgressViewEndOffset ()

Returns
int The offset in pixels from the top of this view at which the progress spinner should come to rest after a successful swipe gesture.

getProgressViewStartOffset

added in version 1.0.0
public int getProgressViewStartOffset ()

Returns
int The offset in pixels from the top of this view at which the progress spinner should appear.

hasNestedScrollingParent

public boolean hasNestedScrollingParent ()

Returns
boolean

isNestedScrollingEnabled

public boolean isNestedScrollingEnabled ()

Returns
boolean

isRefreshing

added in version 1.0.0
public boolean isRefreshing ()

Returns
boolean Whether the SwipeRefreshWidget is actively showing refresh progress.

onInterceptTouchEvent

public boolean onInterceptTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onMeasure

added in version 1.0.0
public void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onNestedFling

public boolean onNestedFling (View target, 
                float velocityX, 
                float velocityY, 
                boolean consumed)

Parameters
target View

velocityX float

velocityY float

consumed boolean

Returns
boolean

onNestedPreFling

public boolean onNestedPreFling (View target, 
                float velocityX, 
                float velocityY)

Parameters
target View

velocityX float

velocityY float

Returns
boolean

onNestedPreScroll

public void onNestedPreScroll (View target, 
                int dx, 
                int dy, 
                int[] consumed)

Parameters
target View

dx int

dy int

consumed int

onNestedScroll

public void onNestedScroll (View target, 
                int dxConsumed, 
                int dyConsumed, 
                int dxUnconsumed, 
                int dyUnconsumed)

Parameters
target View

dxConsumed int

dyConsumed int

dxUnconsumed int

dyUnconsumed int

onNestedScrollAccepted

public void onNestedScrollAccepted (View child, 
                View target, 
                int axes)

Parameters
child View

target View

axes int

onStartNestedScroll

public boolean onStartNestedScroll (View child, 
                View target, 
                int nestedScrollAxes)

Parameters
child View

target View

nestedScrollAxes int

Returns
boolean

onStopNestedScroll

public void onStopNestedScroll (View target)

Parameters
target View

onTouchEvent

public boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

requestDisallowInterceptTouchEvent

public void requestDisallowInterceptTouchEvent (boolean b)

Parameters
b boolean

setColorScheme

added in version 1.0.0
public void setColorScheme (int... colors)

This method was deprecated in API level 1.0.0.
Use setColorSchemeResources(int)

Parameters
colors int

setColorSchemeColors

added in version 1.0.0
public void setColorSchemeColors (int... colors)

Set the colors used in the progress animation. The first color will also be the color of the bar that grows in response to a user swipe gesture.

setColorSchemeResources

added in version 1.0.0
public void setColorSchemeResources (int... colorResIds)

Set the color resources used in the progress animation from color resources. The first color will also be the color of the bar that grows in response to a user swipe gesture.

setDistanceToTriggerSync

added in version 1.0.0
public void setDistanceToTriggerSync (int distance)

Set the distance to trigger a sync in dips

setEnabled

public void setEnabled (boolean enabled)

Parameters
enabled boolean

setNestedScrollingEnabled

public void setNestedScrollingEnabled (boolean enabled)

Parameters
enabled boolean

setOnChildScrollUpCallback

added in version 1.0.0
public void setOnChildScrollUpCallback (SwipeRefreshLayout.OnChildScrollUpCallback callback)

Set a callback to override canChildScrollUp() method. Non-null callback will return the value provided by the callback and ignore all internal logic.

Parameters
callback SwipeRefreshLayout.OnChildScrollUpCallback: Callback that should be called when canChildScrollUp() is called.

setOnRefreshListener

added in version 1.0.0
public void setOnRefreshListener (SwipeRefreshLayout.OnRefreshListener listener)

Set the listener to be notified when a refresh is triggered via the swipe gesture.

Parameters
listener SwipeRefreshLayout.OnRefreshListener

setProgressBackgroundColor

added in version 1.0.0
public void setProgressBackgroundColor (int colorRes)

This method was deprecated in API level 1.0.0.
Use setProgressBackgroundColorSchemeResource(int)

Parameters
colorRes int

setProgressBackgroundColorSchemeColor

added in version 1.0.0
public void setProgressBackgroundColorSchemeColor (int color)

Set the background color of the progress spinner disc.

setProgressBackgroundColorSchemeResource

added in version 1.0.0
public void setProgressBackgroundColorSchemeResource (int colorRes)

Set the background color of the progress spinner disc.

Parameters
colorRes int: Resource id of the color.

setProgressViewEndTarget

added in version 1.0.0
public void setProgressViewEndTarget (boolean scale, 
                int end)

The refresh indicator resting position is always positioned near the top of the refreshing content. This position is a consistent location, but can be adjusted in either direction based on whether or not there is a toolbar or actionbar present.

Parameters
scale boolean: Set to true if there is no view at a higher z-order than where the progress spinner is set to appear. Setting it to true will cause indicator to be scaled up rather than clipped.

end int: The offset in pixels from the top of this view at which the progress spinner should come to rest after a successful swipe gesture.

setProgressViewOffset

added in version 1.0.0
public void setProgressViewOffset (boolean scale, 
                int start, 
                int end)

The refresh indicator starting and resting position is always positioned near the top of the refreshing content. This position is a consistent location, but can be adjusted in either direction based on whether or not there is a toolbar or actionbar present.

Note: Calling this will reset the position of the refresh indicator to start.

Parameters
scale boolean: Set to true if there is no view at a higher z-order than where the progress spinner is set to appear. Setting it to true will cause indicator to be scaled up rather than clipped.

start int: The offset in pixels from the top of this view at which the progress spinner should appear.

end int: The offset in pixels from the top of this view at which the progress spinner should come to rest after a successful swipe gesture.

setRefreshing

added in version 1.0.0
public void setRefreshing (boolean refreshing)

Notify the widget that refresh state has changed. Do not call this when refresh is triggered by a swipe gesture.

Parameters
refreshing boolean: Whether or not the view should show refresh progress.

setSize

added in version 1.0.0
public void setSize (int size)

One of DEFAULT, or LARGE.

Parameters
size int

setSlingshotDistance

added in version 1.0.0
public void setSlingshotDistance (int slingshotDistance)

Sets a custom slingshot distance.

Parameters
slingshotDistance int: The distance in pixels that the refresh indicator can be pulled beyond its resting position. Use DEFAULT_SLINGSHOT_DISTANCE to reset to the default value.

startNestedScroll

public boolean startNestedScroll (int axes)

Parameters
axes int

Returns
boolean

stopNestedScroll

public void stopNestedScroll ()

Protected methods

getChildDrawingOrder

protected int getChildDrawingOrder (int childCount, 
                int i)

Parameters
childCount int

i int

Returns
int

onDetachedFromWindow

protected void onDetachedFromWindow ()

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int