我有一个json字符串,我分成4个数据集,每个数据集都有自己的RecyclerView.Adapter。
我想在每个适配器上使用StickyHeader在同一个RecyclerView中输出所有适配器。
我相信这可以通过CommonsWare的CWAC合并(https://github.com/commonsguy/cwac-merge)
使用ListViews来完成有关如何使用RecyclerView或应该如何使用RecyclerView的想法?我是一个不成熟的开发人员,所以响应越不抽象越好;-D
感谢。
答案 0 :(得分:3)
在Recyclerview小部件的新更新中,您可以使用 MergeAdapter 类
import androidx.recyclerview.widget.MergeAdapter
实例化适配器:
private val mfirstAdapter = FirstAdapter()
private val mSecondAdapter = SecondAdapter()
// here is MergeAdapter class
private val adapter = MergeAdapter(mfirstAdapter , mSecondAdapter )
然后是用法:
binding.recycler.adapter = adapter
如何更新适配器:(如果使用diffUtils,则使用.submitList,否则使用notifyDataSetChaned())
mfirstAdapter.submitList(myDataArrayList.subList(0, 10))
mSecondAdapter.submitList(myDataArrayList.subList(10, list.size - 1))
答案 1 :(得分:1)
只使用一个适配器即可实现此目的:SectionedRecyclerViewAdapter。使用此库,您可以将项目分组到章节中,您可以选择向其添加页眉和/或页脚。
首先创建一个Section类来对项目进行分组:
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_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);
}
public void addRow(String item) {
this.list.add(item);
}
}
然后使用您的章节设置RecyclerView:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data
MySection section1 = new MySection("Header of section 1", section1List);
MySection section2 = new MySection("Header of section 2", section2List);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
答案 2 :(得分:0)
我会选择这个库:https://github.com/martijnvdwoude/recycler-view-merge-adapter
查看Github页面上的文档,了解如何在代码中使用它的示例。
摘自自述文件:
RecyclerView myRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
// Create new merge adapter
RecyclerViewMergeAdapter mergeAdapter = new RecyclerViewMergeAdapter();
// Add any number of subadapters to merge adapter
MyRecyclerViewSubAdapter subAdapter1 = new MyRecyclerViewSubAdapter();
MyRecyclerViewSubAdapter subAdapter2 = new MyRecyclerViewSubAdapter();
mergeAdapter.addAdapter(subAdapter1);
mergeAdapter.addAdapter(subAdapter2);
// Set merge adapter to RecyclerView
myRecyclerView.setAdapter(mergeAdapter);
答案 3 :(得分:0)
您可以查看GroupAdapter
GroupAdapter.Builder builder = new GroupAdapter.Builder();
builder.add(firstAdapter);
builder.add(secondAdapter);
GroupAdapter groupAdapter = builder.build();
recyclerView.setAdapter(groupAdapter);
答案 4 :(得分:0)
我做了一个完全按照你想要的课程 https://gist.github.com/agustindev/e2faa7ef6a7b61cc0eafeeaf81e4e434
...
val multipleAdapter = MultipleAdapter()
val adapter1 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
val adapter2 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
val adapter3 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
multipleAdapter.addAdapter(adapter1)
multipleAdapter.addAdapter(adapter2)
multipleAdapter.addAdapter(adapter3)
recyclerView.adapter = multipleAdapter