我实现了一个可扩展的recyclerview,其子元素是列表的一部分。我跟着这个code。这是它的工作原理,
使用RecyclerView实现ExpandableListView简要描述如下。列表模型有一个附加参数" type"标识该项是标题还是子项。通过检查此参数,适配器会扩展与该类型对应的视图和视图。如果类型是HEADER,它将膨胀标题项的布局,其中包含TextView和ImageView,用于指示子树是否已展开。
现在,我想要做的是将展开的布局设为网格。我通常会通过将布局管理器设置为GridLayoutManager来实现这一点,但在这种情况下,我只使用一个recyclerview,这意味着我无法更改布局管理器而不更改标头,最终导致整个recyclerview转向进入包含标题的网格。
我的问题是:如何更改适配器内部几个布局的布局管理器?
编辑:我添加了一些代码。
Recyclerview适配器:
public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// These are constants that are used to determine if the item is a child or a header and is defined with each item from the data model
public static final int HEADER = 0;
public static final int CHILD = 1;
private List<Item> data;
public ExpandableListAdapter(List<Item> data) {
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
View view = null;
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Check whether the item is a header or child and inflate differnet layouts
switch (type) {
case HEADER:
// Inflate a header layout if the item is a header
view = inflater.inflate(R.layout.list_header, parent, false);
ListHeaderViewHolder header = new ListHeaderViewHolder(view);
return header;
case CHILD:
// Inflate a child layout if the item is a child
view = inflater.inflate(R.layout.list_child, parent, false);
ListChildViewHolder child = new ListChildViewHolder(view);
return child;
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Item item = data.get(position);
// Bind different layouts based on if the item is a header or child
switch (item.getType()) {
case HEADER:
// ...
case CHILD:
// ...
}
}
@Override
public int getItemViewType(int position) {
return data.get(position).type;
}
@Override
public int getItemCount() {
return data.size();
}
// Viewholder for the header items
private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
// ...
}
// Viewholder for the child items
private static class ListChildViewHolder extends RecyclerView.ViewHolder {
// ...
}
这是我宣布布局管理器的主要活动:
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
答案 0 :(得分:13)
您可以将布局管理器更改为GridLayoutManager并定义&#34; span size&#34;例如,对于标题,如果您希望网格包含2列,则标题的跨度应为2,子标题的跨度为1:
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(getTypeForPosition(position)) {
case HEADER:
return 2;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(glm);
答案 1 :(得分:0)
将布局管理器更改为gridlayout manager并处理跨度大小,如下所述
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int type=mAdapter.getItemViewType(position);
if (type == "view holder type name")
return 2;
else
return 1;
}
});