片段从夏洛特片段活动呼叫方法 [英] Call methods of fragment from sherlock fragment activity
问题描述
我正在开发用行动吧夏洛克的应用程序。 在夏洛特片段活动我增加了两个片段
公共类我的项目扩展SherlockFragmentActivity实现ActionBar.TabListener {
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
的setContentView(R.layout.main);
最后的动作条动作条= getSupportActionBar();
actionBar.setTitle(我的项目);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
名单<片断>碎片=新矢量<片断>();
fragments.add(Fragment.instantiate(此,MainSettinngs.class.getName()));
fragments.add(Fragment.instantiate(此,OtherSettings.class.getName()));
//创建将返回一个片段为每两个的适配器
//应用程序的主要部分。
mSectionsPagerAdapter =新SectionsPagerAdapter(getSupportFragmentManager(),片段,这一点);
//设置的ViewPager与部分适配器。
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//当不同部分之间的刷卡,选择相应的
//标签。我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有
//引用的标签。
mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效onPageSelected(INT位置){
actionBar.setSelectedNavigationItem(位置);
}
});
//对于每个应用程序的章节中,添加一个标签,操作栏。
actionBar.addTab(actionBar.newTab().setText(getString(R.string.title_main_settings)).setTabListen(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.title_other_settings)).setTabListener(this));
}
@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
//当选择给定的标签,切换到对应页面
//该ViewPager。
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
}
@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
}
}
和SectionsPagerAdapter
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {
私人列表<片断>片段;
语境_context;
公共SectionsPagerAdapter(FragmentManager FM,名单,其中,片断>的片段,上下文的背景下){
超(FM);
this.fragments =片段;
_context =背景;
}
@覆盖
公共片段的getItem(INT位置){
//的getItem被称为实例化片段给定的页面。
//返回一个DummySectionFragment(定义为静态内部类
下图)与页面数量作为其唯一的参数//。
返回this.fragments.get(位置);
}
@覆盖
公众诠释getCount将(){
返回this.fragments.size();
}
}
通过这些,我有两个片段MainSettings和OtherSettings延伸支持片段级。
在我的福尔摩斯片段活动我想MainSettings和OtherSettings,这样我可以
目前的对象- 从我的福尔摩斯片段活动调用它的方法
我得到了答案。
由FragmentPagerAdapter提供的片段被自动标记时,他们正在初始化。
所以,让你不得不使用标签 机器人:切换:+ mViewPager.getId()+:+位置
//这会给你的片段的实例在位置0
。片段currentFragmentMainSettings = getSupportFragmentManager()findFragmentByTag(机器人:切换:+ mViewPager.getId()+:+ 0);
//现在打电话给你的片段的方法
((MainSettinngs)currentFragmentMainSettings).resetMainSettings();
。片段currentFragmentOtherSettings = getSupportFragmentManager()findFragmentByTag(机器人:切换:+ mViewPager.getId()+:+ 1);
((OtherSettings)currentFragmentOtherSettings).resetOtherSettings();
I am developing an app with action bar sherlock. In Sherlock fragment activity i added two fragments
public class My Project extends SherlockFragmentActivity implements ActionBar.TabListener{
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
setContentView(R.layout.main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("My Project");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MainSettinngs.class.getName()));
fragments.add(Fragment.instantiate(this, OtherSettings.class.getName()));
// Create the adapter that will return a fragment for each of the two
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),fragments,this);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText(getString(R.string.title_main_settings)).setTabListen(this));
actionBar.addTab(actionBar.newTab().setText(getString(R.string.title_other_settings)).setTabListener(this));
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
}
and SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
Context _context;
public SectionsPagerAdapter(FragmentManager fm,List<Fragment> fragments,Context context) {
super(fm);
this.fragments = fragments;
_context = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
With these i have two fragments MainSettings and OtherSettings which extends support fragment class.
In my Sherlock fragment activity i want current objects of the MainSettings and OtherSettings so that i can
- call a method of it from my Sherlock fragment activity
I got the answer.
The Fragments supplied by the FragmentPagerAdapter are auto-tagged when they're instantiated.
So to get the tag you have to use "android:switcher:" + mViewPager.getId() + ":" + position
//this will give you instance of the fragment at position 0
Fragment currentFragmentMainSettings = getSupportFragmentManager().findFragmentByTag( "android:switcher:" + mViewPager.getId() + ":" + 0);
//Now call the method of your fragment
((MainSettinngs) currentFragmentMainSettings).resetMainSettings();
Fragment currentFragmentOtherSettings = getSupportFragmentManager().findFragmentByTag( "android:switcher:" + mViewPager.getId() + ":" + 1);
((OtherSettings) currentFragmentOtherSettings).resetOtherSettings();
这篇关于片段从夏洛特片段活动呼叫方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!