左/右对齐消息

时间:2017-08-28 20:04:51

标签: java android android-layout

我使用的是firebase API。发送和检索消息。但是我在尝试为发件人/检索器设置布局时遇到问题,因此消息将左/右对齐。目前我只有一个发布者/检索者正在使用的布局,但不确定如何设置不同的布局。

public class MessageAdapter extends RecyclerView.Adapter<MessageViewHolder> {

    private List<Message> messagesList;

    private DatabaseReference databaseReference;
    private DatabaseReference userDatabaseRef;
    private   FirebaseAuth firebaseAuth;
    private Context context;
   private String imageUrl;


    public MessageAdapter(List<Message> messagesList) {
        this.messagesList = messagesList;
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Message c = messagesList.get(viewType);
        String sender = c.getFrom();
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(sender);

            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.messages_layout, parent, false);
            return new MessageViewHolder(v);


    }



    @Override
    public void onBindViewHolder(final MessageViewHolder viewHolder, final int index) {


        final Message c = messagesList.get(index);

        final String sender = c.getFrom();

        databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(sender);

        databaseReference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                imageUrl = dataSnapshot.child("image").getValue(String.class);

              viewHolder.setUserimage(context,imageUrl);


                String name = dataSnapshot.child("name").getValue().toString();
                viewHolder.displayName.setText(name);

            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }

        });

        viewHolder.messageText.setText(c.getMessage());
        viewHolder.time.setText(EpochtimeToDateAndTimeString(c.getTime()));
    }

    @Override
    public int getItemCount() {
        return messagesList.size();
    }


    public String EpochtimeToDateAndTimeString(long time) {
        Date date = new Date(time);
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        String formatted = format.format(date);
        return formatted;
    }



}
  

MessageViewHolder

public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public TextView displayName;
public TextView time;
public ImageView showImage;

public MessageViewHolder(View view) {
    super(view);
    messageText = (TextView) view.findViewById(R.id.messagetext);
    displayName = (TextView) view.findViewById(R.id.displayname);
    time = (TextView) view.findViewById(R.id.timestamp);
    showImage = (ImageView) view.findViewById(R.id.imageview_post_userimage3);
}

2 个答案:

答案 0 :(得分:1)

您应该为发件人和收件人的邮件使用不同的视图持有者。

创建两个布局: R.layout.sender_message_layout R.layout.receiver_message_layout

创建两个视图: SenderMessageHolder ReceiverMessageHolder

定义视图类型整数常量:

public static final int VIEW_TYPE_SENDER = 1;
public static final int VIEW_TYPE_RECEIVER = 2;

在您的适配器上实施getItemViewType(int position)

if(messageList.get(position).getFrom().equals(...)) {
    return VIEW_TYPE_SENDER;
} else {
    return VIEW_TYPE_RECEIVER;
}

onCreateViewHolder()功能中检查视图类型并为不同的发件人创建不同的持有者:

View v;

if(viewType == VIEW_TYPE_SENDER) {
    v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.sender_message_layout, parent, false);
    return new SenderMessageHolder(v);
} else {
    v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.receiver_message_layout, parent, false);
    return new ReceiverMessageHolder(v);
}

在onBindViewHolder()中检查holder实例,以便相应地将值绑定到视图:

if(holder instanceof SenderMessageHolder) {
    ((SenderMessageHolder) holder).textView.setText(...);
    ...
} else {
    ((ReceiverMessageHolder) holder).textView.setText(...);
    ...
}

希望它有所帮助!

答案 1 :(得分:0)

有几种方法可以做到这一点。我建议的方法是覆盖适配器的public int getItemViewType(int position)。您必须在此处实现逻辑,以帮助您确定要返回的布局文件并返回视图类型常量。

public static int INCOMING = 1;
public static int OUTGOING = 2;

    @Override
public int getItemViewType(int position) { 
    int viewType = -1;       
    Message m = messagesList.get(position)
    //Logic here determining if m is left or right aligned and  
    // return either 
    // MessageAdapter.OUTGOING OR MessageAdapter.INCOMING
    return viewType;
}

创建左对齐布局文件和右对齐布局文件。您的新onCreateviewHolder()适配器方法将如下所示:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Message c = messagesList.get(viewType);
    String sender = c.getFrom();
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(sender);

    View v;

    if( viewType == MessageAdapter.INCOMING ) {
        v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.messages_layout_left, parent, false);
    }else if ( viewType == MessageAdapter.OUTGOING) {
        v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.messages_layout_right, parent, false);
    }


    return new MessageViewHolder(v);


}