Skip to content Skip to sidebar Skip to footer

Tabhost Tabs Are Not Horizontally Scrolling When Swiping Using Viewpager

I am writing an app that uses many fragments. I have used ActionBarSherlock (with ActionBar). To work-around bug #240, I've decided to switch a functionally working implementation

Solution 1:

Thanks to this post, and to jucas in particular, I managed to get it to work. Here is the code:

@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    ViewtabView= mTabHost.getTabWidget().getChildAt(position);
    if (tabView != null)
    {
        finalintwidth= mHorizontalScroll.getWidth();
        finalintscrollPos= tabView.getLeft() - (width - tabView.getWidth()) / 2;
        mHorizontalScroll.scrollTo(scrollPos, 0);
    } else {
        mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
    }
}

Of course I had to get mHorizontalScroll initialized in several places. (If someone isn't sure how to do it, I'll be happy to post the full code.)

Solution 2:

I added the code below inside the onPageChangeListener to solve the issue.

mViewPager.setOnPageChangeListener(newViewPager.OnPageChangeListener() {


        @OverridepublicvoidonPageSelected(int position) {
            // on changing the page make respected tab selected//actionBar.setSelectedNavigationItem(position);
            mTabHost.setCurrentTab(position);
        }

        @OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            ViewtabView= mTabHost.getTabWidget().getChildAt(position);
            ViewmHorizontalScroll= (HorizontalScrollView) findViewById(R.id.mHorizontalScroll);
            if (tabView != null)
            {
                intwidth= mHorizontalScroll.getWidth();
                intscrollPos= tabView.getLeft() - (width - tabView.getWidth()) / 2;
                mHorizontalScroll.scrollTo(scrollPos, 0);
            } else {
                mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
            }
        }

        @OverridepublicvoidonPageScrollStateChanged(int arg0) {

        }
    });

Post a Comment for "Tabhost Tabs Are Not Horizontally Scrolling When Swiping Using Viewpager"