Boardy
Boardy

Reputation: 36225

Tab Switching using Horizontal Swipe

I am currently working on an android project and have created a little test project to test the use of the Tab and changing tab by using a horizontal swipe gesture.

The code is pretty much working except for one problem. When I swipe the content to change the tab the activity for the second tab is shown but the actual tab isn't selected. I.e. on android 4.2 when the app loads it starts up the main FragmentActivity and then displays the first fragment (fragment 1) which is inside tab 1. Tab 1 then has a blue line underneath to show that tab is selected.

If I then swipe the screen, the second activity is shown but the blue line stays under Tab 1 and doesn't move across to tab 2.

However, if I select the actual tab instead of swiping then the blue line does move onto the second tab as expected.

I suspect I've missed something really simple from the tutorial I was looking at but I can't find for the life of me what that might be.

Below is the code I am for the FragmentActivity. This is what is loaded when the app first starts.

public class TabsViewPagerFragmentActivity extends FragmentActivity 
    implements OnTabChangeListener, OnPageChangeListener{

    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
    private PagerAdapter mPagerAdapter;

    private class TabInfo
    {
        private String tag;
        private Class<?> clss;
        private Bundle bundle;
        private Fragment fragment;
        public TabInfo(String tag, Class<?> clss, Bundle bundle) {
            this.tag = tag;
            this.clss = clss;
            this.bundle = bundle;
        }
    }

    class TabFactory implements TabContentFactory
    {
        private final Context mContext;

        public TabFactory (Context context)
        {
            mContext = context;
        }

        public View createTabContent(String tag)
        {
            View view = new View(mContext);
            view.setMinimumWidth(0);
            view.setMinimumHeight(0);
            return view;
        }   
    }

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null)
        {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
        this.initialiseViewPager();
    }

    protected void onSaveInstancestate(Bundle outState)
    {
        outState.putString("tab", mTabHost.getCurrentTabTag());
        super.onSaveInstanceState(outState);
    }

    private void initialiseViewPager()
    {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
        this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
        this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(this.mPagerAdapter);
        this.mViewPager.setOnPageChangeListener(this);
    }

    private void initialiseTabHost(Bundle bundle)
    {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost,
                this.mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1"),
                tabInfo = new TabInfo("Tab1", Tab1Fragment.class, bundle));
        TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost,
                this.mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2"),
                tabInfo = new TabInfo("Tab2", Tab2Fragment.class, bundle));

        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        mTabHost.setOnTabChangedListener(this);
    }

    private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost,
            TabHost.TabSpec tabSpec, TabInfo tabInfo)
    {
        tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, 
            int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.mViewPager.setCurrentItem(position);
    }

    @Override
    public void onTabChanged(String tag) {
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }
}

Below is the code for the PageAdapter

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

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

    public int getCount()
    {
        return this.fragments.size();
    }

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

Thanks for any help you can provide.

Upvotes: 1

Views: 1114

Answers (1)

saschoar
saschoar

Reputation: 8230

Add mTabHost.setCurrentTab(position) to the onPageSelected() method in your FragmentActivity.

Upvotes: 1

Related Questions