【问题标题】:Add two sections in recyclerview android在recyclerview android中添加两个部分
【发布时间】:2015-06-10 09:54:53
【问题描述】:

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

就像一个部分是我的应用程序联系人列表,第二个部分是我的电话联系人列表。

像这样

有什么方法可以做到吗?

有人知道怎么做吗?

【问题讨论】:

标签: android android-recyclerview


【解决方案1】:

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

【讨论】:

  • 在同一个例子中,你能告诉我们如何在片段中应用这个cos,以及如何在sCheeseStrings中获取数据。
  • 如何为标题添加自定义布局?
  • 动态数据呢?
【解决方案2】:

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

【讨论】:

  • 有没有办法为每个部分的项目列表设置一个最小高度,这样如果列表很短,在下一个部分开始之前它仍然有一些高度?
  • 另外,有没有办法在没有内容项显示的时候显示一些东西?
【解决方案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】:

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

    您必须有一个 联系人列表,并带有 isFavourite like

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

    根据 isFavourite 和 contactName like this对该数组进行排序

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

    【讨论】:

      【解决方案5】:

      更新(2021 年 3 月)

      ExpandableListView 不会提供 RecyclerView 提供的性能和内存优势。因此,我分别在内置RecyclerViewAdapterRecyclerView 的顶部编写了自己的超轻量级库,该库由自定义回收器适配器和自定义回收器视图(仅在需要网格布局时才需要)组成。该适配器公开了一些类似于 iOS 表格视图工作方式的功能,因此在坚持您自己的代码设计的同时,可以非常容易地实现分段回收器视图。详细说明可以在下面链接的项目 github 页面中找到。该依赖项可从 maven Central 获得。

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

      【讨论】:

        【解决方案6】:

        看看我在 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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-05
          • 1970-01-01
          • 1970-01-01
          • 2015-09-23
          • 2023-03-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多