我有一个活动,将FragmentPagerAdapter与其视图结合在一起。创建视图后,我需要运行此方法来更改textviews(在FragmentPagerAdapter中定义):
public void onPageSelectedCustom(int position) {
int ValidView;
FragmentManager myFrag = passedAct.getSupportFragmentManager();
String name = makeFragmentName(mViewPager.getId(), position);
ArrayListFragment f = (ArrayListFragment) myFrag.findFragmentByTag(name);
if(f!=null) {
ValidView= 1;
View fragView = f.getView();
View tv = fragView.findViewById(R.id.text);
((TextView)tv).setText("VALID FRAGMENT #" + position);
Log.d("Fragment", "fragment is valid: " + position);
} else {
ValidView= 0;
Log.d("Fragment", "fragment is not valid: " + position);
}
}
我尝试在FragmentPagerAdapter的构造函数中调用onPageSelectedCustom()
但是f
始终为null,因此从不执行代码。我尝试使用活动onCreate()
,onResume()
和onPostResume()
,但在创建Fragment视图之前都会调用它们。
任何出错的想法?