【发布时间】:2015-06-10 09:54:53
【问题描述】:
在我的应用程序中,我使用 recyclerview 来显示所有联系人列表。 我想要 recyclerview 中的两个部分。
就像一个部分是我的应用程序联系人列表,第二个部分是我的电话联系人列表。
像这样
有什么方法可以做到吗?
有人知道怎么做吗?
【问题讨论】:
-
能否请您展示您的努力或详细说明
标签: android android-recyclerview
在我的应用程序中,我使用 recyclerview 来显示所有联系人列表。 我想要 recyclerview 中的两个部分。
就像一个部分是我的应用程序联系人列表,第二个部分是我的电话联系人列表。
像这样
有什么方法可以做到吗?
有人知道怎么做吗?
【问题讨论】:
标签: android android-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);
【讨论】:
如果您正在寻找不需要使用硬编码标题/行索引的解决方案,您可以使用库 SectionedRecyclerViewAdapter。
首先创建一个 Section 类来对您的项目进行分组:
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();
【讨论】:
在你的适配器中 getItemViewType 布局像这样 ....
@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);
【讨论】: