我使用带有两个单元的GridLayoutManager,对于某些单元来说,我希望span是一个单元,所以我尝试使用setSpanSizeLookup,但它不起作用。我尝试为所有位置返回span计数1,但仍然出现了两个单元格而不是一个单元格。
以下是我的代码
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return 1;
}
});
recyclerView.setLayoutManager(gridLayoutManager);它不起作用的原因有什么?
发布于 2017-01-30 04:25:53
替换
return 1;至
return 2;这指定您要将两个单元格划分为一个单元格。
码
这是我的代码,用于为特定位置划分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);如何在回收适配器中定义用例范围
@Override
public int getItemViewType(int position) {
if(position==[your_specific_postion_where_to_span]){
return 1;
}
return super.getItemViewType(position);
}发布于 2017-09-22 12:09:04
我为此而挣扎,因为此时这里的文档很差。我就这样想出来了..。
getSpanSize和getSpanIndex似乎一起工作。对我来说,我试图在跨两列的gridlayoutManager中插入一个gridlayoutManager。因此,定义如下: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);
}https://stackoverflow.com/questions/41928940
复制相似问题