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

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

问题描述

CoordinatorLayout似乎破坏了诸如scrollTo()RecyclerViewActions.scrollToPosition()之类的Espresso操作的行为.

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.这是因为此操作仅支持ScrollView,而NestedScrollView不扩展它. 在此解释了此问题的解决方法,基本上,我们可以在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不同,在这里我可以看到一些滚动发生的情况.但是,它看起来像滚动到错误的位置.例如,如果我滚动到最后一个位置,则会显示倒数第二个而不是最后一个.当我将RecyclerViewCoordinatorLayout中移出时,滚动会按预期进行.

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& Horizo​​ntalScrollView.在内部,它使用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天全站免登陆