我的代码有问题,我在一个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视图。确保您没有将'true'传递给LayoutInflater.inflate(...,boolean attachToRoot)的attachToRoot参数 在android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:6796) 在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975) 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)....等等
在方法getItemViewType中,我验证列表中的对象是哪种类型。
答案 0 :(得分:0)
在示例中修复此部分:
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的项目布局应该在该父布局中。 祝你好运!