@Test public void setPrimaryItemNotCalledWhenAdapterIsEmpty() { ViewPager pager = activityRule.getActivity().pager; final PrimaryItemPagerAdapter adapter = new PrimaryItemPagerAdapter(); pager.setAdapter(adapter); InstrumentationRegistry.getInstrumentation().waitForIdleSync(); assertFalse(adapter.primaryCalled); InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { adapter.count = 1; adapter.notifyDataSetChanged(); } }); InstrumentationRegistry.getInstrumentation().waitForIdleSync(); assertTrue(adapter.primaryCalled); }
public void setAdapter(@Nullable PagerAdapter adapter) { if (mAdapter != null) { mAdapter.setViewPagerObserver(null); mAdapter.startUpdate(this); for (int i = 0; i < mItems.size(); i++) { final ItemInfo ii = mItems.get(i); mAdapter.destroyItem(this, ii.position, ii.object); } mAdapter.finishUpdate(this); mItems.clear(); removeNonDecorViews(); mCurItem = 0; scrollTo(0, 0); } final PagerAdapter oldAdapter = mAdapter; mAdapter = adapter; mExpectedAdapterCount = 0; if (mAdapter != null) { if (mObserver == null) { mObserver = new PagerObserver(); } mAdapter.setViewPagerObserver(mObserver); mPopulatePending = false; final boolean wasFirstLayout = mFirstLayout; mFirstLayout = true; mExpectedAdapterCount = mAdapter.getCount(); if (mRestoredCurItem >= 0) { mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader); setCurrentItemInternal(mRestoredCurItem, false, true); mRestoredCurItem = -1; mRestoredAdapterState = null; mRestoredClassLoader = null; } else if (!wasFirstLayout) { populate(); } else { requestLayout(); } } if (mAdapterChangeListeners != null && !mAdapterChangeListeners.isEmpty()) { for (int i = 0, count = mAdapterChangeListeners.size(); i < count; i++) { mAdapterChangeListeners.get(i).onAdapterChanged(this, oldAdapter, adapter); } } }
ViewPager extends ViewGroup { public void setAdapter(@Nullable PagerAdapter adapter) { if (mAdapter != null) { mAdapter.setViewPagerObserver(null); mAdapter.startUpdate(this); for (int i = 0; i < mItems.size(); i++) { final ItemInfo ii = mItems.get(i); mAdapter.destroyItem(this, ii.position, ii.object); } mAdapter.finishUpdate(this); mItems.clear(); removeNonDecorViews(); mCurItem = 0; scrollTo(0, 0); } final PagerAdapter oldAdapter = mAdapter; mAdapter = adapter; mExpectedAdapterCount = 0; if (mAdapter != null) { if (mObserver == null) { mObserver = new PagerObserver(); } mAdapter.setViewPagerObserver(mObserver); mPopulatePending = false; final boolean wasFirstLayout = mFirstLayout; mFirstLayout = true; mExpectedAdapterCount = mAdapter.getCount(); if (mRestoredCurItem >= 0) { mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader); setCurrentItemInternal(mRestoredCurItem, false, true); mRestoredCurItem = -1; mRestoredAdapterState = null; mRestoredClassLoader = null; } else if (!wasFirstLayout) { populate(); } else { requestLayout(); } } if (mAdapterChangeListeners != null && !mAdapterChangeListeners.isEmpty()) { for (int i = 0, count = mAdapterChangeListeners.size(); i < count; i++) { mAdapterChangeListeners.get(i).onAdapterChanged(this, oldAdapter, adapter); } } } }
ViewPager extends ViewGroup { public void setAdapter(@Nullable PagerAdapter adapter) { if (mAdapter != null) { mAdapter.setViewPagerObserver(null); mAdapter.startUpdate(this); for (int i = 0; i < mItems.size(); i++) { final ItemInfo ii = mItems.get(i); mAdapter.destroyItem(this, ii.position, ii.object); } mAdapter.finishUpdate(this); mItems.clear(); removeNonDecorViews(); mCurItem = 0; scrollTo(0, 0); } final PagerAdapter oldAdapter = mAdapter; mAdapter = adapter; mExpectedAdapterCount = 0; if (mAdapter != null) { if (mObserver == null) { mObserver = new PagerObserver(); } mAdapter.setViewPagerObserver(mObserver); mPopulatePending = false; final boolean wasFirstLayout = mFirstLayout; mFirstLayout = true; mExpectedAdapterCount = mAdapter.getCount(); if (mRestoredCurItem >= 0) { mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader); setCurrentItemInternal(mRestoredCurItem, false, true); mRestoredCurItem = -1; mRestoredAdapterState = null; mRestoredClassLoader = null; } else if (!wasFirstLayout) { populate(); } else { requestLayout(); } } if (mAdapterChangeListeners != null && !mAdapterChangeListeners.isEmpty()) { for (int i = 0, count = mAdapterChangeListeners.size(); i < count; i++) { mAdapterChangeListeners.get(i).onAdapterChanged(this, oldAdapter, adapter); } } } ViewPager(@NonNull Context context);  ViewPager(@NonNull Context context, @Nullable AttributeSet attrs); }
ViewPager extends ViewGroup { public void setAdapter(@Nullable PagerAdapter adapter) { if (mAdapter != null) { mAdapter.setViewPagerObserver(null); mAdapter.startUpdate(this); for (int i = 0; i < mItems.size(); i++) { final ItemInfo ii = mItems.get(i); mAdapter.destroyItem(this, ii.position, ii.object); } mAdapter.finishUpdate(this); mItems.clear(); removeNonDecorViews(); mCurItem = 0; scrollTo(0, 0); } final PagerAdapter oldAdapter = mAdapter; mAdapter = adapter; mExpectedAdapterCount = 0; if (mAdapter != null) { if (mObserver == null) { mObserver = new PagerObserver(); } mAdapter.setViewPagerObserver(mObserver); mPopulatePending = false; final boolean wasFirstLayout = mFirstLayout; mFirstLayout = true; mExpectedAdapterCount = mAdapter.getCount(); if (mRestoredCurItem >= 0) { mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader); setCurrentItemInternal(mRestoredCurItem, false, true); mRestoredCurItem = -1; mRestoredAdapterState = null; mRestoredClassLoader = null; } else if (!wasFirstLayout) { populate(); } else { requestLayout(); } } if (mAdapterChangeListeners != null && !mAdapterChangeListeners.isEmpty()) { for (int i = 0, count = mAdapterChangeListeners.size(); i < count; i++) { mAdapterChangeListeners.get(i).onAdapterChanged(this, oldAdapter, adapter); } } } ViewPager(@NonNull Context context);  ViewPager(@NonNull Context context, @Nullable AttributeSet attrs); void setAdapter(@Nullable PagerAdapter adapter); @Nullable PagerAdapter getAdapter(); void addOnAdapterChangeListener(@NonNull OnAdapterChangeListener listener); void removeOnAdapterChangeListener(@NonNull OnAdapterChangeListener listener); void setCurrentItem(int item); void setCurrentItem(int item, boolean smoothScroll); int getCurrentItem(); @Deprecated void setOnPageChangeListener(OnPageChangeListener listener); void addOnPageChangeListener(@NonNull OnPageChangeListener listener); void removeOnPageChangeListener(@NonNull OnPageChangeListener listener); void clearOnPageChangeListeners(); void setPageTransformer(boolean reverseDrawingOrder,
            @Nullable PageTransformer transformer); void setPageTransformer(boolean reverseDrawingOrder,
            @Nullable PageTransformer transformer, int pageLayerType); int getOffscreenPageLimit(); void setOffscreenPageLimit(int limit); void setPageMargin(int marginPixels); int getPageMargin(); void setPageMarginDrawable(@Nullable Drawable d); void setPageMarginDrawable(@DrawableRes int resId); @Override Parcelable onSaveInstanceState(); @Override void onRestoreInstanceState(Parcelable state); @Override void addView(View child, int index, ViewGroup.LayoutParams params); @Override void removeView(View view); @Override void computeScroll(); @Override boolean onInterceptTouchEvent(MotionEvent ev); @Override boolean onTouchEvent(MotionEvent ev); @Override void draw(Canvas canvas); boolean beginFakeDrag(); void endFakeDrag(); void fakeDragBy(float xOffset); boolean isFakeDragging(); @Override boolean canScrollHorizontally(int direction); @Override boolean dispatchKeyEvent(KeyEvent event); boolean executeKeyEvent(@NonNull KeyEvent event); boolean arrowScroll(int direction); @Override void addFocusables(ArrayList<View> views, int direction, int focusableMode); @Override void addTouchables(ArrayList<View> views); @Override boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event); @Override ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs); }
