我只是使用Firebase创建聊天应用程序并使用XML中的RecyclerView并在Android中使用FirebaseRecyclerAdapter,但问题是当我通过EditText发送消息时,消息会成功发送并显示在我设置的右侧您可以在下面的代码中看到布局,但问题是,当我关闭应用并重新启动消息时,请转到左侧:
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);
}
}
}
答案 0 :(得分:1)
FirebaseUI适配器是针对具有单一布局的同类列表而制作的。虽然具有多个布局的异构列表总是被认为是一个有趣的添加功能(参见this original feature request),但到目前为止尚未添加。
有一个开放功能请求,专门为FirebaseUI库添加多布局支持:https://github.com/firebase/FirebaseUI-Android/issues/305。
您可能希望将投票/ + 1添加到该投票中。
答案 1 :(得分:1)
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;
}
}
&#13;