Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 list which loads data when scroll reached the bottom).

I would like to test this, but I haven't found a way to do this. I use RecyclerViewActions which have scrollToPosition but even if I put the last position, the end is not reached (because the height of each element is high).

Anybody know how could I do this?

like image 704
LordWater Avatar asked Mar 20 '17 22:03

LordWater


People also ask

How does scrollto () work in espresso?

And in order for the scrollTo () to work, you need to provide a matcher that uniquely identifies that ItemView. Your current matcher tells espresso to scroll to a ViewHolder that was inflated with a TextView as an itemView.

How do I use recyclerview scrollto ()?

RecyclerViewActions.scrollTo () matches against the ItemView of the ViewHolder, which is inflated in onCreateViewHolder () of the adapter. And in order for the scrollTo () to work, you need to provide a matcher that uniquely identifies that ItemView.

Why does espresso scroll to a textview in a viewholder?

Your current matcher tells espresso to scroll to a ViewHolder that was inflated with a TextView as an itemView. Which can happen, but usually you have some ViewGroup action going on there to style the view holders in the way you want them to look.

How do recyclerviewactions work?

Most RecyclerViewActions are given a matcher to select a particular view / viewholder within the RecyclerView. Performs a ViewAction on a view matched by viewHolderMatcher.


2 Answers

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.

like image 78
Josh Laird Avatar answered Oct 22 '22 21:10

Josh Laird


you can implement ViewAction. like this:

class ScrollToBottomAction : ViewAction {
override fun getDescription(): String {
    return "scroll RecyclerView to bottom"
}

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

override fun perform(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())
like image 41
chunping mu Avatar answered Oct 22 '22 19:10

chunping mu