使用 SpanSizeLookup 为 GridLayoutManager 中的项目设置跨度 [英] Set span for items in GridLayoutManager using SpanSizeLookup
本文介绍了使用 SpanSizeLookup 为 GridLayoutManager 中的项目设置跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用节标题实现类似网格的布局.想想 https://github.com/TonicArtos/StickyGridHeaders
I want to implement grid-like layout with section headers. Think of https://github.com/TonicArtos/StickyGridHeaders
我现在在做什么:
mRecyclerView = (RecyclerView) view.findViewById(R.id.grid);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case MyAdapter.TYPE_HEADER:
return 1;
case MyAdapter.TYPE_ITEM:
return 2;
default:
return -1;
}
}
});
mRecyclerView.setLayoutManager(mLayoutManager);
现在常规项目和标题的跨度大小均为 1.我该如何解决这个问题?
Now both regular items and headers have span size of 1. How do I solve this?
推荐答案
问题是 header 的 span size 应该是 2,常规 item 的 span size 应该是 1.所以正确的实现是:
The problem was that header should have span size of 2, and regular item should have span size of 1. So correct implementations is:
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case MyAdapter.TYPE_HEADER:
return 2;
case MyAdapter.TYPE_ITEM:
return 1;
default:
return -1;
}
}
});
这篇关于使用 SpanSizeLookup 为 GridLayoutManager 中的项目设置跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文