在Recyclerview或Listview中添加多个标题。随机 [英] Adding multiple header in Recyclerview or Listview. randomly

查看:521
本文介绍了在Recyclerview或Listview中添加多个标题。随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在RecyclerView或listview中添加多个标题或分隔符。随机显示,如下图所示:

在RecyclerView中的多个标题的基础日期 - 演示图像



。它有部分的概念,每个部分都可以有自己的标题。



它让我觉得你需要用日期来分组你的电话。所以你可以像这样创建你的Section类:

  class MySection extends StatelessSection {

String date;
列表<致电>呼叫清单;
$ b $ public MySection(String date,List< Call> callList){
//调用构造函数为这个Section header,footer和items
super(R.layout。 section_header,R.layout.section_footer,R.layout.section_item);

this.date = date;
this.callList = callList;
}

@Override
public int getContentItemsTotal(){
return callList.size(); //本节的项目数
}

@Override
public RecyclerView.ViewHolder getItemViewHolder(View view){
//返回一个ViewHolder自定义实例这部分的项目
返回新的MyItemViewHolder(view);

$ b @Override
public void onBindItemViewHolder(RecyclerView.ViewHolder持有者,int位置){
MyItemViewHolder itemHolder =(MyItemViewHolder)持有者;

//在此处绑定您的视图
itemHolder.tvItem.setText(callList.get(position).getContactName());
}

@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view){
return new SimpleHeaderViewHolder(view);

$ b @Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder持有者){
MyHeaderViewHolder headerHolder =(MyHeaderViewHolder)持有者;

//在这里绑定你的标题视图
headerHolder.tvItem.setText(date);


然后你用你的章节设置RecyclerView:

  //创建SectionedRecyclerViewAdapter的实例
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

MySection mySection1 = new MySection(2016年3月27日,callList1);
MySection mySection2 = new MySection(2016年3月28日,callList2);

//添加节
sectionAdapter.addSection(mySection1);
sectionAdapter.addSection(mySection2);

//用SectionedRecyclerViewAdapter设置你的RecyclerView
RecyclerView recyclerView =(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(新的LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);


How can a add multiple header or dividers in RecyclerView or listview. randomly as highlighted in the image below:

Multiple heading in RecyclerView on the basic of date - Demo Image

解决方案

You can achieve it using the library SectionedRecyclerViewAdapter. It has the concept of "Sections" and each section can have its own header.

It looks to me that you need to group your calls by "date". So you can create your Section class like this:

class MySection extends StatelessSection {

    String date;
    List<Call> callList;

    public MySection(String date, List<Call> callList) {
        // 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.date = date;
        this.callList = callList;
    }

    @Override
    public int getContentItemsTotal() {
        return callList.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(callList.get(position).getContactName());
    }

    @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(date);
    }
}

Then you set up the RecyclerView with your Sections:

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

MySection mySection1 = new MySection("27 Mar, 2016", callList1);
MySection mySection2 = new MySection("28 Mar, 2016", callList2);

// Add your Sections
sectionAdapter.addSection(mySection1);
sectionAdapter.addSection(mySection2);

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

这篇关于在Recyclerview或Listview中添加多个标题。随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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