您可以使用该库SectionedRecyclerViewAdapter只有一个RecyclerView构建它。
您可以找到以下here图像的例子的完整代码。
首先创建一个科类:
class MySection extends StatelessSection {
String title;
String subtitle;
List<String> list;
public MySection(String title, String subtitle, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.subtitle = subtitle;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position));
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvTitle.setText(title);
headerHolder.tvSubTitle.setText(subtitle);
}
}
然后您设置RecyclerView与节:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data for each year
MySection section1 = new MySection("Title", "Subhead", firstDataList);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(sectionAdapter.getSectionItemViewType(position)) {
case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
return 2;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);
装饰根据材料设计指南“限制的补充措施,以两个动作”。你想在父列表中添加多少项?滚动如何工作?网格和列表都使用垂直滚动? – masp
我想有几个'ViewPager'选项卡;一个带有3个父亲'CardViews',另一个带有8个父亲'CardViews',另一个带有大约5个父亲'CardViews'。目的是为了网格完全“打开”,因此,不需要内部滚动,并且只能为父CardViews垂直滚动。根据我所看到的,在另一个里面有一个'RecyclerView'也会干扰工具栏的隐藏('CoodinatorLayout'),如果你想通过按住RecyclerView的网格进行滚动,工具栏不会隐藏,隐藏在孩子外面的某个地方'RecyclerViews' – inferKNOX
我想我可能已经与您实际询问的@masp相切了。抱歉。在个人CardViews中,我只想在顶部标题和图标,中心网格以及用于隐藏一些支持文本的“扩展器”。可能还要让Cardview的头部分能够隐藏/折叠并显示/展开网格,就像“ExpandableListView”一样。 – inferKNOX