Android Robolectric单击Recycler查看条目 [英] Android Robolectric Click RecyclerView Item

查看:75
本文介绍了Android Robolectric单击Recycler查看条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以用Robolectric模拟对RecyclerView项目的点击?

Is there any way to simulate a click on a RecyclerView item with Robolectric?

到目前为止,我已经尝试将View放置在RecyclerView的第一个可见位置,但这始终是null. getChildCount()始终返回0,而findViewHolderForPosition始终为null.适配器从getItemCount()返回一个非0的数字(适配器中肯定有项目).

So far, I have tried getting the View at the first visible position of the RecyclerView, but that is always null. It's getChildCount() keeps returning 0, and findViewHolderForPosition is always null. The adapter returns a non-0 number from getItemCount() (there are definitely items in the adapter).

我正在使用Robolectric 2.4快照.

I'm using Robolectric 2.4 SNAPSHOT.

推荐答案

似乎是问题所在,需要在Robolectric中手动测量和布置RecyclerView.调用此即可解决问题:

Seems like the issue was that RecyclerView needs to be measured and layed out manually in Robolectric. Calling this solves the problem:

recyclerView.measure(0, 0);
recyclerView.layout(0, 0, 100, 10000);

这篇关于Android Robolectric单击Recycler查看条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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