我必须创建一个异构的RecyclerView
。默认列数为2,但有些项需要全宽。我在这样使用StaggeredGridLayoutManager
时得到它:
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
FeedItems currentItem = getItem(position);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
switch (viewType) {
case ITEM_TYPE_HEADER:
final HeaderItem headerItem = currentItem.getHeaderItem();
layoutParams.setFullSpan(true);
}
}
如何对gridlayoutmanager执行相同的操作,在这里,我可以轻松地根据不同的视图类型切换跨距?
最佳答案
有一种方法void setSpanSizeLookup (GridLayoutManager.SpanSizeLookup spanSizeLookup)
。像这样使用:
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case 1:
return 1;
case 2:
return 2;
default:
return -1;
}
}
});
mRecyclerView.setLayoutManager(mLayoutManager);