Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用header在recyclerview中创建组项目

如何使用header在recyclerview中创建组项目
EN

Stack Overflow用户
提问于 2016-03-21 10:19:47
回答 1查看 6.8K关注 0票数 2

我想在recyclerview中设置组头,这意味着我有一个包含json对象的api,它包含基于课程的json对象例如Lesson1包含超过3个项目第二课包含4个项目,我已经从api中得到了响应,并像这样显示在recyclerview中。

代码语言:javascript
运行
AI代码解释
复制
Lesson 1:
Lesson 1:Random Numbers
Lesson 1:Complex Numbers
Lesson 1:Matrix

-

代码语言:javascript
运行
AI代码解释
复制
Lesson 2:
Lesson 2:Algebra
Lesson 2:Differentiation
Lesson 2:Integration 

就像这样,我在recyclerview .I中显示了我想要的组标题显示像这样

代码语言:javascript
运行
AI代码解释
复制
Lesson 1:
Random Numbers
Complex Numbers
Matrix

-

代码语言:javascript
运行
AI代码解释
复制
Lesson 2:
Algebra
Differentiation
Integration 

想要设置标题和分组的项目,请帮助我朋友

EN

回答 1

Stack Overflow用户

发布于 2016-03-31 16:04:10

使用库SectionedRecyclerViewAdapter,您可以按部分对项目进行分组,并为每个部分添加一个标题:

代码语言:javascript
运行
AI代码解释
复制
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_footer,  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);
    }
}

然后使用您的部分设置RecyclerView:

代码语言:javascript
运行
AI代码解释
复制
// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

MySection lesson1Section = new MySection("Lesson 1", lesson1List);
MySection lesson2Section = new MySection("Lesson 2", lesson2List);

// Add your Sections
sectionAdapter.addSection(lesson1Section);
sectionAdapter.addSection(lesson2Section);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36128332

复制
相关文章

相似问题

如何使用MVP在RecyclerView中插入/删除项目

20

TFS:在创建项目之前,我如何创建项目组?

20

RecyclerView在项目之间创建边距

40

在recyclerView中创建包含多个项目的TextView

20

使用项目组在NetSuite中创建SalesOrder

22
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档