Android APIs
public class

SwipeRefreshLayout

extends ViewGroup
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.support.v4.widget.SwipeRefreshLayout

Class Overview

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.OnRefreshListener Classes that wish to be notified when the swipe gesture correctly triggers a refresh should implement this interface. 
[Expand]
Inherited XML Attributes
From class android.view.ViewGroup
From class android.view.View
Constants
int DEFAULT
int LARGE
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
Fields
protected int mFrom
protected int mOriginalOffsetTop
[Expand]
Inherited Fields
From class android.view.View
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 isRefreshing()
boolean onInterceptTouchEvent(MotionEvent ev)
Implement this method to intercept all touch screen motion events.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

boolean onTouchEvent(MotionEvent ev)
Implement this method to handle touch screen motion events.
void requestDisallowInterceptTouchEvent(boolean b)
Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).
void setColorScheme(int... colors)
This method is deprecated. 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 setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)
Set the listener to be notified when a refresh is triggered via the swipe gesture.
void setProgressBackgroundColor(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.
Protected Methods
int getChildDrawingOrder(int childCount, int i)
Returns the index of the child to draw for this iteration.
void onLayout(boolean changed, int left, int top, int right, int bottom)
Called from layout when this view should assign a size and position to each of its children.
[Expand]
Inherited Methods
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Constants

public static final int DEFAULT

Constant Value: 1 (0x00000001)

public static final int LARGE

Constant Value: 0 (0x00000000)

Fields

protected int mFrom

protected int mOriginalOffsetTop

Public Constructors

public SwipeRefreshLayout (Context context)

Simple constructor to use when creating a SwipeRefreshLayout from code.

public SwipeRefreshLayout (Context context, AttributeSet attrs)

Constructor that is called when inflating SwipeRefreshLayout from XML.

Public Methods

public boolean canChildScrollUp ()

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

public boolean isRefreshing ()

Returns
  • Whether the SwipeRefreshWidget is actively showing refresh progress.

public boolean onInterceptTouchEvent (MotionEvent ev)

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

Parameters
ev The motion event being dispatched down the hierarchy.
Returns
  • Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

public void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

public boolean onTouchEvent (MotionEvent ev)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev The motion event.
Returns
  • True if the event was handled, false otherwise.

public void requestDisallowInterceptTouchEvent (boolean b)

Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.

Parameters
b True if the child does not want the parent to intercept touch events.

public void setColorScheme (int... colors)

This method is deprecated.
Use setColorSchemeResources(int)

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.

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.

public void setDistanceToTriggerSync (int distance)

Set the distance to trigger a sync in dips

public void setOnRefreshListener (SwipeRefreshLayout.OnRefreshListener listener)

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

public void setProgressBackgroundColor (int colorRes)

Set the background color of the progress spinner disc.

Parameters
colorRes Resource id of the color.

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 Set to true if there is no view at a higher z-order than where the progress spinner is set to appear.
end The offset in pixels from the top of this view at which the progress spinner should come to rest after a successful swipe gesture.

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.

Parameters
scale Set to true if there is no view at a higher z-order than where the progress spinner is set to appear.
start The offset in pixels from the top of this view at which the progress spinner should appear.
end The offset in pixels from the top of this view at which the progress spinner should come to rest after a successful swipe gesture.

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 Whether or not the view should show refresh progress.

public void setSize (int size)

One of DEFAULT, or LARGE.

Protected Methods

protected int getChildDrawingOrder (int childCount, int i)

Returns the index of the child to draw for this iteration. Override this if you want to change the drawing order of children. By default, it returns i.

NOTE: In order for this method to be called, you must enable child ordering first by calling setChildrenDrawingOrderEnabled(boolean).

Parameters
i The current iteration.
Returns
  • The index of the child to draw this iteration.

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

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent