我想在recyclerview中设置组头,这意味着我有一个包含json对象的api,它包含基于课程的json对象例如Lesson1包含超过3个项目第二课包含4个项目,我已经从api中得到了响应,并像这样显示在recyclerview中。
Lesson 1:
Lesson 1:Random Numbers
Lesson 1:Complex Numbers
Lesson 1:Matrix
-
Lesson 2:
Lesson 2:Algebra
Lesson 2:Differentiation
Lesson 2:Integration
就像这样,我在recyclerview .I中显示了我想要的组标题显示像这样
Lesson 1:
Random Numbers
Complex Numbers
Matrix
-
Lesson 2:
Algebra
Differentiation
Integration
想要设置标题和分组的项目,请帮助我朋友
发布于 2016-03-31 16:04:10
使用库SectionedRecyclerViewAdapter,您可以按部分对项目进行分组,并为每个部分添加一个标题:
class MySection extends StatelessSection {
String title;
List<String> list;
public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_footer, R.layout.section_item);
this.title = title;
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.tvItem.setText(title);
}
}
然后使用您的部分设置RecyclerView:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
MySection lesson1Section = new MySection("Lesson 1", lesson1List);
MySection lesson2Section = new MySection("Lesson 2", lesson2List);
// Add your Sections
sectionAdapter.addSection(lesson1Section);
sectionAdapter.addSection(lesson2Section);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
https://stackoverflow.com/questions/36128332
复制