如何在recyclerview中添加聊天日期? [英] How to add dates in recyclerview for a chat?

查看:102
本文介绍了如何在recyclerview中添加聊天日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚要在聊天中创建日期列表项的方式,以显示消息所属的日期.

Im trying to figure out what the way to go would be in order to create date-list-items for in a chat that show from which date the messages belong to.

列表如下:

--- 1 week ago ---
msg
msg
msg
msg
msg
----- today -----
msg 
msg 
msg
msg

我可以做到的一种方法是创建日期时间列表项,然后使用某种逻辑来确定日期时间列表项应位于哪个位置.

One way i can do it is by creating date time list items and then using some logic to decide on which position a date-time-list-item should go.

我当时想创建一个用于显示日期的自定义列表分隔符是可能的,但是我不确定是否可行.

I was thinking that it may be possible to create a custom list divider for showing the date but i am not sure if this is possible.

你们将如何处理?

推荐答案

您知道RecyclerView具有多种视图类型.这意味着您可以根据需要绘制预定义的行.

You know that RecyclerView has multiple view types. It means that you can draw predefined rows as need.

在聊天示例中,确切的日期和消息数据将在服务器端填充.您只需要绘制准备就绪的信息(也许是json).

In chat example, exact dates and messages data will be populated at the server side. You need to draw just ready information (maybe json).

我希望这个建议可以节省您的时间.

I hope that this suggestion may save your time.

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
    }

    @Override
    public int getItemViewType(int position) {
        // Just as an example, return 0 or 2 depending on position
        // Note that unlike in ListView adapters, types don't have to be contiguous
        return position % 2 * 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }
}

这篇关于如何在recyclerview中添加聊天日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