CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.31k stars 5.16k forks source link

BaseBinderAdapter 第一次打开fragment没事,第二次进入后报错,ClassCastException: xxx.bean.ImageUrlBean cannot be cast to xxx.bean.FictionBean #3312

Open RavenGluttonous opened 4 years ago

RavenGluttonous commented 4 years ago

private BaseBinderAdapter adapter = new BaseBinderAdapter();

adapter.addItemBinder(ImageUrlBean.class, new ImageItemBinder()) .addItemBinder(VideoBean.class, new VideoItemBinder()) .addItemBinder(FictionBean.class, new ContentItemBinder());

List data = new ArrayList<>(); data.add(new ImageUrlBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=119713024,2807881900&fm=26&gp=0.jpg")); data.add(new VideoBean("捌佰", "是一部抗日战争神剧来的" + random.nextInt(5) + 10)); data.add(new FictionBean("星辰变", "辰东写的小说")); adapter.setList(data);

异常:ClassCastException: xxx.bean.ImageUrlBean cannot be cast to xxx.bean.FictionBean

kxbyte commented 4 years ago

遇到一样的问题了

LJYq commented 3 years ago

遇到一样的问题了

xiexinli commented 2 years ago

原因:同一个BaseBinderAdapter()对象,两个以上的binder重复添加(第一张图片),会导致第二第三种图片所展示的结果,然后在数据绑定的时候,根据itemType就会拿到错误的viewhodler,convert中,根据布局转化bean类,就会发生强转错误。 image

UunwyTxy4f W7xMgrXNQM

修改方式:保证添加的binder不重复。或者重置BaseBinderAdapter()对象,然后再添加binder。

DearZack commented 10 months ago

是否可以增加获取是否bind过了某个binder的方法?更代码找到了findViewType,但是是protected的,只能继承后再想办法暴露出来