我关注了这个question's answer用于在我的 recyclerView 中添加部分
为了添加部分,我们必须提供如下部分的索引:
//Sections
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0,"Section 1"));
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(5,"Section 2"));
因此第 1 部分将位于索引 0,第 2 部分将位于索引 5
现在我有 ChannelsInformation
数据集,channelInformation 类是:
public class ChannelsInformation {
String id,channelName;
boolean isSelected , isfavorite;
}
所以我的问题是我必须做什么才能将数据集添加到不同的部分?假设我有一个数据
<id = 1 , channelName = channelName1 , isSelcted = false , isfavorite = true >
2 , channelName2 , false , false
3 , channelName3 , false , true
4 , channelName4 , false , false
所以上面是一些数据的演示,现在我想根据两个 isFavorite
字段将该数据添加到我的 recyclerView 中,在 recyclerView 中添加数据后,它应该如下所示:
section 1
1 - channelName1
3 - channelName3
section 2
2 - channelName2
4 - channelName4
知道我必须做什么才能将数据添加到表单中,以便它按照上面的示例进行组织吗?
这就是将数据填充到我的 recyclerView 中(在我的 fragment 类上)的方法:
public static List<ChannelsInformation> getData(){
List<ChannelsInformation> data = new ArrayList<>();
for (int i = 0 ; i<channelsArray.size() && i<channelsIDArray.size() ; i++ ){
ChannelsInformation current = new ChannelsInformation();
current.channelName = channelsArray.get(i);
current.id = channelsIDArray.get(i);
current.isFavorite = channelIsFavoriteArray.get(i);
data.add(current);
}
return data;
}
知道该怎么做吗?任何提示或指导将不胜感激
如果代码不够,请告诉我,我会更新它
最佳答案
您可以根据 boolean 值“isfavorite”对列表进行排序,如下所示:
public class ChannelsInformation implements Comparable<ChannelsInformation> {
String id, channelName;
boolean isSelected, isfavorite;
@Override
public int compareTo(ChannelsInformation o) {
int b1 = this.isfavorite ? 1 : 0;
int b2 = o.isfavorite ? 1 : 0;
return b2 - b1;
}
}
然后调用Collections.sort(data);设置数组列表后。
关于java - 将值添加到 recyclerView 的部分中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884827/