下面是我完整的sendbird chat适配器类。当前显示每个消息的时间日期。viewHolder.getView("left_time")
和viewHolder.getView("right_time")
是用于显示日期和时间的视图,它是按方法{{1我想要只在日期发生变化时才显示日期和时间。例如,如果有一组日期为23日的消息,那么日期和时间应仅显示日期为23日6月的第一条消息。
我尝试通过在xyz变量中存储日期并将其与每条消息的日期进行比较并且每当它们不相等时检测日期发生变化,然后使该消息的日期视图可见并将新日期分配给xyz来尝试这样做用于与未来消息进行比较的变量。但是当我向下滚动并向上滚动列表时,此方法失败。 实现这一目标的理想方法是什么?
getDisplayDateTime
答案 0 :(得分:1)
几个月前我有一个similar problem (ref this SO Question)除了从ArrayAdapter<MyObject>
而不是BaseAdapter
延伸之外什么也没有用。
因此,将适配器的基类更改为ArrayAdapter<MyObject>
,并实现抽象类的提示方法。您可能需要将现有代码移动到这些被覆盖的方法以获得更好的性能。
答案 1 :(得分:1)
尝试以下代码,
private static String getDisplayDateTime(Context context, long milli, long milli2)
{
Date date = new Date(milli);
Date previousDate = new Date(milli2);
String strCurrentDate = "", strPreviousDate = "";
if (System.currentTimeMillis() - milli < 60 * 60 * 24 * 1000l)
{
strCurrentDate = DateFormat.getTimeFormat(context).format(date);
if(milli2 != -1)
strPreviousDate = DateFormat.getTimeFormat(context).format(previousDate);
//return strCurrentDate.equals(strPreviousDate) ? "" : strCurrentDate;
}
else
{
strCurrentDate = DateFormat.getDateFormat(context).format(date) + " " + DateFormat.getTimeFormat(context).format(date);
if(milli2 != -1)
strPreviousDate = DateFormat.getDateFormat(context).format(strPreviousDate) + " " + DateFormat.getTimeFormat(context).format(strPreviousDate);
}
return strCurrentDate.equals(strPreviousDate) ? "" : strCurrentDate;
}
现在调用您的方法,如下所示
if(position > 0)
{
final Object previousItem = getItem(position - 1);
Message previousMessage = (Message) previousItem;
viewHolder.getView("right_time", TextView.class).setText(getDisplayDateTime(mContext, message.getTimestamp(), previousMessage.getTimestamp()));
}
else
{
viewHolder.getView("right_time", TextView.class).setText(getDisplayDateTime(mContext, message.getTimestamp(), -1));
}
答案 2 :(得分:0)