onBindViewHolder上的Recyclerview ClassCastException

时间:2018-08-12 11:19:29

标签: java android

enter image description here

我在RecyclerView中制作了RecyclerView。 onBindViewHolder上有ClassCastException

此处错误消息。

    java.lang.ClassCastException: com.work.hany.playinseoul.main.adapter.MainRecyclerViewAdapter$TourViewHolder$TourSectionItemsAdapter$TourItemViewHolder cannot be cast to com.work.hany.playinseoul.main.adapter.MainRecyclerViewAdapter$CategoryViewHolder$CategoryItemViewHolder
    at com.work.hany.playinseoul.main.adapter.MainRecyclerViewAdapter$CategoryViewHolder$CategoryHorizontalAdapter.onBindViewHolder(MainRecyclerViewAdapter.java:216)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)

但是我没能在ClassCastException TourSectionItemsAdapter抓住onBindViewHolder。 因此,我无法在BindViewHolder的TourSectionItemsAdapter上检查支架类型。

只有我在MainRecyclerViewAdapter onBindViewHolder上捕获了ClassCastException。 但是MainRecyclerViewAdapter的onBindViewHolder持有者类型是相同的铸造hodler类。检查屏幕截图。

enter image description here

我的代码在这里。

    import android.support.annotation.NonNull;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    ...
    import java.util.ArrayList;


    public class MainRecyclerViewAdapter extends BaseSectionRecyclerAdapter {
        private MainRecyclerViewAdapter.ItemListener mainItemListener;
        private RecyclerView.RecycledViewPool recycledViewPool;

        public interface ItemListener {
            void onTourClicked(AreaTour tour);
            void onMoreTourClicked(AreaTour tour);
        }


        public MainRecyclerViewAdapter(ArrayList<Section> sections, MainRecyclerViewAdapter.ItemListener mainItemListener) {
            this.mainItemListener = mainItemListener;
            this.sections = sections;
        }

        @Override
        public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
            this.recycledViewPool = recyclerView.getRecycledViewPool();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ViewHolder viewHolder = null;
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            Section.ItemType currentItemType = getCurrentItemType(viewType);
            View itemView;

            switch (currentItemType) {
                case CATEGORY:
                    itemView = inflater.inflate(R.layout.main_recycler_row_category, parent, false);
                    CategoryViewHolder categoryViewHolder = new CategoryViewHolder(itemView);
                    categoryViewHolder.categoryRecyclerView.setRecycledViewPool(recycledViewPool);
                    viewHolder = categoryViewHolder;
                    break;

                case MAIN_TOUR:
                    itemView = inflater.inflate(R.layout.main_recycler_row_tour, parent, false);
                    TourViewHolder tourViewHolder = new TourViewHolder(itemView);
                    tourViewHolder.tourSectionItemsRecyclerView.setRecycledViewPool(recycledViewPool);
                    viewHolder = tourViewHolder;
                    break;

            }

            return viewHolder;
        }

        private int categoryRecyclerViewScrollPosition = 0;

        @Override
        public void onViewRecycled(@NonNull ViewHolder holder) {
            super.onViewRecycled(holder);
            if (Section.ItemType.CATEGORY.getCode() == holder.getItemViewType()) {
                CategoryViewHolder categoryViewHolder = CategoryViewHolder.class.cast(holder);
                RecyclerView categoryRecyclerView = categoryViewHolder.categoryRecyclerView;
                categoryRecyclerViewScrollPosition = categoryRecyclerView.computeHorizontalScrollOffset();

            }

        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
            super.onBindViewHolder(holder, position);
            try {
                if (Section.ItemType.CATEGORY.getCode() == holder.getItemViewType()) {
                    CategoryViewHolder categoryViewHolder = CategoryViewHolder.class.cast(holder);
                    categoryViewHolder.categoryRecyclerView.scrollBy(categoryRecyclerViewScrollPosition, 0);
                }
            } catch (ClassCastException e) {
                    Log.e("HANY_TAG", "MainRecyclerViewAdapter.onBindViewHolder" + e.getClass().getSimpleName());
                //  But.... holder type is CategoryViewHolder.
                }
            }


        class TourViewHolder extends ViewHolder<ArrayList<AreaTour>> {
            private ImageView tourSectionImageView;
            private TextView tourSectionTitleTextView;
            private RecyclerView tourSectionItemsRecyclerView;


            public TourViewHolder(View itemView) {
                super(itemView);
                tourSectionImageView = itemView.findViewById(R.id.tour_content_image_view);
                tourSectionTitleTextView = itemView.findViewById(R.id.tour_section_title_text_view);
                tourSectionItemsRecyclerView = itemView.findViewById(R.id.tour_section_item_recycler_view);

            }

            @Override
            public void bind(final ArrayList<AreaTour> areaTour) {
                String sectionTitle = ConverterUtils.convertContentType(areaTour.get(0).getContentTypeId());
                tourSectionTitleTextView.setText(sectionTitle);

                ImageLoderUtils.lodeURI(tourSectionImageView, areaTour.get(areaTour.size() - 1).getLargeImage());
                TourSectionItemsAdapter tourSectionItemsAdapter = new TourSectionItemsAdapter(areaTour);
                tourSectionItemsRecyclerView.setLayoutManager(new GridLayoutManager(itemView.getContext(), 2));
                tourSectionItemsRecyclerView.setAdapter(tourSectionItemsAdapter);
            }

            private class TourSectionItemsAdapter extends RecyclerView.Adapter<TourSectionItemsAdapter.TourItemViewHolder> {
                private ArrayList<AreaTour> areaTourList;
                private final int SECTION_IMAGE_COUNT = 1;

                public TourSectionItemsAdapter(ArrayList<AreaTour> areaTourList) {
                    this.areaTourList = areaTourList;
                }

                @NonNull
                @Override
                public TourItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    return new TourItemViewHolder(inflater.inflate(R.layout.main_recycler_row_tour_item, null));
                }

                @Override
                public void onBindViewHolder(@NonNull TourItemViewHolder holder, int position) {
                    holder.bind(areaTourList.get(position));

                }

                @Override
                public int getItemCount() {
                    return areaTourList.size() - SECTION_IMAGE_COUNT;
                }

                class TourItemViewHolder extends ViewHolder<AreaTour> {
                    private ImageView tourImageView;
                    private TextView tourTextView;
                    private TextView tourAddrTextView;
                    private TextView tourContentShowCountTextView;

                    public TourItemViewHolder(View itemView) {
                        super(itemView);
                        tourImageView = itemView.findViewById(R.id.tour_content_image_view);
                        tourTextView = itemView.findViewById(R.id.tour_title_text_view);
                        tourContentShowCountTextView = itemView.findViewById(R.id.tour_show_text_view);
                        tourAddrTextView = itemView.findViewById(R.id.tour_content_addr_text_view);

                    }

                    public void bind(final AreaTour tour) {
                        ImageLoderUtils.lodeURI(tourImageView, tour.getLargeImage());
                        tourTextView.setText(tour.getContentTitle());

                        String countStr = new StringBuilder().append("조회수 ").append(tour.getReadCount()).toString();
                        tourContentShowCountTextView.setText(countStr);
                        tourAddrTextView.setText(tour.getAreaAddress());

                        itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mainItemListener.onTourClicked(tour);
                            }
                        });

                    }
                }

            }
        }


        class CategoryViewHolder extends ViewHolder<ArrayList<ContentType>> {
            private RecyclerView categoryRecyclerView;
    //        private TextView categoryTitleView;

            public CategoryViewHolder(View itemView) {
                super(itemView);
    //            categoryTitleView = itemView.findViewById(R.id.main_tour_title_text_view);
                categoryRecyclerView = itemView.findViewById(R.id.main_tour_recycler_view);
                categoryRecyclerView.setHasFixedSize(true);
            }

            @Override
            public void bind(ArrayList<ContentType> data) {
                CategoryHorizontalAdapter categoryHorizontalAdapter = new CategoryHorizontalAdapter(data);
                LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false);
                categoryRecyclerView.setLayoutManager(layoutManager);
                categoryRecyclerView.setAdapter(categoryHorizontalAdapter);

            }

            private class CategoryHorizontalAdapter extends RecyclerView.Adapter<CategoryItemViewHolder> {
                private ArrayList<ContentType> categoryTypes;


                public CategoryHorizontalAdapter(ArrayList<ContentType> categoryTypes) {
                    this.categoryTypes = categoryTypes;
                }

                @NonNull
                @Override
                public CategoryItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    return new CategoryItemViewHolder(inflater.inflate(R.layout.main_recycler_row_category_item, null));
                }

                @Override
                public void onBindViewHolder(@NonNull CategoryItemViewHolder holder, int position) {
                    holder.bind(categoryTypes.get(position));
                }


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


            private class CategoryItemViewHolder extends ViewHolder<ContentType> {
                private TextView categoryTitleTextView;


                public CategoryItemViewHolder(View itemView) {
                    super(itemView);
                    categoryTitleTextView = itemView.findViewById(R.id.category_item_title_text_view);
                }

                public void bind(ContentType type) {
                    categoryTitleTextView.setText(type.getName());
                }
            }
        }
    }

