在 Android RecyclerView 中添加两个部分

40
在我的应用程序中,我使用RecyclerView来显示所有联系人列表。
我想在RecyclerView中有两个部分。
就像一个部分是我的应用程序联系人列表,另一个部分是我的手机联系人列表。
是否有任何方法来实现它?
有没有人知道如何做到这一点?

2
你能否展示一下你的努力或者更详细地说明一下? - Abhishek
4
请参考这个链接:https://dev59.com/F18d5IYBdhLWcg3w8WJt该链接介绍了如何创建具有多种视图类型的RecyclerView。 - N J
1
请查看此答案:https://dev59.com/F18d5IYBdhLWcg3w8WJt#34464367 - Islam Assi
结账粘性部分:https://stackoverflow.com/a/62865328/14784590 - Reejesh
7个回答

18
如果你已经有了一个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);

在同一个示例中,您能否告诉我如何在片段中应用此代码,并如何在sCheeseStrings中获取数据。 - Abhi Soni
你如何为标题添加自定义布局? - CodeMonkey
动态数据怎么办? - Pratik Butani

13
如果你正在寻找一种无需使用硬编码的标题/行索引的解决方案,你可以使用库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();

有没有一种方法可以为每个部分的项目列表设置最小高度,以便如果列表非常短,在下一个部分开始之前仍然具有一定的高度? - CodeMonkey
另外,是否有一种方法可以在没有内容项可显示时显示某些内容? - CodeMonkey

6

5
让我试着提出一种本地解决方案。
您必须拥有一个带有isFavourite标志的联系人列表。
private class Contacts{
  private String name;
  private String phoneNumber;
  private boolean isFavourite;
}

按照isFavourite和contactName对该数组进行排序,像这样

将该列表传递给您的ContactRecyclerAdapter,并针对Header和Items使用两种不同的布局像这样


1

更新(2021年3月)

ExpandableListView无法提供RecyclerView提供的性能和内存优势。因此,我编写了自己的超轻量级库,其中包括自定义的RecyclerViewAdapter和自定义的RecyclerView(仅在需要网格布局时才需要)。适配器公开了一些类似于iOS表视图的函数,因此实现分组的可滚动列表非常容易,同时保持自己的代码设计。详细说明可以在下面链接的项目Github页面中找到。该依赖项可从Maven Central获取。

分组的RecyclerView

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完全相同,因此您无需进行任何额外的自定义。

0

请查看我的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);

0
最简单的方法是在你的RecyclerView的item布局(例如TextView)的顶部添加一个View,表示分区分隔符,并将其可见性设置为GONE。然后对列表进行排序,在你的适配器中进行以下操作:
- 如果位置等于0,设置VISIBLE并设置分区名称。 - 如果当前项目的分区与前一个项目的分区相同,则设置GONE。 - 否则,设置VISIBLE并显示分区名称。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接