如何获取适配器视图中没有的具有相同ID的项目计数 [英] How to get count of items with same ids which are not in adapter view

查看:78
本文介绍了如何获取适配器视图中没有的具有相同ID的项目计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个项目的多个实例,我想对它们进行计数的情况. 我的视图匹配器如下:

I have the case where multiple instances of an item are there and I want to count them. My view matcher is as follows:

 public static ViewInteraction onTestPanelView(){
        return onView(allOf(withId(R.id.myId), isDisplayed()));
    }

使用视图匹配器,出现以下错误:

With the view matcher, I get the following error:

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: '(id为:< 2131427517>,并在屏幕上显示为 用户)匹配层次结构中的多个视图.问题视图是 在下面标有"**** MATCHES ****".

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: '(with id: is <2131427517> and is displayed on the screen to the user)' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.

这是正确的,因为我有多个具有相同ID(R.id.myId)的项目实例.我想编写一个方法,该方法返回符合我的条件的视图数.请注意-它们不在适配器视图中.

This is correct because I have the multiple instance of items with same id (R.id.myId). I want to write a method which returns me the count of the views matching my criteria. Please note - they are not in adapter view.

推荐答案

您可以将放置在onView()中的条件包装到匹配器中,然后在匹配器中放置一个计数器.匹配器每次匹配该项目时,计数器都应递增.

You can wrap the condition you place in onView() into matcher and put a counter inside the matcher. Each time the matcher will match the item the counter should be incremented.

int counter = 0;

public static Matcher<View> withIdAndDisplayed(final int id) {
    Checks.checkNotNull(id);
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("with item id: " + id);
        }

        @Override
        public boolean matchesSafely(View view) {
            if ((view.getId() == id) && (view.getGlobalVisibleRect(new Rect())
                    && withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view))){
                counter++;
                return true;
            }
            return false;
        }
    };
}

public static ViewInteraction onTestPanelView(){
    return onView(withIdAndDisplayed(R.id.myId));
}

这篇关于如何获取适配器视图中没有的具有相同ID的项目计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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