我正在开发一个带有操作栏sherlock的应用程序。 在Sherlock片段活动中,我添加了两个片段
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) {
}
}
和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();
}
}
这些我有两个片段MainSettings和OtherSettings,它们扩展了支持片段类。
在我的Sherlock片段活动中,我想要MainSettings和OtherSettings的当前对象,以便我可以
答案 0 :(得分:2)
我得到了答案。
FragmentPagerAdapter提供的片段在实例化时会自动标记。
所以要获得你必须使用的标签 “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();
答案 1 :(得分:1)
尝试这样的事情
public enum TabEnum
{
NONE, MAIN, OTHER;
public static TabEnum fromInt(int id)
{
switch (id)
{
case 0:
return MAIN;
case 1:
return OTHER;
}
return NONE;
}
public int toInt()
{
switch (this)
{
case MAIN:
return 0;
case OTHER:
return 1;
case NONE:
default:
return -1;
}
}
};
public FragmentMain getFarFragmentMain()
{
return ((FragmentMain) (getViewPager().getViewPagerAdapter().instantiateItem(getViewPager(), TabEnum.MAIN.toInt())));
}
public FragmentOther getFarFragmentOther()
{
return ((FragmentOther) (getViewPager().getViewPagerAdapter().instantiateItem(getViewPager(), TabEnum.OTHER.toInt())));
}
这些将返回您的片段的正确实例,您可以在以后将其用作
getFarFragmentMain().myMethodFromMain(arg1, arg2);