我正在使用Firebase作为后端制作聊天应用程序。
目前,我在设置用于在RecyclerView中发送和接收消息的项目布局时遇到问题。
当我对发送和接收消息使用相同的项目布局时,它的工作原理非常完美。
但是我想将屏幕分成两部分,左面用于发送消息,右面用于接收消息。
当制作FirebaseRecyclerAdapter对象时,我只能传递一种布局作为其参数。我真的在这个问题上挣扎。希望有人能帮助我。

private void loadMessages() {

    mAdapter = new FirebaseRecyclerAdapter<MessageToSend, NewMessageViewHolder>(
            MessageToSend.class,
            R.layout.single_message_new,
            NewMessageViewHolder.class,
            messageRef.child(sender).child(receiver)
    ) {
        @TargetApi(Build.VERSION_CODES.M)
        @Override
        protected void populateViewHolder(NewMessageViewHolder viewHolder, MessageToSend model, int position) {
            if (sender.equals(model.getSender())) {

                viewHolder.outMessage.setTextColor(getResources().getColor(R.color.darkGreen));
            }
            else {
                viewHolder.outMessage.setTextColor(Color.BLACK);
                //viewHolder.outMessage.setGravity(Gravity.RIGHT);
            }
            viewHolder.outMessage.setText(model.getMessage());
            viewHolder.sender.setText(model.getSender());
        }
    };

    mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int friendlyMessageCount = mAdapter.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 >= (friendlyMessageCount - 1) && lastVisiblePosition == (positionStart - 1))) {
                recyclerView.scrollToPosition(positionStart);
            }
        }
    });
    recyclerView.setLayoutManager(mLinearlayoutManager);
    recyclerView.setAdapter(mAdapter);
}


ViewHolder类别

public static class InMessageViewHolder extends RecyclerView.ViewHolder{
    TextView inMessage;
    TextView receiver;
    CircleImageView receiverImage;

    public InMessageViewHolder(View itemView) {
        super(itemView);
        inMessage = (TextView) itemView.findViewById(R.id.textView11);
        receiver = (TextView) itemView.findViewById(R.id.textView12);
    }
}

最佳答案

您可以覆盖getItemViewTypeFirebaseRecyclerAdapter以实现此目的:

 // Check if the item at position is sender
 private boolean isSender(int position) {
   sender.equals(getItem(position).getSender());
 }

 @Override
 public int getItemViewType(int position) {
     if (isSender(position)) {
         return R.layout.my_sender_layout;
     } else {
         return R.layout.my_receiver_layout;
     }
 }


这将导致RecyclerAdapter有条件地扩大布局。但是,您仍然只能有一个ViewHolder子类,因此您需要确保在实现public MyViewHolder(View itemView)时,您的代码已准备好使itemView成为两种布局之一,并且如果这些布局具有不同的内容,则有些调用将返回null。

关于android - 如何在聊天应用程序中将FirebaseRecyclerAdapter用于两个项目布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39078698/

10-15 14:25