适配器类出错。创建时不得附加ViewHolder视图

问题描述 投票:1回答:1

我的代码有问题,我在一个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视图。确保您没有在android.support的android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:6796)上传递'true'到LayoutInflater.inflate(...,boolean attachToRoot)的attachToRoot参数。 .v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)....等等

在方法getItemViewType中,我验证列表中的对象是哪个TYPE。

java android android-adapter android-viewholder
1个回答
1
投票

修复此部分,例如:

 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项目布局所在的父布局。祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.