我有一個使用ViewPagerIndicator
的活動。 我已經實現了一個延伸FragmentPagerAdapter
的類PagerAdapter
,以設置頁面上的片段。如何在使用ViewPagerIndicator的活動中獲取當前片段(JakeWharton Library)
在我的活動,這是我的代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new PagerAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
我的活動也有一個菜單選項,以選擇 「A」。當用戶點擊選項菜單並選擇「A」時,我的活動想要調用當前片段的方法。但是,活動如何知道currentFragment
?
我想這一點:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.A:
getCurrentFragment.Method(); //Method is the method that want to execute
// ...
}
}