2

我使用GridLayoutManager与2个单元格和一些单元格我想跨度为一个,所以我尝试使用setSpanSizeLookup,但它不工作。我试过为所有位置返回1,但仍然出现两个单元而不是一个。GridLayoutManager setSpanSizeLookup不工作

以下是我的代码

gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
     @Override 
     public int getSpanSize(int position) { 
      return 1; 
     } 
    }); 
    recyclerView.setLayoutManager(gridLayoutManager); 

为什么它不工作的任何原因?

回答

3

更换

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); 

如何在回收站适配器定义的情况下跨度

@Override 
public int getItemViewType(int position) { 
    if(position==[your_specific_postion_where_to_span]){ 
     return 1; 
    } 
    return super.getItemViewType(position); 
} 
+0

我想是在即使使用2个跨度和和奇数位置1个跨度位置,但两个跨度只得到一个被示出和离开网格空间用于第二列只有空!如果条件应该是'if(position%2!= 0){return 1};'其余的代码与上面的 –

+0

@ NaszNjokaSr.just修改'getItemViewType'跨越所有奇数位置的1列 –

+0

相同,我们希望所有职位的行为都不是特定的 –

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); 
}