GridLayoutManager跨列处理的代码:
/**
*当RecyelrView开始观察Adapter会被回调
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
mAdapter.onAttachedToRecyclerView(recyclerView);
final RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof GridLayoutManager) {
final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int viewType = getItemViewType(position);
//如果是HeaderView或者是FooterView,设置占据gridLayoutManager.getSpanCount()列
if (null != mHeaderViews.get(viewType) || null != mFooterViews.get(viewType)) {
return gridLayoutManager.getSpanCount();
}
return 1;
}
});
}
}
StaggeredGridLayoutManager跨列处理的代码:
/**
* 一个item通过adapter开始显示会被回调
*/
@Override
public void onViewAttachedToWindow(BaseViewHolder holder) {
super.onViewAttachedToWindow(holder);
int position = holder.getLayoutPosition();
if (isHeaderViewPosition(position)||isFooterViewPosition(position)){
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
if (null != lp && lp instanceof StaggeredGridLayoutManager.LayoutParams){
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
p.setFullSpan(true);//占满一行
}
}
}