首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >浓缩咖啡NestedScrollView

浓缩咖啡NestedScrollView
EN

Stack Overflow用户
提问于 2015-07-24 15:57:08
回答 4查看 6.7K关注 0票数 12

我正在尝试以我的形式测试EditTexts,该表单位于NestedScrollView中。我正在运行以下代码:

代码语言:javascript
复制
onView(withId(R.id.register_scroll_view)).perform(scrollTo()).perform(click());

register_scroll_view是我的NestedScrollView。然而,我得到了一个例外:

使用id: com.eazyigz.myapp:id/ android.support.test.espresso.PerformException:_scroll执行‘滚动到’on view‘时出错。由: java.lang.RuntimeException: Action引起的操作将不会执行,因为目标视图不匹配以下一个或多个约束:(视图具有有效的visibility=VISIBLE,并且是a:(可从类: android.widget.ScrollView分配或从类:android.widget.HorizontalScrollView分配)的后代)

如何正确地设计这个测试,以便测试需要滚动才能显示的EditTexts?

EN

回答 4

Stack Overflow用户

发布于 2015-07-25 18:47:37

我对NestedScrollView没有任何经验,但似乎requestRectangleOnScreen(),也就是普通ScrollView中的浓缩咖啡卷轴,应该与NestedScrollView一起工作。

唯一的问题是ScrollView约束被硬编码到scrollTo()操作中,而NestedScrollView没有继承常规的ScrollView。

我认为这里唯一的解决方案是将整个ScrollToAction类复制并粘贴到您自己的操作实现中,并替换讨厌的约束。

票数 15
EN

Stack Overflow用户

发布于 2017-05-30 23:42:13

我编写了一个ViewAction,用于处理滚动到NestedScrollView子视图的视图。它还考虑到CoordinatorLayout可能是根-所以您不需要害怕工具栏改变它的大小。

有一些密码。您需要将这个类复制到某个项目中。然后您可以使用它,例如:

代码语言:javascript
复制
onView(withId(R.id.register_scroll_view))
        .perform(CustomScrollActions.nestedScrollTo, click());

重要:它不是scrollTo()的替代品,而是在处理NestedScrollView时应该使用的另一个滚动ViewAction。

所以我说的是一个ViewAction:

代码语言:javascript
复制
public class CustomScrollActions {

    public static ViewAction nestedScrollTo() {
        return new ViewAction() {

            @Override
            public Matcher<View> getConstraints() {
                return Matchers.allOf(
                        isDescendantOfA(isAssignableFrom(NestedScrollView.class)),
                        withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE));
            }

            @Override
            public String getDescription() {
                return "Find parent with type " + NestedScrollView.class +
                        " of matched view and programmatically scroll to it.";
            }

            @Override
            public void perform(UiController uiController, View view) {
                try {
                    NestedScrollView nestedScrollView = (NestedScrollView)
                            findFirstParentLayoutOfClass(view, NestedScrollView.class);
                    if (nestedScrollView != null) {
                        CoordinatorLayout coordinatorLayout =
                                (CoordinatorLayout) findFirstParentLayoutOfClass(view, CoordinatorLayout.class);
                        if (coordinatorLayout != null) {
                            CollapsingToolbarLayout collapsingToolbarLayout =
                                    findCollapsingToolbarLayoutChildIn(coordinatorLayout);
                            if (collapsingToolbarLayout != null) {
                                int toolbarHeight = collapsingToolbarLayout.getHeight();
                                nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
                                nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
                            }
                        }
                        nestedScrollView.scrollTo(0, view.getTop());
                    } else {
                        throw new Exception("Unable to find NestedScrollView parent.");
                    }
                } catch (Exception e) {
                    throw new PerformException.Builder()
                            .withActionDescription(this.getDescription())
                            .withViewDescription(HumanReadables.describe(view))
                            .withCause(e)
                            .build();
                }
                uiController.loopMainThreadUntilIdle();
            }
        };
    }

    private static CollapsingToolbarLayout findCollapsingToolbarLayoutChildIn(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            if (child instanceof CollapsingToolbarLayout) {
                return (CollapsingToolbarLayout) child;
            } else if (child instanceof ViewGroup) {
                return findCollapsingToolbarLayoutChildIn((ViewGroup) child);
            }
        }
        return null;
    }

    private static View findFirstParentLayoutOfClass(View view, Class<? extends View> parentClass) {
        ViewParent parent = new FrameLayout(view.getContext());
        ViewParent incrementView = null;
        int i = 0;
        while (parent != null && !(parent.getClass() == parentClass)) {
            if (i == 0) {
                parent = findParent(view);
            } else {
                parent = findParent(incrementView);
            }
            incrementView = parent;
            i++;
        }
        return (View) parent;
    }

    private static ViewParent findParent(View view) {
        return view.getParent();
    }

    private static ViewParent findParent(ViewParent view) {
        return view.getParent();
    }
}
票数 8
EN

Stack Overflow用户

发布于 2018-09-06 14:17:41

使用

代码语言:javascript
复制
onView(withId(R.id.register_scroll_view))
        .perform(swipeUp(), click())
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31614937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档