我有一个列表要传递给RecyclerView。WifiNetwork对象包含一个布尔值"inRange“。
在我的标题中,我想要这个结构:- RecyclerView : IN RANGE - items with boolean inRange == true - header: NOT IN RANGE - items with boolean inRange == false
我似乎找不到一种简单的方法来做这件事。
我的尝试:-在我的活动中直接加上“在范围内”和“不在范围内”的标签,并使用了2个RecyclerViews (这很丑陋)-- afollestad的分段回收视图(这对我来说有点不清楚)
这肯定是非常常见的。你们是怎么处理这事的?
发布于 2016-07-07 12:57:20
您可以使用库SectionedRecyclerViewAdapter将数据分组为多个部分。
首先创建一个Section类:
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_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:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data for each year
MySection section1 = new MySection("IN RANGE", inRangeDataList);
MySection section2 = new MySection("NOT IN RANGE", notInRangeDataList);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
https://stackoverflow.com/questions/37880158
复制相似问题