使用FragmentPagerAdapter以编程方式从Fragment更改选项卡

时间:2014-08-20 15:14:58

标签: java android android-fragments android-tabhost fragmentpageradapter

我正在尝试让我的应用从标签1更改为标签3.标签位于自定义TabsPagerAdapter extends FragmentPagerAdapter

我尝试更改标签,但这会导致NullPointerException。机制是否与FragmentPagerAdapter不同?

TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);

2 个答案:

答案 0 :(得分:7)

知道了。需要使用我的ViewPager代替TabHost

ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
viewPager.setCurrentItem(2);

答案 1 :(得分:0)

将此代码用于标签

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();
        }
    }
}