Java Code Examples for androidx.viewpager.widget.ViewPager#getAdapter()

The following examples show how to use androidx.viewpager.widget.ViewPager#getAdapter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IconPageIndicator.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    PagerAdapter adapter = view.getAdapter();
    if (adapter == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    view.setOnPageChangeListener(this);
    notifyDataSetChanged();
}
 
Example 2
Source File: TabLayoutWithViewPagerTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private static <Q> ViewAction addItemToPager(final String title, final Q content) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(ViewPager.class);
    }

    @Override
    public String getDescription() {
      return "Add item and notify on content change";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();

      final ViewPager viewPager = (ViewPager) view;
      @SuppressWarnings("unchecked") // no way to avoid this cast
      final BasePagerAdapter<Q> viewPagerAdapter = (BasePagerAdapter<Q>) viewPager.getAdapter();
      viewPagerAdapter.add(title, content);
      viewPagerAdapter.notifyDataSetChanged();

      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 3
Source File: LinePageIndicator.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
@Override
public void setViewPager(ViewPager viewPager) {
    if (mViewPager == viewPager) {
        return;
    }
    if (mViewPager != null) {
        //Clear us from the old pager.
        mViewPager.setOnPageChangeListener(null);
    }
    if (viewPager.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = viewPager;
    mViewPager.setOnPageChangeListener(this);
    invalidate();
}
 
Example 4
Source File: SlidingTabLayout.java    From UIWidget with Apache License 2.0 6 votes vote down vote up
/**
 * 关联ViewPager,用于不想在ViewPager适配器中设置titles数据的情况
 */
public void setViewPager(ViewPager vp, String[] titles) {
    if (vp == null || vp.getAdapter() == null) {
        throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !");
    }

    if (titles == null || titles.length == 0) {
        throw new IllegalStateException("Titles can not be EMPTY !");
    }

    if (titles.length != vp.getAdapter().getCount()) {
        throw new IllegalStateException("Titles length must be the same as the page count !");
    }

    this.mViewPager = vp;
    mTitles = new ArrayList<>();
    Collections.addAll(mTitles, titles);

    this.mViewPager.removeOnPageChangeListener(this);
    this.mViewPager.addOnPageChangeListener(this);
    notifyDataSetChanged();
}
 
Example 5
Source File: AutoRefreshUtil.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public void autoLoadMore(boolean atOnce, boolean autoRefreshUseSmoothScroll) {
    if (mRefreshLayout != null) {
        if (mStatus != SmoothRefreshLayout.SR_STATUS_INIT) return;
        if (mRefreshLayout.isNotYetInEdgeCannotMoveFooter()) {
            if (mRefreshLayout.isVerticalOrientation()) {
                ScrollCompat.flingCompat(mTargetView, mMaximumFlingVelocity);
            } else {
                if (mTargetView instanceof ViewPager) {
                    final ViewPager pager = (ViewPager) mTargetView;
                    final PagerAdapter adapter = pager.getAdapter();
                    if (adapter == null) return;
                    if (adapter.getCount() <= 0) return;
                    pager.setCurrentItem(adapter.getCount() - 1, true);
                } else {
                    HorizontalScrollCompat.flingCompat(mTargetView, mMaximumFlingVelocity);
                }
            }
            mNeedToTriggerLoadMore = true;
            mCachedActionAtOnce = atOnce;
            mCachedAutoRefreshUseSmoothScroll = autoRefreshUseSmoothScroll;
        } else {
            mRefreshLayout.autoLoadMore(atOnce, autoRefreshUseSmoothScroll);
            mNeedToTriggerLoadMore = false;
            mCachedActionAtOnce = false;
            mCachedAutoRefreshUseSmoothScroll = false;
        }
        ViewCompat.postOnAnimation(mRefreshLayout, this);
    }
}
 
Example 6
Source File: SmartTabLayout.java    From SmartTabLayout with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the associated view pager. Note that the assumption here is that the pager content
 * (number of tabs and tab titles) does not change after this call has been made.
 */
public void setViewPager(ViewPager viewPager) {
  tabStrip.removeAllViews();

  this.viewPager = viewPager;
  if (viewPager != null && viewPager.getAdapter() != null) {
    viewPager.addOnPageChangeListener(new InternalViewPagerListener());
    populateTabStrip();
  }
}
 
Example 7
Source File: ViewActions.java    From Kore with Apache License 2.0 5 votes vote down vote up
public static ViewAction setCurrentViewPagerItem(final int pageTitleResourceId) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return new TypeSafeMatcher<View>() {
                @Override
                protected boolean matchesSafely(View item) {
                    return item instanceof ViewPager;
                }

                @Override
                public void describeTo(Description description) {
                    description.appendText("is a SeekBar.");
                }
            };
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            ViewPager viewPager = (ViewPager) view;
            String expectedTitle = view.getResources().getString(pageTitleResourceId);
            PagerAdapter pagerAdapter = viewPager.getAdapter();
            for(int i = 0; i < pagerAdapter.getCount(); i++) {
                if (expectedTitle.contentEquals(pagerAdapter.getPageTitle(i))) {
                    viewPager.setCurrentItem(i);
                    return;
                }
            }
        }
    };
}
 
