RecyclerView开发中遇到的问题
1.上下滑动时数据显示错乱
分析:
由于RecyclerView的刷新机制,在上下滑动时,会复用之前的item,在某一些item需要展示数据,一些不需要等情况会导致数据错乱
解决:
把不需要显示的地方重置,如隐藏或者置空
2.在RecyclerView嵌套RecyclerView的时候,里面RecyclerView如果使用的是SwipeMenuRecyclerView(带侧滑)在上下滑动的时候会报错
java.lang.IllegalStateException: Cannot set menu creator, setAdapter has already been called.
分析:
监听的方法不能在setadpter之前调用
解决:
在调用之前重置适配器,置为null
3.RecyclerView 设置新的数据源不生效
分析:
更新数据源不生效,那一定是数据源的内存地址变了,数据源一定不能重新设置值,只能更新修改
解决:
保证内存地址不变,notify才能生效
4.在RecyclerView使用ViewPager遇到的问题
1.RecyclerView滚动上去,直至ViewPager看不见,再滚动下来,ViewPager下一次切换没有动画
分析:
Viewpager里有一个私有变量mFirstLayout,它表示是不是第一次显示布局,如果是true,则使用无动画的方式显示当前item,如果是fals,则使用动画方式显示当前item
if (mFirstLayout) {// We don't have any idea how big we are yet and shouldn't have any pages either.// Just set things up and let the pending layout handle things.mCurItem = item;if (dispatchSelected) {dispatchOnPageSelected(item);}requestLayout();} else {populate(item);scrollToItem(item, smoothScroll, velocity, dispatchSelected);}
当Viewpager滚动上去后,因为RecyclerView的回收机制,Viewpager会走OnDetachFromWindow,当再次滚动下来时,Viewpager会走OnAttachToWindow,而问题就出现在onAttachToWindow。
@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();mFirstLayout = true;}
解决:
重写onAttachToWindow方法,利用反射将mFirstLayout置为false
@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();if (isLayout && getAdapter() != null && getAdapter().getCount() > 0) {try {Field mScroller = ViewPager.class.getDeclaredField("mFirstLayout");mScroller.setAccessible(true);mScroller.set(this, false);} catch (Exception e) {e.printStackTrace();}}
2.当ViewPager滚动到一半时,RecyclerView滚动上去,再滚动下来,ViewPager会卡在一半
分析:
问题出在ViewPager源码的onDetachedFromWindow中
@Overrideprotected void onDetachedFromWindow() {removeCallbacks(mEndScrollRunnable);// To be on the safe side, abort the scrollerif ((mScroller != null) && !mScroller.isFinished()) {mScroller.abortAnimation();}super.onDetachedFromWindow();}
解决:
在自定义ViewPager中重写onDetachedFromWindow方法
@Overrideprotected void onDetachedFromWindow() {//fix 使用RecyclerView + ViewPager bugif (((Activity) getContext()).isFinishing()) {super.onDetachedFromWindow();}}