Either go with the solution provided by Ketan Ramani or follow this simple trick in onBindViewHolder()
Note: to follow my answer, first your JSON should be sorted from the
backend according to date and time like new Items should go first in
the list
First, make a ModelClass
for your data, for example, you have three things in your JSON type, title, DateTime, so your model class for this will look like this,
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;
}
}
After this just go to your single_item.xml
view in the layout
directory and add a textView
to show date at the top of the view just like the show in your reference image and set its VISIBILITY
to GONE
Now in onBindViewHolder()
simple match the current date in the list with next position
date using getter-setter
like this,
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
}
}
Let me know if this was helpful.