聊天应用程序中将FirebaseRecyclerAdapter用

聊天应用程序中将FirebaseRecyclerAdapter用

本文介绍了如何在聊天应用程序中将FirebaseRecyclerAdapter用于两个项目布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a Chat Application using firebase as its back end.Currently I am facing a problem to set Item layouts for sending and receiving messages in the RecyclerView.When I used the same Item layout for both Sending and Receiving messages it worked perfectly.But I want to split the screen into two parts left hand side for sending messages and right hand side for receiving messages.When making FirebaseRecyclerAdapter object I can pass only one layout as its parameters. I am really struggling with this problem.Hope some one helps me.

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类

ViewHolder Class

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);
    }
}

推荐答案

您可以覆盖FirebaseRecyclerAdaptergetItemViewType来实现此目的:

You can override the getItemViewType of the FirebaseRecyclerAdapter to achieve this:

 // 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.

This will cause the RecyclerAdapter to inflate the layouts conditionally. However you can still only have one ViewHolder subclass so you need to make sure that when you implement public MyViewHolder(View itemView) your code is prepared for itemView to be one of two layouts, and if those layouts have different contents then some findViewById calls will return null.

这篇关于如何在聊天应用程序中将FirebaseRecyclerAdapter用于两个项目布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:14