【问题标题】:Click on last item in AdapterView in Espresso单击 Espresso 中 AdapterView 中的最后一项
【发布时间】:2018-03-25 23:12:20
【问题描述】:

我正在开发一个笔记应用程序。我添加了一个注释,它被添加到列表的底部。作为 espresso 测试中的最后一个断言,我想确保 ListView 显示一个刚刚添加的 listItem。这意味着抓取 listView 中的最后一项。我想你也许可以通过其他方式做到这一点? (例如,获取适应数据的大小,然后转到那个位置?也许?),但列表的最后一个位置似乎很容易,但我一直没能做到。有什么想法吗?

我已尝试过此解决方案,但 Espresso 似乎挂起。 http://www.gilvegliach.it/?id=1

【问题讨论】:

    标签: android listview android-espresso


    【解决方案1】:

    1.在 listView 的适配器中查找元素的数量并将其保存在某个变量中。我们假设适配器已经完全加载到现在。

        final int[] numberOfAdapterItems = new int[1]; 
        onView(withId(R.id.some_list_view)).check(matches(new TypeSafeMatcher<View>() {
            @Override
            public boolean matchesSafely(View view) {
                ListView listView = (ListView) view;
    
                //here we assume the adapter has been fully loaded already  
                numberOfAdapterItems[0] = listView.getAdapter().getCount();
    
                return true;
            }
    
            @Override
            public void describeTo(Description description) {
    
            }
        }));
    

    2。然后,知道 listView 的适配器中的元素总数,您可以滚动到最后一个元素:

        onData(anything()).inAdapterView(withId(R.id.some_list_view)).getPosition(numberOfAdapterItems[0] - 1).perform(scrollTo())                                  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多