Change Tab Programmatically from Fragment with FragmentPagerAdapter
Asked Answered
S

2

5

I'm trying to get my app to change from tab 1 to tab 3. The tabs are in a custom TabsPagerAdapter which extends FragmentPagerAdapter.

I've tried to change the tab likes this but it causes NullPointerException. Is the mechanism different with a FragmentPagerAdapter?

TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);
Seaman answered 20/8, 2014 at 15:14 Comment(0)
S
11

Got it. Needed to use my ViewPager instead of TabHost

ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
viewPager.setCurrentItem(2);
Seaman answered 21/8, 2014 at 9:12 Comment(0)
A
0

use this code for tab

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener {

    private TabHost mTabHost;
    private final HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
    TabInfo mLastTab = null;
    public Context mContext;
    public MainActivity mainActivity;
    static IViewFragmentListener fragmentActionListner;
    String[] menuItems;
    int[] menuItemsIcons;

    private class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;

        TabInfo(String tag, Class<?> clazz, Bundle args) {
            this.tag = tag;
            this.clss = clazz;
            this.args = args;
        }

    }

    class TabFactory implements TabContentFactory {

        private final Context mContext;

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

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

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.am_tabs_layout);


        mContext = this;
        mainActivity = this;
        initialiseTabHost(savedInstanceState);



    }

    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        MainActivity.addTab(
                this,
                this.mTabHost,
                this.mTabHost.newTabSpec("Tab1").setIndicator("Name of your 1st tab",
                        getResources().getDrawable(R.drawable.running)),
                (tabInfo = new TabInfo("Tab1", NextActivity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.addTab(
                this,
                this.mTabHost,
                this.mTabHost.newTabSpec("Tab2").setIndicator("Name of your 2nd tab",
                        getResources().getDrawable(R.drawable.xyz)),
                (tabInfo = new TabInfo("Tab2", NextActivity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        MainActivity.addTab(
                this,
                this.mTabHost,
                this.mTabHost.newTabSpec("Tab3").setIndicator("Name of your 3rd tab",
                        getResources().getDrawable(R.drawable.abc)),
                (tabInfo = new TabInfo("Tab3", NextActivity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        this.onTabChanged("Tab1");
        mTabHost.setOnTabChangedListener(this);
    }

    private static void addTab(MainActivity activity, TabHost tabHost,
            TabHost.TabSpec tabSpec, TabInfo tabInfo) {

        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();

        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }
        tabHost.addTab(tabSpec);

    }

    public void onTabChanged(String tag) {
        TabInfo newTab = this.mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = this.getSupportFragmentManager()
                    .beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            fragmentActionListner = ((IViewFragmentListener) mLastTab.fragment);
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();
        }
    }
}
Alethaalethea answered 20/8, 2014 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.