Skip to content Skip to sidebar Skip to footer

Espresso Recyclerview Scroll To End

I have an Android application which has a RecyclerView with N elements, and when this RecyclerView reaches to end when scrolling, then more elements are added (so, it's an infinite

Solution 1:

I use the below to scroll to the bottom of my RecyclerView.

activity = mActivityTestRule.launchActivity(startingIntent);

onView(withId(R.id.recyclerView)).perform(
    RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
        activity.recyclerView.getAdapter().getItemCount() - 1
    )
);

You'll then have to use idling resources (or Thread.sleep()) to call this again when more data has loaded.

Solution 2:

you can implement ViewAction. like this:

classScrollToBottomAction : ViewAction {overridefungetDescription(): String {
    return"scroll RecyclerView to bottom"
}

overridefungetConstraints(): Matcher<View> {
    return allOf<View>(isAssignableFrom(RecyclerView::class.java), isDisplayed())
}

overridefunperform(uiController: UiController?, view: View?) {
    val recyclerView = view as RecyclerView
    val itemCount = recyclerView.adapter?.itemCount
    val position = itemCount?.minus(1) ?: 0
    recyclerView.scrollToPosition(position)
    uiController?.loopMainThreadUntilIdle()
}
}

and then use it like this:

onView(withId(R.id.recyclerView)).perform(ScrollToBottomAction())

Solution 3:

I use this ;

// Get total item of myRecyclerViewRecyclerViewrecyclerView= mActivityTestRule.getActivity().findViewById(R.id.myRecyclerView);
intitemCount= recyclerView.getAdapter().getItemCount();

Log.d("Item count is ", String.valueOf(itemCount));

// Scroll to end of page with position
onView(withId(R.id.myRecyclerView))
    .perform(RecyclerViewActions.scrollToPosition(itemCount - 1));

Post a Comment for "Espresso Recyclerview Scroll To End"