浓咖啡,当 NestedScrollView 或 RecyclerView 在 CoordinatorLayout 中时滚动不起作用 [英] Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout

本文介绍了浓咖啡,当 NestedScrollView 或 RecyclerView 在 CoordinatorLayout 中时滚动不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来 CoordinatorLayout 破坏了 Espresso 操作的行为,例如 scrollTo()RecyclerViewActions.scrollToPosition().

It looks like CoordinatorLayout breaks the behaviour of Espresso actions such as scrollTo() or RecyclerViewActions.scrollToPosition().

对于这样的布局:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        ...

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        ...

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

如果我尝试使用 ViewActions.scrollTo() 滚动到 NestedScrollView 内的任何视图,我发现的第一个问题是我得到一个 PerformException.这是因为此操作仅支持 ScrollViewNestedScrollView 不扩展它.这里解释了这个问题的解决方法,基本上我们可以复制scrollTo()中的代码并更改约束以支持 NestedScrollView.如果 NestedScrollView 不在 CoordinatorLayout 中,这似乎有效,但只要将其放入 CoordinatorLayout 中,滚动操作就会失败.

If I try to scroll to any view inside the NestedScrollView using ViewActions.scrollTo() the first problem I find is that I get a PerformException. This is because this action only supports ScrollView and NestedScrollView doesn't extend it. A workaround for this problem is explained here, basically we can copy the code in scrollTo() and change the constrains to support NestedScrollView. This seems to work if the NestedScrollView is not in a CoordinatorLayout but as soon as you put it inside a the CoordinatorLayout the scrolling action fails.

对于相同的布局,如果我将 NestedScrollView 替换为 RecyclerView,滚动也会出现问题.

For the same layout, if I replace the NestedScrollView with a RecyclerView there is also problems with the the scrolling.

在这种情况下,我使用 RecyclerViewAction.scrollToPosition(position).与 NestedScrollView 不同,在这里我可以看到一些滚动发生.但是,它看起来滚动到错误的位置.例如,如果我滚动到最后一个位置,它会显示倒数第二个但不是最后一个.当我将 RecyclerView 移出 CoordinatorLayout 时,滚动会正常工作.

In this case I'm using RecyclerViewAction.scrollToPosition(position). Unlike the NestedScrollView, here I can see some scrolling happening. However, it looks like it scrolls to the wrong position. For example, if I scroll to the last position, it makes visible the second to last but not the last one. When I move the RecyclerView out of the CoordinatorLayout the scrolling works as it should.

由于这个问题,目前我们无法为使用 CoordinatorLayout 的屏幕编写任何 Espresso 测试.有人遇到同样的问题或知道解决方法吗?

At the moment we can't write any Espresso test for the screens that use CoordinatorLayout due to this issues. Anyone experiencing the same problems or knows a workaround?

推荐答案

发生这种情况是因为 Espresso scrollTo() 方法显式检查布局类并且仅适用于 ScrollView &水平滚动视图.它在内部使用 View.requestRectangleOnScreen(...) 所以我希望它实际上适用于许多布局.

This is happening because the Espresso scrollTo() method explicitly checks the layout class and only works for ScrollView & HorizontalScrollView. Internally it's using View.requestRectangleOnScreen(...) so I'd expect it to actually work fine for many layouts.

我对 NestedScrollView 的解决方法是采用 ScrollToAction 并修改该约束.修改后的操作适用于具有该更改的 NestedScrollView.

My workaround for NestedScrollView was to take ScrollToAction and modify that constraint. The modified action worked fine for NestedScrollView with that change.

ScrollToAction 类中的更改方法:

Changed method in ScrollToAction class:

public Matcher<View> getConstraints() {
    return allOf(withEffectiveVisibility(Visibility.VISIBLE), isDescendantOfA(anyOf(
            isAssignableFrom(ScrollView.class), isAssignableFrom(HorizontalScrollView.class), isAssignableFrom(NestedScrollView.class))));
}

便捷方法:

public static ViewAction betterScrollTo() {
    return ViewActions.actionWithAssertions(new NestedScrollToAction());
}

这篇关于浓咖啡,当 NestedScrollView 或 RecyclerView 在 CoordinatorLayout 中时滚动不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