聊天消息在屏幕的任一侧打印

时间:2017-05-20 05:33:51

标签: android xml listview custom-adapter

我正在尝试模拟聊天应用程序,以在屏幕的任一侧打印发件人和收件人信息。 我使用了CustomAdapter并为服务器响应和用户发送的消息设置了两种不同的布局。 在这个模拟中,用户使用函数'sendMessage()'发送的每个消息都得到'testRecievServerMessage()',它在sendMessage()函数中被调用。

当我运行它时,UI总是被设置为服务器响应。如何解决此问题,以便为用户发送的消息和服务器发送的消息使用不同的布局。

请指导我出错的地方。

感谢您的帮助和建议。

这是我的代码

list_item.xml - userSent消息UI

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:weightSum="10">
    <LinearLayout
        android:id="@+id/llFrontSpacing"
        android:orientation="horizontal"
        android:visibility="gone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"></LinearLayout>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/tvUserMessage"
        android:background="@color/colorAccent"
        android:textColor="@color/colorWhite"
        android:textSize="18dp"
        android:padding="2dp"
        android:layout_weight="7"/>
     </LinearLayout>

list_item_server_response.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:weightSum="10">
<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/tvServerMessage"
    android:background="@color/colorPrimaryDark"
    android:textColor="@color/colorWhite"
    android:textSize="18dp"
    android:padding="2dp"
    android:layout_weight="7"/>
<LinearLayout
    android:id="@+id/llEndSpacing"
    android:orientation="horizontal"
    android:visibility="invisible"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"></LinearLayout>

</LinearLayout>

发送数据的按钮

 <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnSendMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_send"
        android:tint="@android:color/white"
        android:onClick="sendMessage"
         />

OnCreate已

   ListView lvMessages = (ListView) findViewById(R.id.lvMessages);
    customAdapter = new CustomAdapter();
    lvMessages.setAdapter(customAdapter);

SendMessage并接收消息和自定义适配器

   public void sendMessage(View v){

    String message = etMessage.getText().toString().trim();
    messages.add(message);
    serverResponse = false;
    customAdapter.notifyDataSetChanged();
    etMessage.setText("");
    testRecievServerMessage();
}

public void testRecievServerMessage(){
    String testServerMessage = "Test";
    messages.add(testServerMessage);
    serverResponse = true;
    customAdapter.notifyDataSetChanged();
}


class CustomAdapter extends BaseAdapter{

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        if(serverResponse == true){
            convertView = getLayoutInflater().inflate(R.layout.list_item_server_response, null);
            TextView tvSingleMessage = (TextView) convertView.findViewById(R.id.tvServerMessage);
            String message = messages.get(position);
            tvSingleMessage.setText(message);
        }else{
            convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            TextView tvSingleMessage = (TextView) 
            convertView.findViewById(R.id.tvUserMessage);
            String message = messages.get(position);
            tvSingleMessage.setText(message);
        }

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我的坏... 在重新填充整个列表时,布尔值'serverResponse'没有保留该值。

因此,我没有使用ArrayList,而是组合使用了HashMap和ArrayList,其中每条消息都有信息,无论是基于用户还是基于服务器的消息。

这里是整个代码供您参考。

 import android.support.design.widget.FloatingActionButton;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutCompat;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.LinearLayout;
 import android.widget.ListView;
  import android.widget.TextView;
 import android.widget.Toast;
 import org.w3c.dom.Text;
 import java.util.ArrayList;
 import java.util.HashMap;

public class ChatWindow extends AppCompatActivity {

ArrayList<HashMap<String, String>> allMessages = new 
ArrayList<HashMap<String,String>>();
HashMap<String, String> hmMessage;
CustomAdapter customAdapter;
EditText etMessage;
FloatingActionButton btnSendMessage;
Boolean serverResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    serverResponse = false;
    setContentView(R.layout.activity_chat_window);
    etMessage = (EditText) findViewById(R.id.etMessage);
    btnSendMessage = (FloatingActionButton) 
   findViewById(R.id.btnSendMessage);

    hmMessage = new HashMap<String, String>();
    hmMessage.put("Key_source", "User");
    hmMessage.put("key_message", "Hi");
    allMessages.add(hmMessage);
    hmMessage = new HashMap<String, String>();
    hmMessage.put("Key_source", "Server");
    hmMessage.put("key_message", "Connection established!.. How can I 
   help");
    allMessages.add(hmMessage);
    ListView lvMessages = (ListView) findViewById(R.id.lvMessages);
    customAdapter = new CustomAdapter();
    lvMessages.setAdapter(customAdapter);


    etMessage.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, 
  int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int 
    count) {
            if(s.length() > 0){
                btnSendMessage.setEnabled(true);
            }else{
                btnSendMessage.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
   }

   public void sendMessage(View v){

    String message = etMessage.getText().toString().trim();
    hmMessage = new HashMap<String, String>();
    hmMessage.put("Key_source", "User");
    hmMessage.put("key_message", message);
    allMessages.add(hmMessage);
    customAdapter.notifyDataSetChanged();
    etMessage.setText("");
    testRecievServerMessage();
  }

  public void testRecievServerMessage(){
    String testServerMessage = "Test";
    hmMessage = new HashMap<String, String>();
    hmMessage.put("Key_source", "Server");
    hmMessage.put("key_message", testServerMessage);
    allMessages.add(hmMessage);
    customAdapter.notifyDataSetChanged();
   }


    class CustomAdapter extends BaseAdapter{

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String source = allMessages.get(position).get("Key_source");
        String message = allMessages.get(position).get("key_message");
        if(source == "Server"){
            convertView = 
    getLayoutInflater().inflate(R.layout.list_item_server_response, null);
            TextView tvSingleMessage = (TextView) 
    convertView.findViewById(R.id.tvServerMessage);
            tvSingleMessage.setText(message);
        }else{
            convertView = getLayoutInflater().inflate(R.layout.list_item, 
    null);
            TextView tvSingleMessage = (TextView) 
    convertView.findViewById(R.id.tvUserMessage);
            tvSingleMessage.setText(message);
        }

        return convertView;
    }
      }


     }