Search code examples
androidandroid-viewpagerviewpagerindicator

Disable smooth animation when clicking on tabs using ViewPagerIndicator


I'm using TabPageIndicator from ViewPagerIndicator lib with ViewPager to display 6 fragments. Suppose I'm on 1st page, if I click 6th tab I'll see all my pages scrolled. Is it possible to disable this animation? Maybe I can somehow disable it in ViewPager?

Here is code of adapter:

public class TabBarFragmentPagerAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
private final List<Fragment> items;
private static final String[] TITLES = new String[] { "Home", "Profile", "Explore", "Contacts", "Beacon" };
private static final int[] ICONS = new int[] {
        R.drawable.icon_tabbar_home_bg,
        R.drawable.icon_tabbar_profile_bg,
        R.drawable.icon_tabbar_explore_bg,
        R.drawable.icon_tabbar_contacts_bg,
        R.drawable.icon_tabbar_beacon_bg
};

public TabBarFragmentPagerAdapter(FragmentManager fm, List<Fragment> items) {
    super(fm);
    this.items = items;
}

@Override
public Fragment getItem(int position) {
    return items.get(position);
}

@Override
public int getIconResId(int index) {
    return ICONS[index];
}

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

@Override
public int getCount() {
    return items.size();
}
}

Solution

  • I've investigated code of TabPageIndicator and I've found that it's impossible for now. See code of mTabClickListener:

    private final OnClickListener mTabClickListener = new OnClickListener() {
        public void onClick(View view) {
            TabView tabView = (TabView)view;
            final int oldSelected = mViewPager.getCurrentItem();
            final int newSelected = tabView.getIndex();
            mViewPager.setCurrentItem(newSelected);
            if (oldSelected == newSelected && mTabReselectedListener != null) {
                mTabReselectedListener.onTabReselected(newSelected);
            }
        }
    };
    

    To support this feature we should add second parameter to setCurrentItem. Something like this:

    mViewPager.setCurrentItem(newSelected, smoothScrollEnabled);