(Android)如何从fragmentActivity中调用片段中的方法

时间:2013-12-02 08:25:01

标签: android android-fragments android-fragmentactivity

我的布局如下:

enter image description here

我的fragmentActivity使用ViewPager + TabHost

标签标签中有3个标签

“save”按钮位于我的fragmentActivity

我希望在单击“保存”按钮

时通过调用save()方法保存所有3个选项卡中的数据

但我不知道怎么称呼他们

有没有办法从子片段调用方法?

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actreg);     
    this.initialiseTabHost(savedInstanceState);



    intialiseViewPager();
    save=(Button)findViewById(R.id.save);
   save.setOnClickListener(new OnClickListener() {
    .....
        }

    }
});
}

private void initialiseTabHost(Bundle args) {



     tabHost = (TabHost)findViewById(android.R.id.tabhost);
     tabHost.setup();
    TabInfo tabInfo = null;
     FragmentActivity.AddTab(this, this.tabHost, this.tabHost.newTabSpec("Tab1").setIndicator("Tab1"),
            (tabInfo = new TabInfo("Tab1", fragment1.class, args)));
    this.myHashMapTabInfo.put(tabInfo.tag, tabInfo);
    ......



    tabHost.setOnTabChangedListener(new OnTabChangeListener() {.....
        }
    });
}
@Override

private static void AddTab(fragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    tabSpec.setContent(activity.new TabFactory(activity));
    tabHost.addTab(tabSpec);
}
private class TabInfo {
    private String tag;
    private Class<?> clss;
    private 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;
    }

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

0 个答案:

没有答案