gpt4 book ai didi

Android - RecyclerView 如何处理具有多个绑定(bind)的 ViewHolder

转载 作者:行者123 更新时间:2023-12-04 03:53:01 24 4
gpt4 key购买 nike

这是我的 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;
}
}
这是我的 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;
}

}
我希望能够获得适当的绑定(bind),以便能够对内部的 View 进行更改。

最佳答案

当你有 2 个不同的 recyclerview 布局时,你必须有 2 个不同的 View 。
首先为您的适配器添加 2 个不同的 viewHolder

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

}
在使用此方法为适配器设置项目类型之后
@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;
}
}
在您的 onCreateViewHolder像这样使用 2 viewHolder:
@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);
}
}
在您的 onBindViewHolder 中,您可以调用 eash viewHolder 方法:
@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);
}
}

关于Android - RecyclerView 如何处理具有多个绑定(bind)的 ViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64164201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com