Ответ 1
Чтобы сделать то, что вы хотите, вам, вероятно, придется написать свой собственный LayoutManager
.
Я думаю, что это проще:
// Create a grid layout with 6 columns
// (least common multiple of 2 and 3)
GridLayoutManager layoutManager = new GridLayoutManager(this, 6);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// 5 is the sum of items in one repeated section
switch (position % 5) {
// first two items span 3 columns each
case 0:
case 1:
return 3;
// next 3 items span 2 columns each
case 2:
case 3:
case 4:
return 2;
}
throw new IllegalStateException("internal error");
}
});
Если элемент сетки должен знать свой размер диапазона, вы можете найти его так: ViewHolder
:
// this line can return null when the view hasn't been added to the RecyclerView yet
RecyclerView recyclerView = (RecyclerView) itemView.getParent();
GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
int spanSize = gridLayoutManager.getSpanSizeLookup().getSpanSize(getLayoutPosition());