Disable smooth animation when clicking on tabs using ViewPagerIndicator
Asked Answered
O

4

5

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();
}
}
Obadiah answered 18/4, 2013 at 8:8 Comment(3)
Why don't you use tabs from ABS then?Vhf
Or do you need a swipe feature and animation only when clicking on a next / prev tab?Vhf
No, I just want to disable smooth scrool when cliking my TabPageIndicator. I don't want to add ABS to project because I have rather specific design and I actualy don't need other ABS features. I also believe that adding ABS would make project more complicatedObadiah
O
4

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);
Obadiah answered 18/4, 2013 at 9:20 Comment(2)
You can always edit the TPI code to suit your needs you know )Vhf
yes, actualy I've already implemented this feature in my project. I think I'll prepare pull request for this feature when I'll have free timeObadiah
C
18

Another option is to override the setCurrentItem(int i) in a CustomViewPager class subclassing ViewPager like this:

@Override
public void setCurrentItem(int item) {
    super.setCurrentItem(item,false);
}
Cheffetz answered 7/6, 2013 at 16:47 Comment(0)
O
4

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);
Obadiah answered 18/4, 2013 at 9:20 Comment(2)
You can always edit the TPI code to suit your needs you know )Vhf
yes, actualy I've already implemented this feature in my project. I think I'll prepare pull request for this feature when I'll have free timeObadiah
K
1

you can now use TabLayout from Material libraries and viewPager2 and just this piece of code to sync them and also disable the smooth scroll:

TabLayoutMediator(tabLayout, pager, false, false) { tab, position ->
                when(position) {
                    0 -> tab.text = "text 1"
                    1 -> tab.text = "text 2"
                    2 -> tab.text = "text 3"
                }
            }.attach()

the second boolean parameter which is set to false is the smoothScroll param

more info here and official documentations: https://material.io/develop/android/components/tabs

Kreiner answered 5/10, 2020 at 22:32 Comment(0)
G
0

copy TabLayoutMediator class as local and modify the method:

@Override
    public void onPageScrollStateChanged(final int state) {
        // when drag page
        if (state == SCROLL_STATE_DRAGGING) {
            smoothScroll = true;
        } else if (state == SCROLL_STATE_IDLE) {
            // click tab
            smoothScroll = false;
        }
        previousScrollState = scrollState;
        scrollState = state;
    }

That works well for me.

Glossematics answered 16/9, 2022 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.