java.lang.ClassCastException android

java.lang.ClassCastException android

我需要在我的 android 应用程序中使用部分执行两个 recyclerView。第一个运行良好,但第二个出现这样的错误

 java.lang.ClassCastException: com.example.battlefield.WaysRecyclerViewAdapter.WaysItemViewHolder cannot be cast to com.example.battlefield.WaysRecyclerViewAdapter.WaysHeaderViewHolder
                                                                         at com.example.battlefield.WaysRecyclerViewAdapter.WaysRecyclerViewAdapter.onBindHeaderViewHolder(WaysRecyclerViewAdapter.java:45)

我为 sectionHeaders 和 recyclerView 的项目分离了 viewHolders。 还有我的类 WaysItemViewHolder:

public class WaysItemViewHolder extends RecyclerView.ViewHolder{
    public TextView waysName;
    public TextView waysInfo;
    public TextView waysTags;
    public Button addWay;
    public WaysItemViewHolder(View itemView) {
        super(itemView);
        waysName = (TextView)itemView.findViewById(R.id.cv_ways_name);
        waysInfo = (TextView)itemView.findViewById(R.id.cv_ways_info);
        waysTags = (TextView)itemView.findViewById(R.id.cv_ways_tags);
        addWay = (Button) itemView.findViewById(R.id.cv_ways_add_way);
}}

WaysHeaderViewHolder:

public class WaysHeaderViewHolder extends RecyclerView.ViewHolder{
    public TextView headerTitle;
    public WaysHeaderViewHolder(View itemView) {
        super(itemView);
        headerTitle = (TextView)itemView.findViewById(R.id.cv_ways_header);
    }
}

和recyclerViewAdapter:

    public class WaysRecyclerViewAdapter extends StatelessSection {
        private static final String TAG = WaysRecyclerViewAdapter.class.getSimpleName();
        private String title;
        private String[] waysList;
        private String[] infoList;
    private String[] tagsList;
    public WaysRecyclerViewAdapter(String title, String[] waysList, String[] tagsList, String[] infoList) {
        super(R.layout.cardview_way_header, R.layout.cardview_way_item);
        this.title = title;
        this.waysList = waysList;
        this.tagsList = tagsList;
        this.infoList = infoList;
    }
    @Override
    public int getContentItemsTotal() {
        return waysList.length;
    }
    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new WaysItemViewHolder(view);
    }
    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        WaysItemViewHolder iHolder = (WaysItemViewHolder)holder;
        iHolder.waysName.setText(waysList[position]);
        iHolder.waysInfo.setText(infoList[position]);
        iHolder.waysTags.setText(tagsList[position]);
    }
    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new WaysItemViewHolder(view);
    }
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        WaysHeaderViewHolder hHolder = (WaysHeaderViewHolder)holder;
        hHolder.headerTitle.setText(title);
    }
}

为了在 recyclerView 中制作部分,我使用了这个库 https://github.com/luizgrp/SectionedRecyclerViewAdapter

有人知道怎么解决吗?

@Override public RecyclerView.ViewHolder getHeaderViewHolder(查看视图) { return 新的 WaysItemViewHolder(视图); }

您需要将 WaysItemViewHolder 替换为 WaysHeaderViewHolder