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

The following examples show how to use androidx.viewpager.widget.ViewPager#addOnAdapterChangeListener() . 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: TabStripHelper.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
private void onAttachedToViewPager(@NonNull ViewPager pager) {
    if (mView instanceof TabStripView)
        ((TabStripView) mView).onAttachedToViewPager(pager);
    else if (mView instanceof TabStripViewGroup)
        ((TabStripViewGroup) mView).onAttachedToViewPager(pager);
    pager.addOnPageChangeListener(this);
    pager.addOnAdapterChangeListener(this);
    onViewPagerAdapterChanged(null, pager.getAdapter());
}
 
Example 2
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 5 votes vote down vote up
protected void setupWithViewPager(@NonNull TabLayout tabLayout, @NonNull ViewPager viewPager) {
    final PagerAdapter adapter = viewPager.getAdapter();
    if (adapter == null) {
        throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
    }

    setTabsFromPagerAdapter(tabLayout, adapter, viewPager.getCurrentItem());

    viewPager.getAdapter().registerDataSetObserver(mInternalDataSetObserver);

    viewPager.addOnPageChangeListener(mInternalTabLayoutOnPageChangeListener);
    viewPager.addOnAdapterChangeListener(mInternalOnAdapterChangeListener);

    tabLayout.addOnTabSelectedListener(mInternalOnTabSelectedListener);
}
 
Example 3
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;
}
 
Example 4
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;
}