Android - RecyclerView how to handle ViewHolder with multiple bindings

1.8k views Asked by At

Here is my onBindViewHolder:

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        MessageModel message = values.get(position);
        switch (holder.getItemViewType()) {
            case VIEW_TYPE_VIDEO_SENT:
                ((VideoMessageHolder) holder).sentBinding.setMessageModel(message);
                break;
            case VIEW_TYPE_VIDEO_RECEIVED:
                ((VideoMessageHolder) holder).receivedBinding.setMessageModel(message);
                break;
        }
    }

Here is my ViewHolder:



    private class VideoMessageHolder extends RecyclerView.ViewHolder {
        private RowRecyclerViewChatVideoMessageSentBinding sentBinding;
        private RowRecyclerViewChatVideoMessageReceivedBinding receivedBinding;

        VideoMessageHolder(RowRecyclerViewChatVideoMessageSentBinding binding) {
            super(binding.getRoot());
            this.sentBinding = binding;
        }

        VideoMessageHolder(RowRecyclerViewChatVideoMessageReceivedBinding binding) {
            super(binding.getRoot());
            this.receivedBinding = binding;
        }

    }

I want to be able to get appropriate binding to be able to make changes to the views inside.

1

There are 1 answers

1
Keyur Nimavat On

When you have 2 different layout for recyclerview than you must have 2 different viewholder for it.

First add 2 different viewHolder for your adapter

private static int TYPE_SENT = 1;
private static int TYPE_RECEIVED = 2;

private class VideoSentHolder extends RecyclerView.ViewHolder {
    private RowRecyclerViewChatVideoMessageSentBinding sentBinding;

    VideoSentHolder(RowRecyclerViewChatVideoMessageSentBinding binding) {
        super(binding.getRoot());
        this.sentBinding = binding;
    }
}

private class VideoReceivedHolder extends RecyclerView.ViewHolder {
    private RowRecyclerViewChatVideoMessageReceivedBinding receivedBinding;

    VideoReceivedHolder(RowRecyclerViewChatVideoMessageReceivedBinding binding) {
        super(binding.getRoot());
        this.receivedBinding = binding;
    }

}

after that set item type for adapter with this method

@Override
public int getItemViewType(int position) {
    //Here you have to check condition for sent or received type
    if (TextUtils.isEmpty(list.get(position).isSent())) {
        return TYPE_SENT;

    } else {
        return TYPE_RECEIVED;
    }
}

in your onCreateViewHolder use 2 viewHolder like this:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_SENT) { // for Sent layout
        RowRecyclerViewChatVideoMessageSentBinding sentBinding;//Your sent binding
        return new VideoSentHolder(sentBinding);

    } else { // for received layout
        RowRecyclerViewChatVideoMessageReceivedBinding receivedBinding;//=your received binding
        return new VideoReceivedHolder(receivedBinding);
    }
}

and inside your onBindViewHolder you can call eash viewHolder method:

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
    MessageModel message = values.get(position);
    if (getItemViewType(position) == TYPE_SENT) {
        ((VideoSentHolder) viewHolder).sentBinding.setMessageModel(message);
    } else {
        ((VideoReceivedHolder) viewHolder).receivedBinding.setMessageModel(message);
    }
}