在recyclerview android中添加两个部分

时间:2015-06-10 09:54:54

标签: android android-recyclerview

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

例如,一个部分是我的申请联系人列表,第二部分是我的电话联系人列表。

喜欢这个

enter image description here

有没有办法做到这一点?

有人知道怎么做吗?

6 个答案:

答案 0 :(得分:14)

如果你已经拥有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);

答案 1 :(得分: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);
    }

}

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

答案 2 :(得分:4)

  

让我尝试提出原生解决方案。

您必须拥有联系人列表,其标志为isFavourite,如

private class Contacts{
  private String name;
  private String phoneNumber;
  private boolean isFavourite;
}

根据isFavourite和contactName like this

对该数组进行排序

将该列表传递给ContactRecyclerAdapter。并为标题和项like this

使用两种不同的布局

答案 3 :(得分:3)

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

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

https://github.com/sayanmanna/LetterSectionedRecyclerView

答案 4 :(得分: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);

希望它有所帮助。

答案 5 :(得分: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完全一样,因此您无需进行其他自定义操作。