Rebolectric:DividerItemDecoration阻止将测量设置为recyclerview

时间:2017-01-10 09:53:48

标签: android android-recyclerview robolectric

我正在使用RecyclerViewRobolectric个项目编写测试用例。 在调用recyclerView.measure(0,0)时面对NullPointerException,因为RecyclerView已应用RecyclerView.ItemDecoration,但RecyclerViewRecyclerView.ItemDecoration无关。

异常日志:

java.lang.NullPointerException
    at ui.adapters.DividerItemDecoration.getItemOffsets(DividerItemDecoration.java:119)
    at android.support.v7.widget.RecyclerView$ItemDecoration.getItemOffsets(RecyclerView.java:8662)

DividerItemDecoration.java的第119行放置了评论,此处mDivider变为NULL,它是从构造函数初始化的。

@Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());//Line:119
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }

构造

public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

ATTRS

private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

请提前告知我如何编写RecyclerView RecyclerView.ItemDecoration的测试用例,并提前感谢。

解: 最后,我通过为Shadow创建RecyclerView类来解决此问题,如下面的代码所述,并遵循基本的阴影映射逻辑,

RecyclerViewShadow.java

@Implements(RecyclerView.class)
public class RecyclerViewShadow extends ShadowViewGroup {
    @Implementation
    public void addItemDecoration(RecyclerView.ItemDecoration decor, int index) {

    }
}

0 个答案:

没有答案