【问题标题】:Espresso Recyclerview scroll to endEspresso Recyclerview 滚动到结束
【发布时间】:2017-03-20 22:25:32
【问题描述】:

我有一个 Android 应用程序,它有一个带有 N 个元素的 RecyclerView,当这个 RecyclerView 在滚动时到达结束时,会添加更多元素(因此,它是一个无限列表,当滚动到底部时会加载数据)。

我想对此进行测试,但我还没有找到方法。我使用具有 scrollToPosition 的 RecyclerViewActions 但即使我放置了最后一个位置,也没有到达终点(因为每个元素的高度都很高)。

有人知道我该怎么做吗?

【问题讨论】:

  • 如果您在测试期间执行了拖动事件怎么办?像这样 - qathread.blogspot.com/2014/01/…
  • 测试进行得如此之快,以至于我没有足够的时间将其向下滑动。我也尝试使用 Espresso Recorder,但没有捕获拖动事件。

标签: android android-recyclerview android-espresso


【解决方案1】:

我使用下面滚动到我的RecyclerView 的底部。

activity = mActivityTestRule.launchActivity(startingIntent);

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

当加载更多数据时,您必须使用idling resources(或Thread.sleep())再次调用它。

【讨论】:

  • 我错过了房车有自己的卷轴。谢谢。
  • 问题表明他已经在使用它。但是没有到达底部,因为最后一个元素比屏幕大。 scrollToPosition 只是滚动到项目的顶部边缘。因此,比屏幕大的项目仍有部分隐藏在屏幕底部边缘下方。即使使用此代码,我也遇到了同样的问题。我有点困惑为什么这是被接受的,因为它只会滑到最后一个元素的顶部,而不是 RecyclerView 的最底部。
  • ScrollToposition 现在将 ViewHolder 名称作为类型。建议对相同的修改
【解决方案2】:

您可以实现 ViewAction。像这样:

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()
}
}

然后像这样使用它:

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

【讨论】:

  • 你是大师。
【解决方案3】:

我用这个;

 // Get total item of myRecyclerView
RecyclerView recyclerView = mActivityTestRule.getActivity().findViewById(R.id.myRecyclerView);
int itemCount = 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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多