gpt4 book ai didi

android - 如何使用标题在recyclerview中制作组项目

转载 作者:行者123 更新时间:2023-11-29 20:02:04 24 4
gpt4 key购买 nike

我想在 recyclerview 中设置组 header ,这意味着我有一个 api,其中包含基于 Lessons 的 json 对象,例如 Lesson1 包含超过 3 个项目 Lesson 2 包含 4 个项目。我已经从 api 得到响应并显示到 recyclerview 中这个。

Lesson 1:
Lesson 1:Random Numbers
Lesson 1:Complex Numbers
Lesson 1:Matrix

-

Lesson 2:
Lesson 2:Algebra
Lesson 2:Differentiation
Lesson 2:Integration

像这样我显示到 recyclerview 中。我想像这样对标题显示进行分组

Lesson 1:
Random Numbers
Complex Numbers
Matrix

-

Lesson 2:
Algebra
Differentiation
Integration

想设置标题和分组项目请帮助我 friend

最佳答案

同图书馆SectionedRecyclerViewAdapter您可以将项目分组并为每个部分添加标题:

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

然后您使用您的 Sections 设置 RecyclerView:

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

关于android - 如何使用标题在recyclerview中制作组项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36128332/

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