studio - Combinar múltiples RecyclerView.Adapter para usar con RecyclerView(Android)
son los elementos qué construyen un recyclerview (4)
Me gustaría ir a esta biblioteca: https://github.com/martijnvdwoude/recycler-view-merge-adapter
Consulte la documentación en la página de Github para ver ejemplos sobre cómo usar esto en su código.
Extracto del archivo léame:
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);
Tengo una cadena json que rompo en 4 conjuntos de datos, cada conjunto de datos tiene su propio RecyclerView.Adapter.
Quiero imprimir todos los adaptadores en el mismo RecyclerView idealmente con StickyHeaders para cada adaptador.
Creo que esto se puede hacer usando ListViews a través de CWAC-merge de CommonsWare ( https://github.com/commonsguy/cwac-merge )
¿Alguna idea de cómo esto puede o debe hacerse con RecyclerView? Soy un desarrollador maduro, así que cuanto menos abstracta sea la respuesta, mejor ;-D
Gracias.
Lo logra con solo un adaptador: SectionedRecyclerViewAdapter . Con esta biblioteca puede agrupar sus elementos en Secciones y opcionalmente puede agregarles encabezados y / o pies de página.
Primero crea una clase de sección para agrupar tus artículos:
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);
}
}
Luego configura el RecyclerView con sus Secciones:
// 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);
Puedes mirar GroupAdapter
GroupAdapter.Builder builder = new GroupAdapter.Builder();
builder.add(firstAdapter);
builder.add(secondAdapter);
GroupAdapter groupAdapter = builder.build();
recyclerView.setAdapter(groupAdapter);
Hice una clase que hace exactamente lo que quieres 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