我已经在聊天屏幕上创建了,我想在左侧显示已发送的消息,在右侧显示已接收的消息。
为此,我创建了两个 xml,一个用于右侧,一个用于左侧,我已经绑定(bind)了这两个带有适配器的 xml。
问题是,当我发送文本时,它位于左侧,当我接收文本时,发送的文本也在右侧对齐。当我再次发送时,整个文本(聊天)靠左对齐。
这是我的 ArrayAdapter:
private void setListAdapterL() {
ArrayAdapter<String> adapterL = new ArrayAdapter<String>(this,
R.layout.row_left, messages);
mList.setAdapter(adapterL);
}
private void setListAdapterR() {
// TODO Auto-generated method stub
ArrayAdapter<String> adapterR = new ArrayAdapter<String>(this,
R.layout.row_right, messages);
mList.setAdapter(adapterR);
}
我的 xml 文件,用于 row_right.xml。在其他 row_left.xml 中,只是引力发生了变化:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="5dip" />
任何想法和建议将不胜感激。
谢谢
最佳答案
为此,您必须为 ListView 创建自定义适配器,而不是为左项(即已发送项)和右项(即已接收项)创建两个单独的适配器。
这意味着您已经在一个自定义适配器中管理发送/接收的消息。
例如
// Sample messages inside MessageActivity
void initMessages() {
HashMap<String, String> messageSent = new HashMap<String, String>();
messageSent.put("message", "Hi");
messageSent.put("type", "sent");
messageSent.put("date", "");
HashMap<String, String> messageReceived = new HashMap<String, String>();
messageSent.put("message", "Hello");
messageSent.put("type", "received");
messageSent.put("date", "");
ArrayList<HashMap<String, String>> messages = new ArrayList<HashMap<String, String>>();
messages.add(messageSent);
messages.add(messageReceived);
MessageAdapter adapter = new MessageAdapter(mContext, messages);
setListAdapter(apater);
}
// adapter class
class MessageAdapter extends ArrayAdapter<HashMap<String, String>> {
LayoutInflater inflater;
public MessageAdapter(Context context, ArrayList<HashMap<String, String>> data) {
super(context, R.layout.raw_message, data);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.raw_message, null);
holder = new ViewHolder();
holder.txtSent = convertView.findViewByTag("sent");
holder.txtReceived = convertView.findViewByTag("received");
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
HashMap<String,String> message = getItem(position);
boolean sent = message.get("type").equals("sent");
if(sent) {
holder.txtSent.setVisibility(View.VISIBLE);
holder.txtSent.setText(message.get("message"));
holder.txtReceived.setVisibility(View.GONE);
} else {
holder.txtSent.setVisibility(View.GONE);
holder.txtReceived.setText(message.get("message"));
holder.txtReceived.setVisibility(View.VISIBLE);
}
}
class ViewHolder {
TextView txtSent;
TextView txtReceived;
}
}
// raw_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tag="sent"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tag="received"/>
</RelativeLayout>
关于android - ListView 项目未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10697249/