如何以这种方式在回收站视图中打印数据?

时间:2018-12-03 07:02:32

标签: android android-recyclerview

我想在回收器视图中以这种方式在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"
}
]

请帮帮我。

1 个答案:

答案 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以在视图顶部显示日期,就像参考图像中的显示并设置其VISIBILITYGONE

现在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
            }
}

让我知道这是否有帮助。