Example 8
Source File: PagerSlidingTabStrip.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
public void setViewPager(@NonNull ViewPager pager) {
    this.pager = pager;

    if (pager.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }

    pager.setOnPageChangeListener(pageListener);

    notifyDataSetChanged();
}
 
Example 9
Source File: SlidingTabLayout.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
/**
 * 关联ViewPager
 */
public void setViewPager(ViewPager vp) {
    if (vp == null || vp.getAdapter() == null) {
        throw new IllegalStateException("ViewPager or ViewPager adapter can not be NULL !");
    }

    this.mViewPager = vp;

    this.mViewPager.removeOnPageChangeListener(this);
    this.mViewPager.addOnPageChangeListener(this);
    notifyDataSetChanged();
}
 
Example 10
Source File: CirclePageIndicator.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.removeOnPageChangeListener(this);
    }
    if (view.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    mViewPager.addOnPageChangeListener(this);
    invalidate();
}
 
Example 11
Source File: CirclePageIndicator.java    From Kore with Apache License 2.0 5 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    if (view.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    mViewPager.setOnPageChangeListener(this);
    invalidate();
}
 
Example 12
Source File: TabStripHelper.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
private void onDetachedFromViewPager(@NonNull ViewPager pager) {
    if (mView instanceof TabStripView)
        ((TabStripView) mView).onDetachedFromViewPager(pager);
    else if (mView instanceof TabStripViewGroup)
        ((TabStripViewGroup) mView).onDetachedFromViewPager(pager);
    pager.removeOnPageChangeListener(this);
    pager.removeOnAdapterChangeListener(this);
    final PagerAdapter adapter = pager.getAdapter();
    if (adapter != null) {
        adapter.unregisterDataSetObserver(this);
    }
}
 
Example 13
Source File: PagerSlidingTabStrip.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
public void setViewPager(ViewPager pager)
    {
        this.pager = pager;

        if (pager.getAdapter() == null) { throw new IllegalStateException("ViewPager does not have adapter instance."); }
        //modified by fee 2016-06-27
        pager.addOnPageChangeListener(pageListener);//本地要监听外部ViewPager的页面切换,以自动滑动水平的选项卡

//        pager.setOnPageChangeListener(pageListener);

        notifyDataSetChanged();
    }
 
