在 recyclerview 中添加两个部分 android
Add two sections in recyclerview android
在我的应用程序中,我使用 recyclerview 来显示所有联系人列表。
我想要 recyclerview 中的两个部分。
第一部分是我的申请联系人列表,第二部分是我的 phone 联系人列表。
像这样
有什么方法可以做到吗?
有人知道怎么做吗?
如果您已经有了 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);
如果您正在寻找不需要使用硬编码 header/row 索引的解决方案,您可以使用库 SectionedRecyclerViewAdapter.
首先创建一个部分 class 来对您的项目进行分组:
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);
}
}
然后你用你的 Sections 设置 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();
Let me try to propose a native solution.
您必须有一个 联系人列表 并带有 isFavourite 标记
private class Contacts{
private String name;
private String phoneNumber;
private boolean isFavourite;
}
根据 isFavourite 和 contactName 对该数组进行排序 like this
将该列表传递给您的 ContactRecyclerAdapter。并为页眉和项目使用两种不同的布局 like this
在你的适配器中 getItemViewType 像这样布局....
@Override
public int getItemViewType(int position) {
if (mCountriesModelList.get(position).isSection) {
return SECTION_VIEW;
} else {
return CONTENT_VIEW;
}
}
在 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);
更新(2021 年 3 月)
ExpandableListView
不会提供 RecyclerView 提供的性能和内存优势。因此,我编写了自己的超轻量级库,其中分别在内置 RecyclerViewAdapter
和 RecyclerView
之上分别包含自定义回收器适配器和自定义回收器视图(仅在需要网格布局时才需要)。该适配器公开了一些类似于 iOS table 视图工作方式的功能,因此在坚持您自己的代码设计的同时,可以非常轻松地实现分段回收器视图。详细说明可以在下面链接的项目 github 页面中找到。该依赖项可从 Maven Central 获得。
implementation 'com.github.harikrishnant1991:sectioned-recyclerview:1.0.0'
原答案
除了使用任何第三方库或使用自定义逻辑将 header 添加到 RecyclerView
之外,还有一个使用 Android SDK 的更简单的解决方案。您可以简单地使用 ExpandableListView
。您可能认为它使列表可折叠,但有一个非常简单的事情可以避免同样的情况发生。在 ExpandableListView
的适配器 class 中,在 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
适配器的工作方式。每当您尝试折叠组 header 时,它都会调用 getGroupView
方法(以便您可以为 expanded/collapsed 状态膨胀不同的视图)。但是您在该方法中扩展了组,因此视图实际上永远不会折叠,有效地为您提供了一个分段列表外观。
除了上面提到的一行之外,所有其他部分都与您对普通行所做的完全相同ExpandableListView
,因此您不需要进行额外的自定义。
在我的应用程序中,我使用 recyclerview 来显示所有联系人列表。 我想要 recyclerview 中的两个部分。
第一部分是我的申请联系人列表,第二部分是我的 phone 联系人列表。
像这样
有什么方法可以做到吗?
有人知道怎么做吗?
如果您已经有了 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);
如果您正在寻找不需要使用硬编码 header/row 索引的解决方案,您可以使用库 SectionedRecyclerViewAdapter.
首先创建一个部分 class 来对您的项目进行分组:
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);
}
}
然后你用你的 Sections 设置 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();
Let me try to propose a native solution.
您必须有一个 联系人列表 并带有 isFavourite 标记
private class Contacts{
private String name;
private String phoneNumber;
private boolean isFavourite;
}
根据 isFavourite 和 contactName 对该数组进行排序 like this
将该列表传递给您的 ContactRecyclerAdapter。并为页眉和项目使用两种不同的布局 like this
在你的适配器中 getItemViewType 像这样布局....
@Override
public int getItemViewType(int position) {
if (mCountriesModelList.get(position).isSection) {
return SECTION_VIEW;
} else {
return CONTENT_VIEW;
}
}
在 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);
更新(2021 年 3 月)
ExpandableListView
不会提供 RecyclerView 提供的性能和内存优势。因此,我编写了自己的超轻量级库,其中分别在内置 RecyclerViewAdapter
和 RecyclerView
之上分别包含自定义回收器适配器和自定义回收器视图(仅在需要网格布局时才需要)。该适配器公开了一些类似于 iOS table 视图工作方式的功能,因此在坚持您自己的代码设计的同时,可以非常轻松地实现分段回收器视图。详细说明可以在下面链接的项目 github 页面中找到。该依赖项可从 Maven Central 获得。
implementation 'com.github.harikrishnant1991:sectioned-recyclerview:1.0.0'
原答案
除了使用任何第三方库或使用自定义逻辑将 header 添加到 RecyclerView
之外,还有一个使用 Android SDK 的更简单的解决方案。您可以简单地使用 ExpandableListView
。您可能认为它使列表可折叠,但有一个非常简单的事情可以避免同样的情况发生。在 ExpandableListView
的适配器 class 中,在 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
适配器的工作方式。每当您尝试折叠组 header 时,它都会调用 getGroupView
方法(以便您可以为 expanded/collapsed 状态膨胀不同的视图)。但是您在该方法中扩展了组,因此视图实际上永远不会折叠,有效地为您提供了一个分段列表外观。
除了上面提到的一行之外,所有其他部分都与您对普通行所做的完全相同ExpandableListView
,因此您不需要进行额外的自定义。