ViewPager extends ViewGroup { public void setAdapter(@Nullable PagerAdapter adapter) { if (mAdapter != null) { mAdapter.setViewPagerObserver(null); mAdapter.startUpdate(this); for (int i = 0; i < mItems.size(); i++) { final ItemInfo ii = mItems.get(i); mAdapter.destroyItem(this, ii.position, ii.object); } mAdapter.finishUpdate(this); mItems.clear(); removeNonDecorViews(); mCurItem = 0; scrollTo(0, 0); } final PagerAdapter oldAdapter = mAdapter; mAdapter = adapter; mExpectedAdapterCount = 0; if (mAdapter != null) { if (mObserver == null) { mObserver = new PagerObserver(); } mAdapter.setViewPagerObserver(mObserver); mPopulatePending = false; final boolean wasFirstLayout = mFirstLayout; mFirstLayout = true; mExpectedAdapterCount = mAdapter.getCount(); if (mRestoredCurItem >= 0) { mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader); setCurrentItemInternal(mRestoredCurItem, false, true); mRestoredCurItem = -1; mRestoredAdapterState = null; mRestoredClassLoader = null; } else if (!wasFirstLayout) { populate(); } else { requestLayout(); } } if (mAdapterChangeListeners != null && !mAdapterChangeListeners.isEmpty()) { for (int i = 0, count = mAdapterChangeListeners.size(); i < count; i++) { mAdapterChangeListeners.get(i).onAdapterChanged(this, oldAdapter, adapter); } } } ViewPager(@NonNull Context context);  ViewPager(@NonNull Context context, @Nullable AttributeSet attrs); void setAdapter(@Nullable PagerAdapter adapter); @Nullable PagerAdapter getAdapter(); void addOnAdapterChangeListener(@NonNull OnAdapterChangeListener listener); void removeOnAdapterChangeListener(@NonNull OnAdapterChangeListener listener); void setCurrentItem(int item); void setCurrentItem(int item, boolean smoothScroll); int getCurrentItem(); @Deprecated void setOnPageChangeListener(OnPageChangeListener listener); void addOnPageChangeListener(@NonNull OnPageChangeListener listener); void removeOnPageChangeListener(@NonNull OnPageChangeListener listener); void clearOnPageChangeListeners(); void setPageTransformer(boolean reverseDrawingOrder,
            @Nullable PageTransformer transformer); void setPageTransformer(boolean reverseDrawingOrder,
            @Nullable PageTransformer transformer, int pageLayerType); int getOffscreenPageLimit(); void setOffscreenPageLimit(int limit); void setPageMargin(int marginPixels); int getPageMargin(); void setPageMarginDrawable(@Nullable Drawable d); void setPageMarginDrawable(@DrawableRes int resId); @Override Parcelable onSaveInstanceState(); @Override void onRestoreInstanceState(Parcelable state); @Override void addView(View child, int index, ViewGroup.LayoutParams params); @Override void removeView(View view); @Override void computeScroll(); @Override boolean onInterceptTouchEvent(MotionEvent ev); @Override boolean onTouchEvent(MotionEvent ev); @Override void draw(Canvas canvas); boolean beginFakeDrag(); void endFakeDrag(); void fakeDragBy(float xOffset); boolean isFakeDragging(); @Override boolean canScrollHorizontally(int direction); @Override boolean dispatchKeyEvent(KeyEvent event); boolean executeKeyEvent(@NonNull KeyEvent event); boolean arrowScroll(int direction); @Override void addFocusables(ArrayList<View> views, int direction, int focusableMode); @Override void addTouchables(ArrayList<View> views); @Override boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event); @Override ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs); static final int SCROLL_STATE_IDLE; static final int SCROLL_STATE_DRAGGING; static final int SCROLL_STATE_SETTLING; }