आप देख रहे हैं एक समाधान है कि हार्डकोडेड हैडर/पंक्ति अनुक्रमित उपयोग करने के लिए की जरूरत नहीं है के लिए, आप पुस्तकालय 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_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 favoritesSection = new MySection("Favorites", favoritesList);
MySection contactsSection = new MySection("Add Favorites", contactsList);
// Add your Sections to the adapter
sectionAdapter.addSection(favoritesSection);
sectionAdapter.addSection(contactsSection);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
आप भी अपने वर्गों के लिए नई पंक्तियाँ जोड़ सकते हैं पुनर्गणना अनुक्रमित:
favoritesSection.addRow("new item");
sectionAdapter.notifyDataSetChanged();
आप अपने प्रयासों को दिखाने या अधिक विस्तृत – Abhishek
इस http देख कृपया: // stackoverflow।कॉम/प्रश्न/26245139/कैसे-से-निर्माण-रीसाइक्लिंग-के-बहु-दृश्य-प्रकार –
इस उत्तर पर एक नज़र डालें: http://stackoverflow.com/a/34464367/2739684 –