如何将MultiType布局设置为RecyclerView?

时间:2018-08-11 12:22:06

标签: java android android-recyclerview

在我的应用程序中,我想使用RecyclerView显示一些列表。
但是我想设置 Multitype layouts,而不仅仅是设置一种布局!
为此,我编写了以下代码,可以设置 multitype layouts并显示2种布局,但是运行我的代码时,请显示layouts,如下所示:

我的代码生成图片(带有错误):
enter image description here

我的代码:

mLayoutManager = new GridLayoutManager(mContext, SPAN_SIZE) {
    @Override
    public boolean canScrollVertically() {
        return true;
    }
};
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        return position == 7 ? 2 : 1;
    }
});
postsRecyclerView.setLayoutManager(mLayoutManager);

此代码用于设置多类型视图。

但是我想显示recyclerView个项目,例如下图:
enter image description here

如何解决此错误?请帮助我

1 个答案:

答案 0 :(得分:2)

您应该检查第9个元素,这意味着第8个索引。因此,请将代码更改为

    @Override
    public int getSpanSize(int position) {
        return position == 8 ? 2 : 1;
    }