Stay organized with collections Save and categorize content based on your preferences.

RecyclerViewActions

public final class RecyclerViewActions


ViewActions to interact RecyclerView. RecyclerView works differently than AdapterView. In fact, RecyclerView is not an AdapterView anymore, hence it can't be used in combination with Espresso#onData(Matcher).

To use ViewActions in this class use Espresso#onView(Matcher) with a Matcher that matches your RecyclerView, then perform a ViewAction from this class.

Summary

Nested types

public interface RecyclerViewActions.PositionableRecyclerViewAction

Most RecyclerViewActions are given a matcher to select a particular view / viewholder within the RecyclerView.

Public constructors

Public methods

static RecyclerViewActions.PositionableRecyclerViewAction
<VH extends ViewHolder> actionOnHolderItem(
    Matcher<VH> viewHolderMatcher,
    ViewAction viewAction
)

Performs a ViewAction on a view matched by viewHolderMatcher.

static RecyclerViewActions.PositionableRecyclerViewAction
<VH extends ViewHolder> actionOnItem(
    Matcher<View> itemViewMatcher,
    ViewAction viewAction
)

Performs a ViewAction on a view matched by viewHolderMatcher.

static ViewAction
<VH extends ViewHolder> actionOnItemAtPosition(
    int position,
    ViewAction viewAction
)

Performs a ViewAction on a view at position.

static RecyclerViewActions.PositionableRecyclerViewAction
<VH extends ViewHolder> scrollTo(Matcher<View> itemViewMatcher)

Returns a ViewAction which scrolls RecyclerView to the view matched by itemViewMatcher.

static RecyclerViewActions.PositionableRecyclerViewAction
<VH extends ViewHolder> scrollToHolder(Matcher<VH> viewHolderMatcher)

Returns a ViewAction which scrolls RecyclerView to the view matched by viewHolderMatcher.

static ViewAction
<VH extends ViewHolder> scrollToLastPosition()

Returns a ViewAction which scrolls RecyclerView to the last position.

static ViewAction
<VH extends ViewHolder> scrollToPosition(int position)

Returns a ViewAction which scrolls RecyclerView to a position.

Public constructors

RecyclerViewActions

public RecyclerViewActions()

Public methods

actionOnHolderItem

public static RecyclerViewActions.PositionableRecyclerViewAction <VH extends ViewHolder> actionOnHolderItem(
    Matcher<VH> viewHolderMatcher,
    ViewAction viewAction
)

Performs a ViewAction on a view matched by viewHolderMatcher.

  1. Scroll Recycler View to the view matched by itemViewMatcher
  2. Perform an action on the matched view
Note: actionOnItem method is not overloaded, method overloading with generic parameters is not possible.
Parameters
Matcher<VH> viewHolderMatcher

a Matcher that matches an item view holder in RecyclerView

ViewAction viewAction

the action that is performed on the view matched by viewHolderMatcher

Throws
androidx.test.espresso.PerformException androidx.test.espresso.PerformException

if there are more than one items matching given viewHolderMatcher.

actionOnItem

public static RecyclerViewActions.PositionableRecyclerViewAction <VH extends ViewHolder> actionOnItem(
    Matcher<View> itemViewMatcher,
    ViewAction viewAction
)

Performs a ViewAction on a view matched by viewHolderMatcher.

  1. Scroll Recycler View to the view matched by itemViewMatcher
  2. Perform an action on the matched view
Parameters
Matcher<View> itemViewMatcher

a Matcher that matches an item view in RecyclerView

ViewAction viewAction

the action that is performed on the view matched by itemViewMatcher

Throws
androidx.test.espresso.PerformException androidx.test.espresso.PerformException

if there are more than one items matching given viewHolderMatcher.

actionOnItemAtPosition

public static ViewAction <VH extends ViewHolder> actionOnItemAtPosition(
    int position,
    ViewAction viewAction
)

Performs a ViewAction on a view at position.

  1. Scroll Recycler View to position
  2. Perform an action on the view at position
Parameters
int position

position of a view in RecyclerView

ViewAction viewAction

the action that is performed on the view matched by itemViewMatcher

scrollTo

public static RecyclerViewActions.PositionableRecyclerViewAction <VH extends ViewHolder> scrollTo(Matcher<View> itemViewMatcher)

Returns a ViewAction which scrolls RecyclerView to the view matched by itemViewMatcher.

This approach uses ViewHolders to find the target view. It will create one ViewHolder per item type and bind adapter data to the ViewHolder. If the itemViewMatcher matches a ViewHolder the current position of the View is used to perform a scrollToPosition.

Parameters
Matcher<View> itemViewMatcher

a Matcher that matches an item view in RecyclerView

Throws
androidx.test.espresso.PerformException androidx.test.espresso.PerformException

if there are more than one items matching given viewHolderMatcher.

scrollToHolder

public static RecyclerViewActions.PositionableRecyclerViewAction <VH extends ViewHolder> scrollToHolder(Matcher<VH> viewHolderMatcher)

Returns a ViewAction which scrolls RecyclerView to the view matched by viewHolderMatcher.

This approach uses ViewHolders to find the target view. It will create one ViewHolder per item type and bind adapter data to the ViewHolder. If the itemViewMatcher matches a ViewHolder the current position of the View is used to perform a scrollToPosition. Note: scrollTo method is not overloaded, method overloading with generic parameters is not possible.

Parameters
Matcher<VH> viewHolderMatcher

a Matcher that matches an item view holder in RecyclerView

Throws
androidx.test.espresso.PerformException androidx.test.espresso.PerformException

if there are more than one items matching given viewHolderMatcher.

scrollToLastPosition

public static ViewAction <VH extends ViewHolder> scrollToLastPosition()

Returns a ViewAction which scrolls RecyclerView to the last position.

scrollToPosition

public static ViewAction <VH extends ViewHolder> scrollToPosition(int position)

Returns a ViewAction which scrolls RecyclerView to a position.

Parameters
int position

the position of the view to scroll to