本文介绍了如何在FirebaseRecyclerAdapter中设置多个布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是使用Firebase,在XML中使用RecyclerView和在Android中使用FirebaseRecyclerAdapter来创建聊天应用程序,但是问题是,当我通过EditText发送消息时,消息已成功发送并显示在我设置的右侧您可以在下面的代码中看到布局,但是问题是当我关闭应用程序并重新启动消息时,请转到左侧:
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);
}
}
}
推荐答案
public Class MyAdapter extends FirebaseRecyclerAdapter<ModelClass, ViewHolderClass> {
private static final int LAYOUT_ONE = 1;
public MyAdapter(Class<ModelClass> modelClass,
int modelLayout,
Class<ViewHolderClass> viewHolderClass,
Query ref) {
super(modelClass, modelLayout, viewHolderClass, ref);
}
@Override
public ViewHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == LAYOUT_ONE) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_one, parent, false);
return new ViewHoldeClassr(view);
}
else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_two, parent, false);
return new ViewHolderClass(view);
}
}
@Override
public int getItemViewType(int position)
ModelClass model = getItem(position);
if (model.caseWhenYouWentToUseLayoutOne)) {
return LAYOUT_ONE;
}
return position;
}
}
这篇关于如何在FirebaseRecyclerAdapter中设置多个布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!