Layout Chat -Invert the side of my message

0

So I'm starting now to do the chat activity I do not have any code yet, but thinking of the process came a doubt ... Almost the way I'm going to do All sent messages will be on one side, what I want is for it stays the same as whatsapp or many other message apk there, All received messages are on the left side and my message is on the right side.

* I'm using firebase.

    
asked by anonymous 05.02.2017 / 23:59

1 answer

2

Well, I did as follows:
Using a RecyclerView, I made two ViewHolders with their respective layouts in xml for the messages (for example, ViewHolderMessageRight and ViewHolderMessageLeft). And at the time of linking the data (in the onBindViewHolder), you should find some way to identify which message is yours and what message is from other people (you can compare IDs, etc) p>

public class RecyclerViewChat extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Mensagem> mensagemList;
    private Context context;
    private Usuario usuario;

    private static final int OWN_MESSAGE = 0;
    private static final int OTHERS_MESSAGE = 1;

    public RecyclerViewChat(List<Mensagem> mensagemList, Context context, Usuario usuario) {
        this.mensagemList = mensagemList;
        this.context = context;
        this.usuario = usuario;
    }

    @Override
    public int getItemViewType(int position) {
        // Comparação para saber se é a msg do user ou dos outros...No caso eu usei o nome dele, mas pode usar o que quiser
        if(mensagemList.get(position).getName().equals(usuario.getName())){
            return OWN_MESSAGE;
        } else {
            return OTHERS_MESSAGE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==OWN_MESSAGE){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_chat_right, parent, false);
            return new RightChatViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_chat_left, parent, false);
            return new LeftChatViewHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        // left = dos outros
        if(holder instanceof LeftChatViewHolder){

            ((LeftChatViewHolder)holder).msg = mensagemList.get(position);
            ((LeftChatViewHolder)holder).userName.setText(mensagemList.get(position).getName());
            ((LeftChatViewHolder)holder).message.setText(mensagemList.get(position).getMessage());

            // right = sua msg
        } else if(holder instanceof RightChatViewHolder){
            ((RightChatViewHolder)holder).msg = mensagemList.get(position);
            ((RightChatViewHolder)holder).userName.setText(mensagemList.get(position).getName());
            ((RightChatViewHolder)holder).message.setText(mensagemList.get(position).getMessage());
        }

    }

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

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public static class LeftChatViewHolder extends RecyclerView.ViewHolder{

        private TextView userName;
        private TextView message;

        private Mensagem msg;

        public LeftChatViewHolder(View itemView) {
            super(itemView);

            userName = (TextView) itemView.findViewById(R.id.chat_adapter_left_username);
            message = (TextView) itemView.findViewById(R.id.chat_adapter_left_mensagem);

        }
    }

    public static class RightChatViewHolder extends RecyclerView.ViewHolder{

        private TextView userName;
        private TextView message;

        private Mensagem msg;

        public RightChatViewHolder(View itemView) {
            super(itemView);

            userName = (TextView) itemView.findViewById(R.id.chat_adapter_right_username);
            message = (TextView) itemView.findViewById(R.id.chat_adapter_right_mensagem);

        }
    }
    // add um novo objeto mensagem...
    public void setNewMessage(Mensagem message){
        if(mensagemList==null){
            mensagemList = new ArrayList<>();
        }
        this.mensagemList.add(message);
        this.notifyDataSetChanged();
    }
     // pega a posição da ultima mensagem
     public Integer lastPos(){
        return mensagemList.size()-1;
    }

}

Do not forget to use recyclerView.scrollToPosition(adapter.lastPos()); in the activity whenever you add a new message where the lastPos method finds the last position of the message list (and the recyclerView is the object of type RecyclerView and the adapter is an object of the type RecyclerViewChat I passed you) and finally gives an automatic scroll to it ... It's to make things easier.

    
06.02.2017 / 12:01