如何在聊天适配器中标记特定的聊天气泡

时间:2016-01-26 10:35:58

标签: java android chat android-arrayadapter adapter

我有群组聊天,我想标记特定消息(isProducertrue时发送的消息)。问题是它将所有消息全部标记在一起...... 这是我的Adapter代码:

if(customer_id.equals(message.getUserId()))
            isMe = true;
        if(message.getUserId().equals(producer_id))
            isProducer = true;

        if (isMe) {
            holder.imageRight.setVisibility(View.VISIBLE);
            holder.imageLeft.setVisibility(View.GONE);
            holder.body.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

        } else {
            holder.imageLeft.setVisibility(View.VISIBLE);
            holder.imageRight.setVisibility(View.GONE);
            holder.body.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        }

        holder.body.setText(message.getBody());
        if (isProducer) {
            holder.body.setTypeface(null, Typeface.BOLD);
        }

日Thnx!

2 个答案:

答案 0 :(得分:0)

if(customer_id.equals(message.getUserId()))
            isMe = true;
        if(message.getUserId().equals(producer_id))
            isProducer = true;

        if (isMe) {
            holder.imageRight.setVisibility(View.VISIBLE);
            holder.imageRight.setbackground(R.drawable.rightimg);
            holder.imageLeft.setVisibility(View.GONE);
            holder.body.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

        } else {
            holder.imageLeft.setVisibility(View.VISIBLE);
            holder.imageRight.setbackground(R.drawable.leftimg);
            holder.imageRight.setVisibility(View.GONE);
            holder.body.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        }

        holder.body.setText(message.getBody());
        if (isProducer) {
            holder.body.setTypeface(null, Typeface.BOLD);
        }

}

你可以用户R.drawable.leftimg和R.drawable.rightimg应该是ninepatch image

答案 1 :(得分:0)

试试这个

isMe = customer_id.equals(message.getUserId()); 
    isProducer = message.getUserId().equals(producer_id);

    if (isMe) {
        holder.imageRight.setVisibility(View.VISIBLE);
        holder.imageLeft.setVisibility(View.GONE);
        holder.body.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

    } else {
        holder.imageLeft.setVisibility(View.VISIBLE);
        holder.imageRight.setVisibility(View.GONE);
        holder.body.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    }

    holder.body.setText(message.getBody());
    if (isProducer) {
        holder.body.setTypeface(null, Typeface.BOLD);
    } else {
        holder.body.setTypeface(null, Typeface.NORMAL); // Or anything else
     }