user2082169 user2082169 - 1 month ago 15
Android Question

Get Horizontal Scroll on TabLayout

I want to interpolate between two colors while scrolling or "swiping" to the next tab in a TabLayout. To do this, I need some way to know when the user is swiping between tabs. I have tried using TabLayout.OnScrollChangeListener but the method is never called. Is it possible to know when the user is swiping to a new tab and, if so, how?

Edit:

libraryTabs.setOnScrollChangeListener(new TabLayout.OnScrollChangeListener() {

@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {

}
});


is never called.

Answer

i think to achieve what you want then you will need a ViewPage something like this

ViewPager mViewPager;
    TabLayout mSlidingTabs;
    mSlidingTabs = (TabLayout) findViewById(R.id.tabs);
    mSlidingTabs.setupWithViewPager(mViewPager);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Log.w(TAG, "onPageScrolled");
            // do what you want
        }
        @Override
        public void onPageSelected(int position) {
            Log.w(TAG, "onPageSelected");
            // do what you want
        }
        @Override
        public void onPageScrollStateChanged(int state) {
            Log.w(TAG, "onPageScrollStateChanged");
            // do what you want
        }
    });
Comments