I just make a chat application with Firebase and using RecyclerView in XML and using FirebaseRecyclerAdapter in Android, but the problem is that when I send a message through EditText, the message is sent successfully and show to the right side that I set with Layout as you can see in code below but the problem is that when I close app and restart the message go to left side:
mFirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MessageModel, MessageViewHolder>(
MessageModel.class,
R.layout.layout_message_sender,
MessageViewHolder.class,
mFirebaserefrence_messages) {
@Override
public int getItemViewType(int position) {
MessageModel model = getItem(position);
if (model.getUserId() == mUserId) {
return R.layout.layout_message_sender;
}
else {
return R.layout.layout_message_recipitent;
}
}
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, MessageModel model, int position) {
viewHolder.mTextView_userMessage.setText(model.getUserMessage());
if (model.getUserName() == null) {
viewHolder.mTextView_userName.setText("Anonymous");
} else {
viewHolder.mTextView_userName.setText(model.getUserName());
}
if (model.getUserImageUrl() == null) {
viewHolder.mCircleImageView_userImage
.setImageDrawable(ContextCompat.getDrawable(ChatActivity.this,
R.drawable.ic_account_circle_red_48dp));
} else {
Glide.with(ChatActivity.this)
.load(model.getUserImageUrl())
.into(viewHolder.mCircleImageView_userImage);
}
}
};
mFirebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int messageCount = mFirebaseRecyclerAdapter.getItemCount();
int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the user is at the bottom of the list, scroll
// to the bottom of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (messageCount - 1) && lastVisiblePosition == (positionStart - 1))) {
mRecyclerView.scrollToPosition(positionStart);
}
}
});
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mFirebaseRecyclerAdapter);
mEditText_message = (EditText) findViewById(R.id.editText_message);
mEditText_message.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().trim().length() > 0) {
mImageButton.setEnabled(true);
} else {
mImageButton.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
}
);
mImageButton = (ImageButton) findViewById(R.id.imageButton_send);
mImageButton.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mFirebaseAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mFirebaseAuthListener != null) {
mFirebaseAuth.removeAuthStateListener(mFirebaseAuthListener);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_signOut:
mFirebaseAuth.signOut();
startActivity(new Intent(ChatActivity.this, SignInActivity.class));
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onClick(View v) {
if (v == mImageButton) {
MessageModel messageModel = new MessageModel(mUserId,
mUserImageUrl,
mEditText_message.getText().toString(),
mUsername,
null);
mFirebaserefrence_messages.push().setValue(messageModel);
mEditText_message.setText(null);
}
}
}
The FirebaseUI adapters were made for a homogenous lists with a single layout. While having heterogenous list with multiple layouts were always considered an interesting feature to add (see this original feature request), it hasn't been added so far.
There is an open feature request for specifically adding multi-layout support on the FirebaseUI library: https://github.com/firebase/FirebaseUI-Android/issues/305.
You might want to add your vote/+1 to that.