答案 0 :(得分:2)
步骤1:定义两个布局项目,一个用于列,另一个用于行
步骤2:创建回收者视图适配器,并覆盖以下方法
@Override
public int getItemViewType(int position) {
int viewType;
if ((position + 1) % 10 == 0) {
viewType = VIEW_TYPE_ROW;
} else {
viewType = VIEW_TYPE_COLUMN;
}
return viewType;
}
@Override
public long getItemId(int position) {
/**
* Optimization of notifyDataSetChanged
* Look at documentation for setHasStableIds(true) and getItemId(int position)
*/
return STABLE_ID + position;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case VIEW_TYPE_COLUMN:
// inflate coloumn layout and view holder
break;
case VIEW_TYPE_ROW:
// inflate row layout and view holder
break;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case VIEW_TYPE_COLUMN:
break;
case VIEW_TYPE_ROW:
break;
}
}
初始化适配器如下所示
final GridLayoutManager layoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 2);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch (themeAdapter.getItemViewType(position)) {
case ThemeAdapter.VIEW_TYPE_COLUMN:
return 1;
case ThemeAdapter.VIEW_TYPE_ROW:
return layoutManager.getSpanCount();
default:
return layoutManager.getSpanCount();
}
}
});
这里themeAdapter是您的RecyclerView适配器。
答案 1 :(得分:0)
如果将RecyclerView
与GridLayoutManager
一起使用,则可以使用setSpanSizeLookup
您需要adapter
和multiviewtype
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(adapter.getItemViewType(position)){
case adapter.TYPE_ROW:
return 2;
case adapter.TYPE_COLUMN:
return 1;
}
}
});