问题
I want my espresso test to scroll a RecyclerView down until it's sees a certain view and clicks it.
Therefore I am using RecyclerViewActions
:
onView(withId(R.id.recycler)).perform(
RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(
hasDescendant(withText(R.string.title_item_x))
)
)
Thread.sleep(2000)
onView(withId(R.id.recycler)).perform(
RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>(
hasDescendant(withText(R.string.title_item_x)),
click()
)
)
The test scrolls the RecyclerView down, but only about 50% of the needed amount. The click call crashes because of it.
Does somebody encountered something similar?
Thanks!
回答1:
I've found the reason why the view action wasn't successul:
The app has a collapsible toolbar which was expanded before I started the scroll action. When I performed the scroll action the toolbar collapsed first and then the recycler view scrolled. Therefore the recycler view scrolled to little.
The solution I found is that I collapse the toolbar first with a custom AppBarViewAction. The solution is found here
来源:https://stackoverflow.com/questions/49072631/testing-a-recyclerview-recyclerviewactions-isnt-scrolling-far-enough