How can I use FirebaseRecyclerAdapter for two Item layouts in a Chat Application

亡梦爱人 提交于 2019-12-13 00:11:40

问题


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

回答1:


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

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.




回答2:


You read https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView#prerequisite

Also In order to use few code of FirebaseRecyclerAdapter you can extends and provide the functinality for view only. Other will taken care from super class such as Firebase Query.

Please take care of model layout which can be -1 as you are overriding onCreateViewHolder



来源:https://stackoverflow.com/questions/39078698/how-can-i-use-firebaserecycleradapter-for-two-item-layouts-in-a-chat-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!