在recyclerview android中添加两个部分

在我的应用程序中,我使用recyclerview显示所有联系人列表。 我想在recyclerview中有两个部分。

像一个部分是我的申请联系人列表,第二部分是我的电话联系人列表。

喜欢这个

在此处输入图像描述

有没有办法做到这一点?

有谁知道怎么做?

Solutions Collecting From Web of "在recyclerview android中添加两个部分"

如果你已经有了RecyclerView ,那么实现这些部分的简单方法就是使用Gabriele Mariotti的SimpleSectionedRecyclerViewAdapter 。

我贴你的例子:

 //Your RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL)); //Your RecyclerView.Adapter mAdapter = new SimpleAdapter(this,sCheeseStrings); //This is the code to provide a sectioned list List sections = new ArrayList(); //Sections sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0,"Section 1")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(5,"Section 2")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(12,"Section 3")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(14,"Section 4")); sections.add(new SimpleSectionedRecyclerViewAdapter.Section(20,"Section 5")); //Add your adapter to the sectionAdapter SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]; SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new SimpleSectionedRecyclerViewAdapter(this,R.layout.section,R.id.section_text,mAdapter); mSectionedAdapter.setSections(sections.toArray(dummy)); //Apply this adapter to the RecyclerView mRecyclerView.setAdapter(mSectionedAdapter); 

如果您正在寻找不需要使用硬编码标头/行索引的解决方案,则可以使用库SectionedRecyclerViewAdapter 。

首先创建一个Section类来对项目进行分组:

 class MySection extends StatelessSection { String title; List list; public MySection(String title, List 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(); 

让我试着提出一个原生解决方案。

您必须有一个标记为isFavourite 的联系人列表

 private class Contacts{ private String name; private String phoneNumber; private boolean isFavourite; } 

基于isFavourite和contactName 对此数组进行排序

将该列表传递给您的ContactRecyclerAdapter。 并为这样的标题和项目使用两种不同的布局

在您的适配器getItemViewType布局像这样….

 @Override public int getItemViewType(int position) { if (mCountriesModelList.get(position).isSection) { return SECTION_VIEW; } else { return CONTENT_VIEW; } } 

https://github.com/sayanmanna/LetterSectionedRecyclerView

看看我在Github上的图书馆,可以用来轻松创建部分: RecyclerAdapter&Easy Section

 mRecylerView.setLayoutManager(...); /*create Adapter*/ RecyclerAdapter baseAdapter = new RecyclerAdapter<>(...); /*create sectioned adapter. the Adapter type can be RecyclerView.Adapter*/ SectionedAdapter adapter = new SectionedAdapter<>(SectionViewHolder.class, baseAdapter); /*add your sections*/ sectionAdapter.addSection(0/*position*/, "Title Section 1"); /*attach Adapter to RecyclerView*/ mRecylerView.setAdapter(sectionAdapter); 

希望能帮助到你。