BaseSectionRecyclerAdapter代码。

abstract public class BaseSectionRecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {
    protected ArrayList<Section> sections;

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

    public <T>void addSection(Section.ItemType type, T data){
        sections.add(new Section(type,data));
        notifyDataSetChanged();
    }

    public <T>void updateSection(Section.ItemType type, T data) {
        for(int position = 0, end = sections.size(); position < end; position++ ){
            if (sections.get(position).getType().equals(type)) {
                sections.get(position).setData(data);
                notifyItemChanged(position);
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind((sections.get(position).getData()));
    }

    @Override
    public int getItemViewType(int position) {
        return sections.get(position).getType().getCode();
    }

    protected Section.ItemType getCurrentItemType(int position) {
        for (Section.ItemType itemType : Section.ItemType.values()) {
            if (itemType.getCode() == position) return itemType;
        }

        return NOTHING;
    }

}

ViewHolder代码。

public abstract class ViewHolder<T> extends  RecyclerView.ViewHolder {
    public ViewHolder(View itemView) {
        super(itemView);
    }
    abstract public void bind(T data);

}

我不明白我做错了什么... 如何解决?

2 个答案:

答案 0 :(得分:0)

我刚刚遇到了上面提到的问题。我在switch函数中使用了onBindViewHolder大小写,但是我没有写default大小写。当我添加default大小写时,该错误已得到解决。

在您的情况下,您正在if中使用onBindViewHolder()条件,只需尝试向函数中添加else并尝试。

希望您解决了该问题,然后尝试执行此操作。谢谢。

答案 1 :(得分:0)

也许为时已晚,但对于功能读者来说,只需像这样定义 viewPool

private val viewPool = RecyclerView.RecycledViewPool()