0

我需要在我的 android 应用程序中对部分进行两个 recyclerViews。第一个效果很好,但第二个显示这样的错误

 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 的 viewHolders 和 recyclerView 的项目。有我的类 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

有人知道如何解决吗?

4

1 回答 1

0

@Override public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new WaysItemViewHolder(view); }

您需要将WaysItemViewHolder替换为WaysHeaderViewHolder

于 2017-05-21T15:37:52.200 回答