Example 14
Source File: PagerSlidingTabStrip.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void setViewPager(ViewPager pager) {
    this.pager = pager;
    if (pager.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    pager.setOnPageChangeListener(pageListener);
    notifyDataSetChanged();
}
 
Example 15
Source File: PagerSlidingTabStrip.java    From EmojiChat with Apache License 2.0 5 votes vote down vote up
public void setViewPager(ViewPager pager) {
    this.pager = pager;

    if (pager.getAdapter() == null) {
        throw new IllegalStateException(
                "ViewPager does not have adapter instance.");
    }

    pager.addOnPageChangeListener(pageListener);

    notifyDataSetChanged();
}
 
Example 16
Source File: CirclePageIndicator.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    if (view.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    mViewPager.setOnPageChangeListener(this);
    invalidate();
}
 
Example 17
Source File: TitlePageIndicator.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    if (view.getAdapter() == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    mViewPager.setOnPageChangeListener(this);
    invalidate();
}
 
Example 18
Source File: TabLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void setupWithViewPager(
    @Nullable final ViewPager viewPager, boolean autoRefresh, boolean implicitSetup) {
  if (this.viewPager != null) {
    // If we've already been setup with a ViewPager, remove us from it
    if (pageChangeListener != null) {
      this.viewPager.removeOnPageChangeListener(pageChangeListener);
    }
    if (adapterChangeListener != null) {
      this.viewPager.removeOnAdapterChangeListener(adapterChangeListener);
    }
  }

  if (currentVpSelectedListener != null) {
    // If we already have a tab selected listener for the ViewPager, remove it
    removeOnTabSelectedListener(currentVpSelectedListener);
    currentVpSelectedListener = null;
  }

  if (viewPager != null) {
    this.viewPager = viewPager;

    // Add our custom OnPageChangeListener to the ViewPager
    if (pageChangeListener == null) {
      pageChangeListener = new TabLayoutOnPageChangeListener(this);
    }
    pageChangeListener.reset();
    viewPager.addOnPageChangeListener(pageChangeListener);

    // Now we'll add a tab selected listener to set ViewPager's current item
    currentVpSelectedListener = new ViewPagerOnTabSelectedListener(viewPager);
    addOnTabSelectedListener(currentVpSelectedListener);

    final PagerAdapter adapter = viewPager.getAdapter();
    if (adapter != null) {
      // Now we'll populate ourselves from the pager adapter, adding an observer if
      // autoRefresh is enabled
      setPagerAdapter(adapter, autoRefresh);
    }

    // Add a listener so that we're notified of any adapter changes
    if (adapterChangeListener == null) {
      adapterChangeListener = new AdapterChangeListener();
    }
    adapterChangeListener.setAutoRefresh(autoRefresh);
    viewPager.addOnAdapterChangeListener(adapterChangeListener);

    // Now update the scroll position to match the ViewPager's current item
    setScrollPosition(viewPager.getCurrentItem(), 0f, true);
  } else {
    // We've been given a null ViewPager so we need to clear out the internal state,
    // listeners and observers
    this.viewPager = null;
    setPagerAdapter(null, false);
  }

  setupViewPagerImplicitly = implicitSetup;
}
 
Example 19
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param tabLayout TabLayout instance
 * @param viewPager ViewPager instance
 */
public TabLayoutHelper(@NonNull TabLayout tabLayout, @NonNull ViewPager viewPager) {
    PagerAdapter adapter = viewPager.getAdapter();

    if (adapter == null) {
        throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
    }

    mTabLayout = tabLayout;
    mViewPager = viewPager;

    mInternalDataSetObserver = new DataSetObserver() {
        @Override
        public void onChanged() {
            handleOnDataSetChanged();
        }
    };

    mInternalOnTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            handleOnTabSelected(tab);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            handleOnTabUnselected(tab);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            handleOnTabReselected(tab);
        }
    };

    mInternalTabLayoutOnPageChangeListener = new FixedTabLayoutOnPageChangeListener(mTabLayout);

    mInternalOnAdapterChangeListener = new ViewPager.OnAdapterChangeListener() {
        @Override
        public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
            handleOnAdapterChanged(viewPager, oldAdapter, newAdapter);
        }
    };

    setupWithViewPager(mTabLayout, mViewPager);
}
 
Example 20
Source File: TabLayout.java    From a with GNU General Public License v3.0 4 votes vote down vote up
private void setupWithViewPager(@Nullable final ViewPager viewPager, boolean autoRefresh,
                                boolean implicitSetup) {
    if (mViewPager != null) {
        // If we've already been setup with a ViewPager, remove us from it
        if (mPageChangeListener != null) {
            mViewPager.removeOnPageChangeListener(mPageChangeListener);
        }
        if (mAdapterChangeListener != null) {
            mViewPager.removeOnAdapterChangeListener(mAdapterChangeListener);
        }
    }

    if (mCurrentVpSelectedListener != null) {
        // If we already have a tab selected listener for the ViewPager, remove it
        removeOnTabSelectedListener(mCurrentVpSelectedListener);
        mCurrentVpSelectedListener = null;
    }

    if (viewPager != null) {
        mViewPager = viewPager;

        // Add our custom OnPageChangeListener to the ViewPager
        if (mPageChangeListener == null) {
            mPageChangeListener = new TabLayoutOnPageChangeListener(this);
        }
        mPageChangeListener.reset();
        viewPager.addOnPageChangeListener(mPageChangeListener);

        // Now we'll add a tab selected listener to set ViewPager's current item
        mCurrentVpSelectedListener = new ViewPagerOnTabSelectedListener(viewPager);
        addOnTabSelectedListener(mCurrentVpSelectedListener);

        final PagerAdapter adapter = viewPager.getAdapter();
        if (adapter != null) {
            // Now we'll populate ourselves from the pager adapter, adding an observer if
            // autoRefresh is enabled
            setPagerAdapter(adapter, autoRefresh);
        }

        // Add a listener so that we're notified of any adapter changes
        if (mAdapterChangeListener == null) {
            mAdapterChangeListener = new AdapterChangeListener();
        }
        mAdapterChangeListener.setAutoRefresh(autoRefresh);
        viewPager.addOnAdapterChangeListener(mAdapterChangeListener);

        // Now update the scroll position to match the ViewPager's current item
        setScrollPosition(viewPager.getCurrentItem(), 0f, true);
    } else {
        // We've been given a null ViewPager so we need to clear out the internal state,
        // listeners and observers
        mViewPager = null;
        setPagerAdapter(null, false);
    }

    mSetupViewPagerImplicitly = implicitSetup;
}