首页 > 解决方案 > 如何滚动 Recycler 视图并从每张卡片中收集文本?

问题描述

基本上我需要滚动浏览回收站视图并从每张卡片或单元格中收集数据,

我试过使用 RecyclerViewActions.scrollToPosition 像,

  1. 获得了 Recycler 视图的总数
  2. 使用 RecyclerViewActions.scrollToPosition 从 0 滚动到总数
  3. 使用本地列表视图计数中的索引从卡片或单元格中获取文本

是的,它有时可以工作,但是每次卡的大小不同时它都不能工作

是否有任何其他方式可以滚动并从每张卡片中获取文本或任何其他方式在所有情况下都能完美运行?

我试过的代码:

public int countofRecyclerView(int id){
    RecyclerView recyclerView=getCurrentActivity().findViewById(id);

    return recyclerView.getAdapter().getItemCount();
}
public  int localListviewcount (int listViewId) {
    onView(withId(listViewId)).check(matches(allOf(isCompletelyDisplayed(), isFocusable(), new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {}
        @Override
        protected boolean matchesSafely(View item) {
            if (item instanceof RecyclerView) {
                listCount = ((RecyclerView) item).getChildCount();
            } else if (item instanceof ListView) {
                listCount = ((ListView) item).getChildCount();
            }
            return true;
        }
    })));
    return listCount;
}

int totalCount=countofRecyclerView(id.recyclerview)

ArrayList<String>ab=new ArrayList<>()
int index=0

for(int i=0;i<totalCount;i++){
    onView(id.recyclerview).perform(RecyclerViewActions.scrollToPosition(i));
    int j=localListviewcount(id.recyclerview)
    if(i<j){
        index=j
    }else{
        index=i
    }
    ab.add(getTextfromId(id, index))
}

标签: androidandroid-recyclerviewandroid-espressoandroid-uiautomatorui-testing

解决方案


推荐阅读