我正在使用带有2个单元格的GridLayoutManager,对于某些单元格,我希望span为1,所以我尝试使用setSpanSizeLookup,但它不起作用。我尝试为所有位置返回跨度计数1,但仍然出现两个单元格而不是一个。
以下是我的代码
Name
为什么它不能正常工作?
答案 0 :(得分:12)
替换
return 1;
到
return 2;
这表明你正在将2个细胞分成1个细胞。
<强>代码强>
这是我为特定位置扫描2个单元格的代码
GridLayoutManager glm=new GridLayoutManager(mContext,2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(categoryAdapter.getItemViewType(position)) {
case 1:
return 2;
default:
return 1;
}
}
});
mRecyclerViewCategory.setLayoutManager(glm);
如何在 Recycler Adapter
中定义案例范围@Override
public int getItemViewType(int position) {
if(position==[your_specific_postion_where_to_span]){
return 1;
}
return super.getItemViewType(position);
}
答案 1 :(得分:0)
我为此努力,因为此时的文档很差。我这样想出来......
getSpanSize和getSpanIndex似乎一起工作。对我来说,我试图在gridlayoutManager中插入一个pageViewer来生成两列。所以定义如下:mGridLayout = new GridLayoutManager(getActivity(), 2);
//must be called before setLayoutManager is invoked
private void setNumOfColumnsForPageViewer(final FallCollectionRecyclerAdapter adapter) {
mGridLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (adapter.getItemViewType(position) == MyRecyclerAdapter.TYPE_PAGE_VIEWER)
return 2; //some other form of item like a header, or whatever you need two spans for
else
return 1; //normal item which will take up the normal span you defined in the gridlayoutmanager constructor
}
@Override
public int getSpanIndex(int position, int spanCount) {
if (adapter.getItemViewType(position) == FallCollectionRecyclerAdapter.TYPE_PAGE_VIEWER)
return 1;//use a single span
else
return 2; //use two spans
}
});
mRecyclerView.setLayoutManager(mGridLayout);
}