在同一个气泡中显示用户的连续聊天

时间:2014-08-01 04:17:55

标签: android android-listview whatsapp

我正在开发聊天应用.. 这是我到目前为止所做的。 chat bw two

现在我想要的是这个: grouping continuous chat from a user

请我被困住,不知道如何在这个UI上工作.. 即 如果用户视图再次查看聊天,那么如果用户持续使用msg,则将其显示在与第二张图像相同的气泡中。 这个聊天事务都是本地的,没有使用任何服务或服务器。 我正在给我的代码。

MessageActivity.java

public class MessageActivity extends ListActivity {
/** Called when the activity is first created. */

ArrayList<Message> messages;
AwesomeAdapter adapter;
EditText text;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    text = (EditText) this.findViewById(R.id.text);


    this.setTitle("Harish");
    messages = new ArrayList<Message>();
    adapter = new AwesomeAdapter(this, messages);
    setListAdapter(adapter);

}
public void sendMessage(View v)
{
    String newMessage = text.getText().toString().trim(); 
    //String msg="\nTesting";
    if(newMessage.length() > 0)
    {
        text.setText("");
        //addNewMessage(new Message(newMessage+msg, true));
        addNewMessage(new Message(newMessage, true));
    }
}
public void sendMessage2(View v)
{
    String newMessage = text.getText().toString().trim(); 
    if(newMessage.length() > 0)
    {
        text.setText("");
        addNewMessage(new Message(newMessage, false));

    }
}
void addNewMessage(Message m)
{
    messages.add(m);
    adapter.notifyDataSetChanged();
    getListView().setSelection(messages.size()-1);
}

}

AwesomeAdapter.java

public class AwesomeAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Message> mMessages;



public AwesomeAdapter(Context context, ArrayList<Message> messages) {
    super();
    this.mContext = context;
    this.mMessages = messages;
}

@Override
public int getCount() {
    return mMessages.size();
}

@Override
public Object getItem(int position) {       
    return mMessages.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Message message = (Message) this.getItem(position);

    ViewHolder holder; 
    if(convertView == null)
    {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row, parent, false);
        holder.message = (TextView) convertView.findViewById(R.id.message_text);
        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();

    holder.message.setText(message.getMessage());

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


        if(message.isMine())
        {
            holder.message.setBackgroundResource(R.drawable.me);
            lp.setMargins(15, 10, 0, 10);


        }

        else
        {
            holder.message.setBackgroundResource(R.drawable.you);
            lp.setMargins(0, 10, 15, 10);
        }
        holder.message.setLayoutParams(lp);


    return convertView;
}
private static class ViewHolder
{
    TextView message;
}


@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

}

Message.java

public class Message {

String message;

boolean isMine;

public Message(String message, boolean isMine) {
    super();
    this.message = message;
    this.isMine = isMine;

}
/**
 * Constructor to make a status Message object
 * consider the parameters are swaped from default Message constructor,
 *  not a good approach but have to go with it.
 */
public Message(boolean status, String message) {
    super();
    this.message = message;
    this.isMine = false;

}
public String getMessage() {
    return message;
}

public boolean isMine() {
    return isMine;
}

}

1 个答案:

答案 0 :(得分:1)

在收到新消息时,将该消息附加到ArrayList<Message>中的上一条消息(即从arraylist获取上一条消息,并添加附加换行符\n的新消息)应该工作)并致电adapter.notifyDataSetChanged();