【问题标题】:Chat messages to print on either side of the screen聊天消息打印在屏幕的任一侧
【发布时间】:2017-05-20 05:33:50
【问题描述】:

我正在尝试模拟聊天应用程序以在屏幕的任一侧打印发送者和接收者消息。 我使用了 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);

发送消息和接收消息和自定义适配器

   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;
    }
}

【问题讨论】:

    标签: android xml listview custom-adapter


    【解决方案1】:

    这是我的错.. 重新填充整个列表时,布尔值“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;
        }
          }
    
    
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      相关资源
      最近更新 更多