具有不同布局的分段 RecyclerView [英] Sectioned RecyclerView with different layouts

查看:35
本文介绍了具有不同布局的分段 RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我需要创建一个包含 3 个部分的 RecyclerView
名为第一"的第一部分应显示 3 行,每行包含 TextView 和 EditText
名为Second"的第二部分应包含 2 行,每行显示一个 TextView

I had a scenario where I need to create a RecyclerView with 3 sections
The first section named as "First" should display 3 rows with each row containing TextView and EditText
Second section named as "Second" should contain 2 rows with each row displaying a single TextView

名为Third"的第三部分应包含 4 行,每行显示一个带有图像的 TextView
.有没有办法使用 RecyclerView 实现这一点?
任何人都可以分享我的链接或示例代码片段以实现此功能

Third section named as "Third" should contain 4 rows with each row displaying a TextView with image
. Is there a way in which can I achieve this using RecyclerView ?
Can any one share me links or sample code fragments to achieve this functionality

提前致谢

推荐答案

您可以使用库SectionedRecyclerViewAdapter将数据分组.

You can use the library SectionedRecyclerViewAdapter to group your data into sections.

首先创建一个Section类:

First create a Section class:

class MyFirstSection extends StatelessSection {

    String title;
    List<String> list;

    public MyFirstSection(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);
    }
}

然后你用你的部分设置 RecyclerView:

Then you set up the RecyclerView with your Sections:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each year
MyFirstSection section1 = new MyFirstSection("First", firstDataList);
MySecondSection section2 = new MySecondSection("Second", secondDataList);
MyThirdSection section3 = new MyThirdSection("Third", thirdDataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
sectionAdapter.addSection(section3);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

这篇关于具有不同布局的分段 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