测试 RecyclerView:RecyclerViewActions 滚动得不够远

Testing a RecyclerView: RecyclerViewActions isn't scrolling far enough

我希望我的 espresso 测试向下滚动 RecyclerView,直到它看到某个视图并单击它。

因此我使用 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()
          )
)

测试将 RecyclerView 向下滚动,但只有所需数量的 50% 左右。点击调用因此而崩溃。

有人遇到过类似的事情吗?

谢谢!

我找到了查看操作不成功的原因:

该应用程序有一个可折叠的工具栏,在我开始滚动操作之前已将其展开。当我执行滚动操作时,工具栏首先折叠,然后回收站视图滚动。因此,回收站视图滚动到很小。

我找到的解决方案是先使用自定义 AppBarViewAction 折叠工具栏。已找到解决方案 here