在recyclerview android中添加两个部分

Kin*_*jal 34 android android-recyclerview

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

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

像这样

在此输入图像描述

有没有办法做到这一点?

有谁知道怎么做?

mar*_*nes 16

如果你已经拥有了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);
Run Code Online (Sandbox Code Playgroud)


Gus*_*avo 12

如果您正在寻找不需要使用硬编码标头/行索引的解决方案,则可以使用库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);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后使用您的章节设置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);
Run Code Online (Sandbox Code Playgroud)

您还可以向部分添加新行,而无需重新计算索引:

favoritesSection.addRow("new item");
sectionAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)


Xar*_*mer 5

让我尝试提出一个本地解决方案。

您必须有一个带有 isFavourite 标志的联系人列表,例如

private class Contacts{
  private String name;
  private String phoneNumber;
  private boolean isFavourite;
}
Run Code Online (Sandbox Code Playgroud)

排序isFavourite和联系人姓名的基础上,该数组像这样

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


Say*_*nna 5

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

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

https://github.com/sayamanna/LetterSectionedRecyclerView