在我的应用程序中,我使用RecyclerView来显示所有联系人列表。
我想在RecyclerView中有两个部分。
就像一个部分是我的应用程序联系人列表,另一个部分是我的手机联系人列表。
是否有任何方法来实现它?
有没有人知道如何做到这一点?
我想在RecyclerView中有两个部分。
就像一个部分是我的应用程序联系人列表,另一个部分是我的手机联系人列表。
是否有任何方法来实现它?
有没有人知道如何做到这一点?
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<SimpleSectionedRecyclerViewAdapter.Section> sections =
new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>();
//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);
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();
@Override
public int getItemViewType(int position) {
if (mCountriesModelList.get(position).isSection) {
return SECTION_VIEW;
} else {
return CONTENT_VIEW;
}
}
更新(2021年3月)
ExpandableListView
无法提供RecyclerView
提供的性能和内存优势。因此,我编写了自己的超轻量级库,其中包括自定义的RecyclerViewAdapter
和自定义的RecyclerView
(仅在需要网格布局时才需要)。适配器公开了一些类似于iOS表视图的函数,因此实现分组的可滚动列表非常容易,同时保持自己的代码设计。详细说明可以在下面链接的项目Github页面中找到。该依赖项可从Maven Central获取。
implementation 'com.github.harikrishnant1991:sectioned-recyclerview:1.0.0'
原始回答
如果不使用任何第三方库或自定义逻辑来向RecyclerView
添加标题,那么使用Android SDK有一个简单得多的解决方案。您可以简单地使用ExpandableListView
。您可能会认为它使列表可折叠,但是有一个非常简单的事情可以做来避免相同的问题。在ExpandableListView
的适配器类中,在getGroupView
方法中,只需添加以下行:
(parent as ExpandableListView).expandGroup(groupPosition)
这个方法可能长这样:
override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
var view = convertView
if (convertView == null) {
val layoutInflater = context
.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = layoutInflater.inflate(R.layout.group_view, null)
}
/*
Code to populate your data
*/
// The following code will expand the group whenever the group view is inflated
(parent as ExpandableListView).expandGroup(groupPosition)
return view
}
ExpandableListView
适配器的工作方式。每当您尝试折叠组标题时,它都会调用getGroupView
方法(以便您可以为展开/折叠状态膨胀不同的视图)。但是在该方法中扩展了该组,因此视图实际上永远不会折叠,有效地给您提供了一个分节列表外观。ExpandableListView
完全相同,因此您无需进行任何额外的自定义。请查看我的Github库,可以用于轻松创建部分: RecyclerAdapter & Easy Section
mRecylerView.setLayoutManager(...);
/*create Adapter*/
RecyclerAdapter<Customer> baseAdapter = new RecyclerAdapter<>(...);
/*create sectioned adapter. the Adapter type can be RecyclerView.Adapter*/
SectionedAdapter<String, RecyclerAdapter> adapter = new SectionedAdapter<>(SectionViewHolder.class, baseAdapter);
/*add your sections*/
sectionAdapter.addSection(0/*position*/, "Title Section 1");
/*attach Adapter to RecyclerView*/
mRecylerView.setAdapter(sectionAdapter);