android - ListView 项目未正确对齐

标签 android xml android-layout

我已经在聊天屏幕上创建了,我想在左侧显示已发送的消息,在右侧显示已接收的消息。
为此,我创建了两个 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/

相关文章:

java - 如果您按下按钮返回 MainActivity,Android 应用程序将退出

android - View 中的选项卡

android - Layout_weight 不能正常工作

android - 如何在 ImageView 上重叠 Cardview?

c# - Json.net:JObject.SelectToken 可以做 XPath 可以做的事情吗?如果是,语法是什么?

android - 在 Android 中将已解析的 Xml 数据显示到 ListView

android - 如何使用 Web 服务连接到远程数据库?

android - 功能部署错误 Firebase

javascript - javascript 中收到的响应不是 XML,而在浏览器上它显示的是 XML

java - JAXB自定义命名空间前缀问题