Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android espresso中访问recycler视图的子视图?_Android_Android Recyclerview_Android Espresso - Fatal编程技术网

如何在android espresso中访问recycler视图的子视图?

如何在android espresso中访问recycler视图的子视图?,android,android-recyclerview,android-espresso,Android,Android Recyclerview,Android Espresso,我正在为Recycler view编写浓缩咖啡单元测试。 在“回收器”视图中,我有可单击的文本视图。我想在浓缩咖啡测试中的文本视图上执行单击事件。那么如何访问该文本视图呢?您可以使用 包括格拉德尔: dependencies { androidTestCompile 'com.android.support.test:testing-support-lib:0.1' androidTestCompile 'com.android.support.test.espresso:esp

我正在为Recycler view编写浓缩咖啡单元测试。 在“回收器”视图中,我有可单击的文本视图。我想在浓缩咖啡测试中的文本视图上执行单击事件。那么如何访问该文本视图呢?

您可以使用

包括格拉德尔:

dependencies {
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
你可以用

包括格拉德尔:

dependencies {
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

试试这个,我也测试过同样的

onView(withRecyclerView(R.id.recyclerView)
      .atPositionOnView(position, R.id.childView))
      .perform(click());
方法调用

public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
    return new RecyclerViewMatcher(recyclerViewId);
}
RecycleServiceWMatcher

import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class RecyclerViewMatcher {

private final int recyclerViewId;

public RecyclerViewMatcher(int recyclerViewId) {
    this.recyclerViewId = recyclerViewId;
}

public Matcher<View> atPosition(final int position) {
    return atPositionOnView(position, -1);
}

public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
    return new TypeSafeMatcher<View>() {
        Resources resources = null;
        View childView;

        public void describeTo(Description description) {
            String idDescription = Integer.toString(recyclerViewId);
            if (this.resources != null) {
                try {
                    idDescription = this.resources.getResourceName(recyclerViewId);
                } catch (Resources.NotFoundException var4) {
                    idDescription = String.format("%s (resource name not found)", recyclerViewId);
                }
            }
            description.appendText("with id: " + idDescription);
        }

        public boolean matchesSafely(View view) {
            this.resources = view.getResources();
            if (childView == null) {
                RecyclerView recyclerView = (RecyclerView) view.getRootView().findViewById(recyclerViewId);

                if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);

                    if (viewHolder != null) {
                        childView = viewHolder.itemView;
                    }
                } else {
                    return false;
                }
            }
            if (targetViewId == -1) {
                return view == childView;
            } else {
                View targetView = childView.findViewById(targetViewId);
                return view == targetView;
            }
        }
    };
}
导入android.content.res.Resources;
导入android.support.v7.widget.RecyclerView;
导入android.view.view;
导入org.hamcrest.Description;
导入org.hamcrest.Matcher;
导入org.hamcrest.TypeSafeMatcher;
公共类RecycleServiceWatcher{
私人最终int回收服务ID;
公共RecycleServiceWatcher(int RecycleServiceWid){
this.recyclerivewid=recyclerivewid;
}
公共匹配器位置(最终整数位置){
返回位置视图(位置-1);
}
公共匹配器ATPOSITIONVIEW(最终int位置,最终int targetViewId){
返回新的TypeSafeMatcher(){
Resources=null;
查看儿童视图;
公共无效说明(说明){
字符串idDescription=Integer.toString(RecycleServiceWid);
if(this.resources!=null){
试一试{
idDescription=this.resources.getResourceName(RecycleServiceWid);
}catch(Resources.NotFoundException var4){
idDescription=String.format(“%s(未找到资源名称)”,RecycleServiceWid);
}
}
description.appendText(“id:+idDescription”);
}
公共布尔匹配安全(视图){
this.resources=view.getResources();
if(childView==null){
RecycleView RecycleView=(RecycleView)view.getRootView().findViewById(RecycleWebId);
if(recyclerView!=null&&recyclerView.getId()==recyclerViewId){
RecyclerView.ViewHolder ViewHolder=RecyclerView.FindViewHolder for AdapterPosition(位置);
if(viewHolder!=null){
childView=viewHolder.itemView;
}
}否则{
返回false;
}
}
如果(targetViewId==-1){
返回视图==子视图;
}否则{
View targetView=childView.findViewById(targetViewId);
返回视图==目标视图;
}
}
};
}

试试这个,我也做过同样的测试

onView(withRecyclerView(R.id.recyclerView)
      .atPositionOnView(position, R.id.childView))
      .perform(click());
方法调用

public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
    return new RecyclerViewMatcher(recyclerViewId);
}
RecycleServiceWMatcher

import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class RecyclerViewMatcher {

private final int recyclerViewId;

public RecyclerViewMatcher(int recyclerViewId) {
    this.recyclerViewId = recyclerViewId;
}

public Matcher<View> atPosition(final int position) {
    return atPositionOnView(position, -1);
}

public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
    return new TypeSafeMatcher<View>() {
        Resources resources = null;
        View childView;

        public void describeTo(Description description) {
            String idDescription = Integer.toString(recyclerViewId);
            if (this.resources != null) {
                try {
                    idDescription = this.resources.getResourceName(recyclerViewId);
                } catch (Resources.NotFoundException var4) {
                    idDescription = String.format("%s (resource name not found)", recyclerViewId);
                }
            }
            description.appendText("with id: " + idDescription);
        }

        public boolean matchesSafely(View view) {
            this.resources = view.getResources();
            if (childView == null) {
                RecyclerView recyclerView = (RecyclerView) view.getRootView().findViewById(recyclerViewId);

                if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);

                    if (viewHolder != null) {
                        childView = viewHolder.itemView;
                    }
                } else {
                    return false;
                }
            }
            if (targetViewId == -1) {
                return view == childView;
            } else {
                View targetView = childView.findViewById(targetViewId);
                return view == targetView;
            }
        }
    };
}
导入android.content.res.Resources;
导入android.support.v7.widget.RecyclerView;
导入android.view.view;
导入org.hamcrest.Description;
导入org.hamcrest.Matcher;
导入org.hamcrest.TypeSafeMatcher;
公共类RecycleServiceWatcher{
私人最终int回收服务ID;
公共RecycleServiceWatcher(int RecycleServiceWid){
this.recyclerivewid=recyclerivewid;
}
公共匹配器位置(最终整数位置){
返回位置视图(位置-1);
}
公共匹配器ATPOSITIONVIEW(最终int位置,最终int targetViewId){
返回新的TypeSafeMatcher(){
Resources=null;
查看儿童视图;
公共无效说明(说明){
字符串idDescription=Integer.toString(RecycleServiceWid);
if(this.resources!=null){
试一试{
idDescription=this.resources.getResourceName(RecycleServiceWid);
}catch(Resources.NotFoundException var4){
idDescription=String.format(“%s(未找到资源名称)”,RecycleServiceWid);
}
}
description.appendText(“id:+idDescription”);
}
公共布尔匹配安全(视图){
this.resources=view.getResources();
if(childView==null){
RecycleView RecycleView=(RecycleView)view.getRootView().findViewById(RecycleWebId);
if(recyclerView!=null&&recyclerView.getId()==recyclerViewId){
RecyclerView.ViewHolder ViewHolder=RecyclerView.FindViewHolder for AdapterPosition(位置);
if(viewHolder!=null){
childView=viewHolder.itemView;
}
}否则{
返回false;
}
}
如果(targetViewId==-1){
返回视图==子视图;
}否则{
View targetView=childView.findViewById(targetViewId);
返回视图==目标视图;
}
}
};
}
检查此链接-检查此链接-