Testing a RecyclerView: RecyclerViewActions isn't scrolling far enough

断了今生、忘了曾经 提交于 2019-12-11 16:47:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!