如何在发送邮件时将列表项设置为右侧?

时间:2017-04-28 18:22:32

标签: android xml android-layout android-arrayadapter

问题是列表中的每个项目都与左对齐。但是,我想区分发送的消息和传入的消息。

我有两个布局文件:

我的MessageAdapter课程中有if声明:

 if (convertView == null) {
    convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_right_admin, parent, 
 }

这是我的应用的截图:

enter image description here

  

我在Stack溢出时看到一个关于同一问题的post。我发现了一个线索,但我是   无法实现它。谁能建议我这样做?以下是线索。   因此,每当用户按下发送按钮时,他的消息应该在右边。传入的消息应该在左侧。

if (convertView == null) {
        if (list.get(position).getTypeOfSms().equals("send"))
            convertView     = myInflater.inflate(R.layout.raw_left, null);
        else
            convertView     = myInflater.inflate(R.layout.raw_right, null);

当用户点击发送按钮时,为了使发送的消息与右边对齐,应该怎么做?

1 个答案:

答案 0 :(得分:2)

像这样更改你的适配器代码,

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class MessageAdapter extends ArrayAdapter<Message> {


    private static final int ROW_TYPE_LEFT = 0;
    private static final int ROW_TYPE_RIGHT = 1;

    private List<Message> messagesList;

    public MessageAdapter(Context context, int resource, List<Message> objects) {
        super(context, resource, objects);
        this.messagesList = objects;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int pos) {
        return messagesList.get(pos).isDelivered() ? ROW_TYPE_LEFT : ROW_TYPE_RIGHT ;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          // Gets the message that we are displaying at a position
        Message message = getItem(position);

        Log.d("TAG", "message: " + message);

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            int rowType = getItemViewType(position);
            switch (rowType)
            {
                case ROW_TYPE_RIGHT:
                    convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_right_admin, parent, false);
                    break;
                case ROW_TYPE_LEFT:
                    convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_left_user, parent, false);
                    default:
            }

        }

        // Lookup view for data population
        ImageView photoImageView = (ImageView) convertView.findViewById(R.id.photoImageView);
        TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
        TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);

        boolean isPhoto = message.getPhotoUrl() != null;

        if (isPhoto) {
            messageTextView.setVisibility(View.GONE);
            photoImageView.setVisibility(View.VISIBLE);
            Glide.with(photoImageView.getContext())
                    .load(message.getPhotoUrl())
                    .into(photoImageView);
        } else {
            messageTextView.setVisibility(View.VISIBLE);
            photoImageView.setVisibility(View.GONE);
            messageTextView.setText(message.getText());
        }

        authorTextView.setText(message.getName());

        // Return the completed view to render on screen
        return convertView;
    }

}

并在isDelivered模型中添加Message方法。