您可以使用該庫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