在RecyclerView中使用ViewPager时出现的两个Bug

402 阅读1分钟

在RecyclerView中使用ViewPager时出现的两个Bug解决

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。
   @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mFirstLayout = true;
    }

解决方案

  • 重写onAttachToWindow方法,利用反射将mFirstLayout置为False
    @Override
    protected 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中

   @Override
    protected void onDetachedFromWindow() {
        removeCallbacks(mEndScrollRunnable);
        // To be on the safe side, abort the scroller
        if ((mScroller != null) && !mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        super.onDetachedFromWindow();
    }

解决方法

  • 在自定义ViewPager中重写onDetachedFromWindow方法
    @Override
    protected void onDetachedFromWindow() {
        //fix 使用RecyclerView + ViewPager bug
        if (((Activity) getContext()).isFinishing()) {
            super.onDetachedFromWindow();
        }