gpt4 book ai didi

java - 适配器类错误。创建时不得附加 ViewHolder View

转载 作者:行者123 更新时间:2023-11-29 23:12:30 25 4
gpt4 key购买 nike

我的代码有问题,我在一个 recyclerview 中创建了两个适配器。我将它们放在一个类中,并且我需要此适配器根据某些情况返回一个 Viewholder。

这是我的适配器代码:

public class CurrentUserTaskListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public List<Task> taskList;
private static final int ITEM_TYPE_PUBLISHED_TASK = 1;
private static final int ITEM_TYPE_ASSIGNED_TASK = 2;

public CurrentUserTaskListAdapter(List<Task> taskList) {
this.taskList = taskList;
}

public class AssignedTaskViewHolder extends RecyclerView.ViewHolder {
View mView;

public TextView tvTagAndDate;
public TextView tvStatus;

public AssignedTaskViewHolder(View itemView) {
super(itemView);
mView = itemView;

tvTagAndDate = itemView.findViewById(R.id.tvTagAndDate);
tvStatus = itemView.findViewById(R.id.tvStatus);
}
}

public class PublishedTaskViewHolder extends RecyclerView.ViewHolder {
View mView;

public TextView tvTagAndDate;
public TextView tvStatus;

public PublishedTaskViewHolder(View itemView) {
super(itemView);
mView = itemView;

tvTagAndDate = itemView.findViewById(R.id.tvTagAndDate);
tvStatus = itemView.findViewById(R.id.tvStatus);
}
}

@Override
public int getItemViewType(int position) {
if (taskList.get(position).getAssignedTo() == null)
return ITEM_TYPE_PUBLISHED_TASK;
else
return ITEM_TYPE_ASSIGNED_TASK;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());

if(i == ITEM_TYPE_ASSIGNED_TASK){
layoutInflater.inflate(R.layout.layout_curent_user_assigned_tasks_item, viewGroup, false);

return new AssignedTaskViewHolder(viewGroup);
}
else if (i == ITEM_TYPE_PUBLISHED_TASK){
layoutInflater.inflate(R.layout.layout_current_user_pending_tasks, viewGroup, false);

return new PublishedTaskViewHolder(viewGroup);
}

return null;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if(viewHolder instanceof AssignedTaskViewHolder){
final AssignedTaskViewHolder assignedTaskViewHolder = (AssignedTaskViewHolder) viewHolder;
}
else if (viewHolder instanceof PublishedTaskViewHolder) {
final PublishedTaskViewHolder publishedTaskViewHolder = (PublishedTaskViewHolder) viewHolder;
}
}

@Override
public int getItemCount() {
return taskList.size();
}}

我遇到的问题是出现此错误:

java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot) at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6796) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)....and so on

在 getItemViewType 方法中,我验证列表中的对象是哪种类型。

最佳答案

修复示例中的这部分:

if (i == ITEM_TYPE_ASSIGNED_TASK) {
View view = layoutInflater.inflate(R.layout.layout_curent_user_assigned_tasks_item, viewGroup, false);

return new AssignedTaskViewHolder(view);
} else if (i == ITEM_TYPE_PUBLISHED_TASK) {
View view = layoutInflater.inflate(R.layout.layout_current_user_pending_tasks, viewGroup, false);

return new PublishedTaskViewHolder(view);
}

问题是您正在将父布局传递给 ViewHolder 的项目布局。祝你好运!

关于java - 适配器类错误。创建时不得附加 ViewHolder View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55830229/

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