我想在回收器视图中以这种方式在android中列出我的数据,我该如何继续下去。如何根据日期划分数据。
并且数据采用这种形式
[
{
"type": "IMPORTED",
"title": "Google Cal X",
"datetime": 1541340000000
},
{
"type": "IMPORTED",
"title": "Google Cal Z",
"datetime": 1541343600000
},
{
"type": "HOLIDAY",
"title": "Summer Bank Holiday 3"
},
{
"type": "HOLIDAY",
"title": "Summer Bank Holiday 4"
}
]
请帮帮我。
答案 0 :(得分:1)
要么采用Ketan Ramani提供的解决方案,要么按照onBindViewHolder()
注意:要遵循我的回答,首先应从JSON排序 根据日期和时间的后端,例如新项目应排在第一 列表
首先,为数据创建一个ModelClass
,例如,您的JSON类型中包含三项内容,即标题,DateTime,因此您的模型类将如下所示,
public class ModelClass{
public String title;
public String type;
public String dateTime;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
此后,只需转到single_item.xml
目录中的layout
视图,并添加一个textView
以在视图顶部显示日期,就像参考图像中的显示并设置其VISIBILITY
至GONE
现在onBindViewHolder()
中使用position
这样简单地将列表中的当前日期与下一个getter-setter
日期进行匹配,
if (position < notificationListData.size()) {
String previousDateTime = (((ModelClass) notificationListData.get(position)).getDateTime());
if(previousDateTime.equals((((ModelClass) notificationListData.get(position+1)).getDateTime()))){
hide the textView containing date;
}else{
show text view containing Date and setDate on that textView
}
}
让我知道这是否有帮助。