0

我正在尝试制作一个聊天程序。我的问题是每次背景图像更新(发件人更改)时,数组中的每个项目都会获得相同的背景。如何更改此设置,以便传出消息获得一种类型的背景,而传入消息获得另一种类型的背景?

    public class RowAdapter extends BaseAdapter {

private Context context;
private int textViewResourceId;
private final ArrayList<String> messageList;
private final ArrayList<String> senderList;
public String messageDirection; 
boolean out; 

public RowAdapter(Context context, int textViewResourceId, ArrayList<String> messageList, ArrayList<String> senderList) {
super();

this.textViewResourceId = textViewResourceId;
this.messageList = messageList; 
this.senderList = senderList; 
this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){ 
View rowView = convertView;
ViewHolder holder;
long timestamp = System.currentTimeMillis();
String readableTimestamp = (String)DateUtils.getRelativeTimeSpanString(timestamp);

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(textViewResourceId, parent, false);

    holder = new ViewHolder();
    holder.label = (TextView)rowView.findViewById(R.id.label);
    holder.message=(TextView)rowView.findViewById(R.id.message);
    holder.time = (TextView)rowView.findViewById(R.id.textTime);
    holder.RL = (RelativeLayout)rowView.findViewById(R.id.userAndMessage);
    rowView.setTag(holder);
}else{
    holder = (ViewHolder)rowView.getTag();
}

holder.label.setText(senderList.get(position));
holder.message.setText(messageList.get(position));
holder.time.setText(readableTimestamp);

if(out){
    if (messageList.get(position)!= null) {
        holder.RL.setBackgroundResource(R.drawable.yellow);
       ((RelativeLayout) rowView).setGravity(Gravity.RIGHT);
    }

}else{
    if (messageList.get(position)!= null) {
        holder.RL.setBackgroundResource(R.drawable.green);
       ((RelativeLayout) rowView).setGravity(Gravity.LEFT);
    }
}
return rowView;
}

public void setOut(boolean out){
this.out = out; 
}
static class ViewHolder{
RelativeLayout RL; 
TextView label;
TextView message;
TextView time;

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

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

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

}
4

1 回答 1

2

好的,尝试这样做

这只是一个示例,您必须在其中添加您的流程

主布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView_Chat"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:background="#123456"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="fromMeBt"
            android:text="From me" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="fromOtherBt"
            android:text="From other" />
    </LinearLayout>

</LinearLayout>

添加列表行xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/txt_chat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

创建一个名为 ChatObject 的新类

public class ChatObject {

    private String mssg;
    private int from;

    public ChatObject(String mssg, int from) {
        super();
        this.mssg = mssg;
        this.from = from;
    }

    public String getMssg() {
        return mssg;
    }

    public void setMssg(String mssg) {
        this.mssg = mssg;
    }

    public int getFrom() {
        return from;
    }

    public void setFrom(int from) {
        this.from = from;
    }

}

现在,像这样对您的 MainActivity 进行一些更改

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ArrayList<ChatObject> items = new ArrayList<ChatObject>();
    ListView listView_chat;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new CustomAdapter(items);
        listView_chat = (ListView) findViewById(R.id.listView_Chat);
        listView_chat.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void fromMeBt(View v) {
        ChatObject object = new ChatObject("from me :)", 0);
        items.add(object);
        adapter.notifyDataSetChanged();
        listView_chat.setSelection(items.size());
    }

    public void fromOtherBt(View v) {
        ChatObject object = new ChatObject("from other :)", 1);
        items.add(object);
        adapter.notifyDataSetChanged();
        listView_chat.setSelection(items.size());
    }

    class CustomAdapter extends BaseAdapter {

        ArrayList<ChatObject> items;

        public CustomAdapter(ArrayList<ChatObject> items) {
            // TODO Auto-generated constructor stub
            this.items = items;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_chat, null);
            TextView txt_chat = (TextView) convertView.findViewById(R.id.txt_chat);

            ChatObject object = items.get(position);
            txt_chat.setText(object.getMssg());

            if (object.getFrom() == 0) {
                convertView.setBackgroundColor(Color.BLUE);
            }else {
                convertView.setBackgroundColor(Color.CYAN);
            }

            return convertView;
        }

    }
}

就是这样,如果您想更改 xml 布局中的填充或将其添加到您的 dimens.xml 文件

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>

现在开心?我希望这对你有帮助

于 2013-10-16T11:08:42.257 回答