我的ListView应用最后一项的布局样式

问题描述

我正在尝试在Android中建立聊天,因此,当我收到消息对象时,我希望它在屏幕左侧和右侧发送我发送的消息。我建立了两个布局,一个用于接收的消息,另一个用于我发送的消息。但是,当我尝试将所有内容放到ListView中时,消息的样式与上一个样式相同。

例如,如果对话的最后一条消息来自我,则所有消息都将在屏幕右侧。并且如果最后一条消息是来自其他用户的,则所有消息都将在屏幕的左侧。

对不起,英语错误

定义消息类型的代码

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.parse.ParSEObject;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;

class ChatArrayAdapter extends ArrayAdapter<ParSEObject> {

    private TextView chatText;
    private List<ParSEObject> chatMessageList = new ArrayList<ParSEObject>();
    private Context context;

    @Override
    public void add(ParSEObject object) {
        chatMessageList.add(object);
        super.add(object);
    }

    public ChatArrayAdapter(Context context,int textViewResourceId) {
        super(context,textViewResourceId);
        this.context = context;
    }

    public int getCount() {
        return this.chatMessageList.size();
    }

    public ParSEObject getItem(int index) {
        return this.chatMessageList.get(index);
    }

    public View getView(int position,View convertView,ViewGroup parent) {
        ParSEObject chatMessageObj = getItem(position);
        View row = convertView;
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (chatMessageObj.get("fromUser").equals(ParseUser.getCurrentUser())) {
            System.out.println("Message envoyé par : " + chatMessageObj.get("fromUserId"));
            row = inflater.inflate(R.layout.layout_message_send,parent,false);
        }else{
            System.out.println("Message envoyé par : " + chatMessageObj.get("fromUserId"));
            //row = inflater.inflate(R.layout.layout_message_send,false);

            row = inflater.inflate(R.layout.layout_message_receive,false);
        }
        chatText = (TextView) row.findViewById(R.id.messageText);
        System.out.println(chatMessageObj.get("message") + "**********************");
        chatText.setText(chatMessageObj.get("message").toString());
        return row;
    }
}

XML收到的消息:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--
        android:background="@drawable/textview"
        android:layout_gravity="left"
    -->
    <TextView
        android:id="@+id/messageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="20dp"
        android:paddingBottom="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:text="@string/sample"
        android:textColor="#000" />
</RelativeLayout>

XML发送的消息:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--  android:background="@drawable/textview" -->
    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/messageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="20dp"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:text="@string/sample"
        android:textColor="#000" />
</LinearLayout>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)