Java Code Examples for com.google.android.material.tabs.TabLayout#getSelectedTabPosition()

The following examples show how to use com.google.android.material.tabs.TabLayout#getSelectedTabPosition() . 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: TabLayoutMediator.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
@Override
public void onPageSelected(final int position) {
    TabLayout tabLayout = mTabLayoutRef.get();
    if (tabLayout != null
            && tabLayout.getSelectedTabPosition() != position
            && position < tabLayout.getTabCount()) {
        // Select the tab, only updating the indicator if we're not being dragged/settled
        // (since onPageScrolled will handle that).
        boolean updateIndicator = mScrollState == SCROLL_STATE_IDLE
                || (mScrollState == SCROLL_STATE_SETTLING
                && mPreviousScrollState == SCROLL_STATE_IDLE);
        selectTab(tabLayout, tabLayout.getTabAt(position), updateIndicator);
    }
}
 
Example 2
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 5 votes vote down vote up
protected void setTabsFromPagerAdapter(@NonNull TabLayout tabLayout, @Nullable PagerAdapter adapter, int currentItem) {
    try {
        mDuringSetTabsFromPagerAdapter = true;

        int prevSelectedTab = tabLayout.getSelectedTabPosition();
        int prevScrollX = tabLayout.getScrollX();

        // remove all tabs
        tabLayout.removeAllTabs();

        // add tabs
        if (adapter != null) {
            int count = adapter.getCount();
            for (int i = 0; i < count; i++) {
                TabLayout.Tab tab = createNewTab(tabLayout, adapter, i);
                tabLayout.addTab(tab, false);
                updateTab(tab);
            }

            // select current tab
            currentItem = Math.min(currentItem, count - 1);
            if (currentItem >= 0) {
                tabLayout.getTabAt(currentItem).select();
            }
        }

        // adjust tab mode & gravity
        if (mAutoAdjustTabMode) {
            adjustTabMode(prevScrollX);
        } else {
            // restore scroll position if needed
            int curTabMode = tabLayout.getTabMode();
            if (curTabMode == TabLayout.MODE_SCROLLABLE) {
                tabLayout.scrollTo(prevScrollX, 0);
            }
        }
    } finally {
        mDuringSetTabsFromPagerAdapter = false;
    }
}
 
Example 3
Source File: TabLayoutHelper.java    From android-tablayouthelper with Apache License 2.0 5 votes vote down vote up
@Override
public void onPageSelected(int position) {
    final TabLayout tabLayout = mTabLayoutRef.get();
    if (tabLayout != null && tabLayout.getSelectedTabPosition() != position) {
        // Select the tab, only updating the indicator if we're not being dragged/settled
        // (since onPageScrolled will handle that).
        Internal.selectTab(tabLayout, tabLayout.getTabAt(position),
                mScrollState == ViewPager.SCROLL_STATE_IDLE);
    }
}