【问题标题】:Call methods of fragment from sherlock fragment activity从 sherlock 片段活动中调用片段的方法
【发布时间】:2013-09-07 15:36:46
【问题描述】:

我正在开发一个带有操作栏 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 的当前对象,以便我可以

  1. 从我的 Sherlock 片段活动中调用它的方法

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:

    我得到了答案。

    FragmentPagerAdapter 提供的片段在实例化时会自动标记。

    所以要获得你必须使用的标签 "android:switcher:" + mViewPager.getId() + ":" + 位置

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

    【讨论】:

      【解决方案2】:

      试试这样的

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

      【讨论】:

        猜你喜欢
        • 2012-09-21
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        相关资源
        最近更新 更多