2017-04-04 79 views
1

如何從活動中調用片段中的方法? 我搜索了一些代碼,但大多數的人表示使用這樣的,Android - 如何從活動中調用片段中的方法

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); 
fragment.specific_function_name(); 

,但我沒有片段,我的XML

我需要另一種方式來調用片段的方法。

這是片段

public void video() 
{ 
    new getVideo().execute(); 
} 

方法,這是我的活動

public class ActivityTwitch extends AppCompatActivity 
{ 
    Toolbar toolbar; 
    TabLayout tabLayout; 
    ViewPager viewPager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_twitch); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setTitle("개구멍"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     toolbar.setNavigationOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       finish(); 
       overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
      } 
     }); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     Adapter adapter = new Adapter(getSupportFragmentManager()); 
     adapter.addFragment(new TwitchChannel(), "채널"); 
     adapter.addFragment(new TwitchStream(), "스트리밍"); 
     adapter.addFragment(new TwitchFeed(), "게시물"); 
     adapter.addFragment(new TwitchVideo(), "동영상"); 

     viewPager = (ViewPager) findViewById(R.id.viewPager); 
     viewPager.setAdapter(adapter); 

     tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
     tabLayout.setupWithViewPager(viewPager); 
    } 

    public class Adapter extends FragmentPagerAdapter 
    { 
     private final List<Fragment> mFragments = new ArrayList<>(); 
     private final List<String> mFragmentTitles = new ArrayList<>(); 

     public Adapter(FragmentManager fm) 
     { 
      super(fm); 
     } 

     public void addFragment(Fragment fragment, String title) 
     { 
      mFragments.add(fragment); 
      mFragmentTitles.add(title); 
     } 

     @Override 
     public Fragment getItem(int position) 
     { 
      return mFragments.get(position); 
     } 

     @Override 
     public int getCount() 
     { 
      return mFragments.size(); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) 
     { 
      return mFragmentTitles.get(position); 
     } 
    } 

    @Override 
    public void onBackPressed() 
    { 
     super.onBackPressed(); 
     overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
    } 
} 

回答

4

如果通過代碼添加的片段,當你添加您的片段用一個代碼字符串,用findFragmentByTag()改爲:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag"); 
+0

我加了這樣的片段 Adapter adapter = new Adapter(getSupportFragmentManager()); adapter.addFragment(new TwitchChannel(),「channel」); adapter.addFragment(new TwitchStream(),「stream」); adapter.addFragment(new TwitchFeed(),「feed」); adapter.addFragment(new TwitchVideo(),「video」); 所以我不能使用findFragmentByTag() –