【问题标题】:RecyclerView not scrolling on android espresso testRecyclerView 在 android espresso 测试中不滚动
【发布时间】:2019-07-26 16:23:46
【问题描述】:

我想滚动一个 recyclerView 并点击一个最初在屏幕上不可见的项目,使用 Espresso,但滚动操作不起作用(它在其他活动中的其他 recyclerViews 的其他测试中起作用)。

我已经尝试过https://developer.android.com/training/testing/espresso/lists中指出的onData()方法

但它也不会起作用。

这是我执行滚动并单击的方法:

fun scrollToPositionAndClick(resId: Int, position: Int) {
        getActivityInstance()?.findViewById<RecyclerView>(resId)?.adapter?.let {
            onView(withId(resId))
                .perform(
                    scrollToPosition<RecyclerView.ViewHolder>(position),
                    actionOnItemAtPosition<RecyclerView.ViewHolder>(position, click())
                )
        }
    }

此方法适用于其他活动测试,但由于某种原因它不适用于此屏幕(片段),我收到此错误:

androidx.test.espresso.PerformException: Error performing 'single click - At Coordinates: 290, 2197 and precision: 16, 16'
...
Caused by: androidx.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.

【问题讨论】:

  • 嘿,你找到解决这个问题的办法了吗?

标签: android kotlin android-recyclerview automated-tests android-espresso


【解决方案1】:

当我在 ViewPager 中有一个 RecyclerView 时,我遇到了这个不滚动的问题。我可以通过自己实现 RecyclerViewAction.scrollToPosition 来修复它。我没有使用recyclerView滚动到的API,而是使用了LayoutManager的API,如果它是LinearLayoutManager,我使用了scrollToPositionWithOffset。

这是操作的代码。除了perform 方法之外,一切都和原来的一样

public final class CustomRecyclerViewActions {

private CustomRecyclerViewActions() { }


public static <VH extends RecyclerView.ViewHolder> ViewAction scrollToPosition(@IntRange(from = 0) final int position) {
   return new ScrollToPositionViewAction(position);
}

private static final class ScrollToPositionViewAction implements ViewAction {
   private final int position;

   private ScrollToPositionViewAction(int position) {
     this.position = position;
   }

   public Matcher<View> getConstraints() {
      return Matchers.allOf(ViewMatchers.isAssignableFrom(RecyclerView.class), ViewMatchers.isDisplayed());
   }

  public String getDescription() {
    int var1 = this.position;
    return (new StringBuilder(44)).append("scroll RecyclerView to position: ").append(var1).toString();
  }

  public void perform(@NonNull UiController uiController, @NonNull View view) {
    RecyclerView recyclerView = (RecyclerView)view;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
      ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(this.position, 0);
    } else {
      layoutManager.scrollToPosition(this.position);
    }
    uiController.loopMainThreadUntilIdle();
   }
 }
}

【讨论】:

  • 是的,我的问题很相似,原来我的 recyclerView 在一个 nestedScrollView 内,滚动 NestedScrollView 而不是 recyclerView 解决了这个问题。见medium.com/@devasierra/…
  • 是的,这行得通,但是请仔细检查您是否认为 recyclerview 中的视图应该被回收,因为在 nestedScrollView 中关闭它会关闭列表中的回收。
猜你喜欢
  • 2016-12-08
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多