在GridLayout RecyclerView中添加分隔符

时间:2017-02-23 21:01:52

标签: android android-recyclerview indexoutofboundsexception gridlayoutmanager

我尝试将水平分隔线添加到GridLayout RecyclerView。但出于某种原因,分隔线并没有出现。

作为替代方案,我使用带有2个参数的addItemDecoration方法,其中第二个参数是添加ItemDecoration之后的索引。

RecyclerView r = (RecyclerView) findViewById(R.id.recycler_view);
r.setAdapter(new AppAdapter(this, fetchData());
r.setLayoutManager(new GridLayoutManager(this, 5));
r.addItemDecoration(new DividerItemDecoration(this, GridLayoutManager.HORIZONTAL), 2);

并强制关闭,但出现以下异常:

Caused by: java.lang.IndexOutOfBoundsException: Index: 2, Size: 0

这个尺码表示什么?为什么是零?

可能是因为我在Activity而不是Fragment中使用这个RecyclerView吗?

P.S。:适配器没有问题;它不是空的。

0 个答案:

没有答案