RecyclerView 问题记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w_xue/article/details/82379832

1.调用更新数据后发现界面不展示,从layout inspector看到recyclerview中无子控件,检查发现没有设置LayoutManager。更离谱的是检查代码时还比对https://github.com/drakeet/MultiType 的sample code来对比,估计drakeet是在xml中设置了layoutmanager

LinearLayoutManager llm = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(llm);

2.item的顶层View的宽度异常,具体现象为列表首次展示时item都是wrap_content的,上下滚动recyclerview后发现item又变成我们设定的match_parent。 此处检查发现item在inflate的时候是有传入parent的,理论上不应该会出现item的顶层view layoutparams丢失的问题。 后来在stackoverflow找到答案,需要关闭LinearLayoutManager的automeasure

LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setAutoMeasureEnabled(false);
mRecyclerView.setLayoutManager(llm);

3.点击列表中CheckBox需要更新列表然后刷新,执行报错

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

解决办法为在下一次消息处理中执行列表刷新

mMainHandler.post(() -> refreshList());

猜你喜欢

转载自blog.csdn.net/w_xue/article/details/82379832