gpt4 book ai didi

listview - 在 Recyclerview 或 Listview 中添加多个标题。随机地

转载 作者:行者123 更新时间:2023-12-02 08:00:36 25 4
gpt4 key购买 nike

如何在 RecyclerView 或 listview 中添加多个标题或分隔线。随机如下图突出显示:

RecyclerView 中基于日期的多个标题 - 演示图片

enter image description here

最佳答案

您可以使用库 SectionedRecyclerViewAdapter 来实现它。它有“部分”的概念,每个部分可以有自己的标题。

在我看来,您需要按“日期”对通话进行分组。所以你可以像这样创建你的Section类:

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);
}
}

然后用您的部分设置 RecyclerView:

// 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);

关于listview - 在 Recyclerview 或 Listview 中添加多个标题。随机地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36277735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com