【问题标题】:Group ListView items by date has strange vehavior按日期分组 ListView 项目有奇怪的行为
【发布时间】:2014-12-19 22:52:26
【问题描述】:

我有一个包含消息列表的聊天窗口。我想像whatsapp一样按日期对消息进行分组。 我的解决方案是将标题添加到所有项目,并使其仅在项目是具有该日期的第一个项目时才可见,并将其隐藏在其他位置。我得到每条消息的年份并进行比较,但没有得到预期的行为。有时列表显示正确的日期,然后日期消失,看起来与下图不同。我认为问题在于 getView 没有按顺序调用。

public View getView(int position, View convertView, ViewGroup parent) {


    View row = convertView;
    MessageHolder holder = null;
    ChatMessageThread msg = data.get(position);

    int  msgData = TimingUtils.getDateFromStringTimeStamp(msg.getSendTime());



    if(row == null)
    {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        row                     = inflater.inflate(layoutResourceId, parent, false);

        holder                  = new MessageHolder();
        holder.time             = (TextView)row.findViewById(R.id.msg_time);
        holder.txtTitle         = (TextView)row.findViewById(R.id.list_messages_title);
        holder.txtDate          = (TextView) row.findViewById(R.id.msg_date);

        row.setTag(holder);   
    }
    else
    {
        holder = (MessageHolder)row.getTag();
    }

    if (position>0 && msg.getSendTime()!=null ) {

    }



    holder.txtTitle.setText(msg.getContent());
    holder.time.setText(TimingUtils.getFormattedDate(msg.getSendTime()));
    holder.txtDate.setText(""+TimingUtils.getFormattedDate(msg.getSendTime()));

    LayoutParams lp = (LayoutParams) holder.txtTitle.getLayoutParams();

    if (msg.getUserId().equals( GlobalVaribles.account.getUserId())) {

        holder.txtTitle.setBackgroundResource(R.drawable.outgoing);
        lp.gravity = Gravity.RIGHT;

    } else {

        holder.txtTitle.setBackgroundResource(R.drawable.incoming);

        lp.gravity = Gravity.LEFT;

    }
    holder.txtTitle.setLayoutParams(lp);
    holder.time.setLayoutParams(lp);



    //first item only
    if (currentTempDate == -1) {
        currentTempDate = msgData;
        holder.txtDate.setVisibility(View.VISIBLE);

    } else {

        if (msgData == currentTempDate) {
            Log.i("DAY",""+msg.getContent()+ "msgData"+msgData+ "currentTempDate"+currentTempDate);

            holder.txtDate.setVisibility(View.GONE);
        } else if (msgData>currentTempDate) {
            holder.txtDate.setVisibility(View.VISIBLE);
            currentTempDate = msgData;
        } 
    }


    return row;
}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    我使用数据源获取上一项,不依赖 getView 调用

    //Current Item
    
            try {
                currentItem = (ChatMessageThread) getItem(position);
            } catch (Exception e) {
            }
    
        try {
            previousItem = (ChatMessageThread) getItem(position-1);
        } catch (Exception e) {
            previousItem = null;
        }
    
        if (previousItem ==null) {
            previousItem = currentItem;
            holder.txtDate.setVisibility(View.VISIBLE);
        } else {
    
            if (TimingUtils.getDay(currentItem.getSendTime()) == 
                    TimingUtils.getDay(previousItem.getSendTime())) {
                holder.txtDate.setVisibility(View.GONE);
            } else{
                holder.txtDate.setVisibility(View.VISIBLE);
            }
    
            previousItem = currentItem;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2020-05-02
      • 1970-01-01
      • 2012-03-10
      相关资源
      最近更新 更多