Android Fragment之间跳转

1.创建一个接口:

public interface ChangeFragment {
    void changge(int postion);
}

2.设置一个全局变量:

public class GlobalParms {
    private static HomeFragment sHomeFragment; //主页fragemnt
    private static ChartsFragment sChartsFragment; //走势图fragemnt
    private static ZiXunFragment sZiXunFragment; //资讯fragemnt
    private static OtherFragment sOtherFragment; //其它fragemnt
    public static ChangeFragment sChangeFragment;  //改变选中Frangment的接口

    /**
     * 获取主页Fragment
     *
     * @return
     */
    public static HomeFragment getHomeFragment() {
        if (sHomeFragment == null) {
            sHomeFragment = new HomeFragment();
        }
        return sHomeFragment;
    }

    /**
     * 走势图fragemnt
     *
     * @return
     */
    public static ChartsFragment getChartsFragment() {
        if (sChartsFragment == null) {
            sChartsFragment = new ChartsFragment();
        }
        return sChartsFragment;
    }

    /**
     * 资讯fragemnt
     *
     * @return
     */
    public static ZiXunFragment getZiXunFragment() {
        if (sZiXunFragment == null) {
            sZiXunFragment = new ZiXunFragment();
        }
        return sZiXunFragment;
    }

    /**
     * //其它fragemnt
     *
     * @return
     */
    public static OtherFragment getOtherFragment() {
        if (sOtherFragment == null) {
            sOtherFragment = new OtherFragment();
        }
        return sOtherFragment;
    }

    /**
     * 设置被选中的Fragment
     * @param changeFragment
     */
    public static void setFragmentSelected(ChangeFragment changeFragment) {
            sChangeFragment = changeFragment;

    }
}

3.在MainActivity创建接口:

private void initData() {
   GlobalParms.setFragmentSelected(new ChangeFragment() {
       @Override
       public void changge(int postion) {
                mTabHost.setCurrentTab(postion);
       }
   });
}

4.fragment中调用接口:


//Fragment中调用接口:
GlobalParms.sChangeFragment.changge(1);

你可能感兴趣的:(Android Fragment之间跳转)