[英]Call Fragment method from activity using FragmentPagerAdapter
Originally I had a single fragment loaded in my MainActivity
class. 最初,我在
MainActivity
类中加载了一个片段。 I used the following in 我在
onCreate
onCreate
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(new TimeTableFragment(), TIMETABLEFRAGMENT_TAG)
.commit();
}
and in 和在
onResume
onResume
if(*setting has changed*){
TimeTableFragment tf = (TimeTableFragment) getSupportFragmentManager()
.findFragmentByTag(TIMETABLEFRAGMENT_TAG);
if (null != tf) {
tf.onStudentIdChanged();
}
}
The onStudentIdChanged()
method would refresh the fragment data as preference settings have changed. onStudentIdChanged()
方法将在首选项设置已更改时刷新片段数据。
I have now refactored this activity to use FragmentPagerAdapter
and load two different fragments in tabs. 现在,我已经将此活动重构为使用
FragmentPagerAdapter
并在选项卡中加载两个不同的片段。 Both fragments are instances of the same TimetableFragment
class, but with different parameters passed when creating. 这两个片段都是相同的
TimetableFragment
类的实例,但是在创建时传递了不同的参数。
FragmentPagerAdapter methods
FragmentPagerAdapter方法
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a Fragment
switch (position) {
case 1:
case 2:
return TimeTableFragment.newInstance(position);
case 0:
//todo add the single day here
return TimeTableFragment.newInstance(position);
}
return null;
}
TimetableFragment Method
时间表片段方法
public static TimeTableFragment newInstance(int sectionNumber) {
TimeTableFragment fragment = new TimeTableFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
Now that this is working, in onResume
of my MainActivity
I am no longer able to access the Fragments using findFragmentByTag
, as they are not added in this way during onCreate
and I can no longer call the onStudentIdChanged
method. 现在,这已经可以了,在我的
MainActivity
onResume
,我不再能够使用findFragmentByTag
访问Fragment,因为在onCreate
期间不会以这种方式添加这些片段,并且我无法再调用onStudentIdChanged
方法。
How, for all created instances of the Timetable Fragment can I call this method and have the data updated / refreshed? 如何为所有创建的时间表片段实例调用此方法并更新/刷新数据?
In your FragmentPagerAdapter
imprementation, add private SparseArray<Fragment> mFragments = new SparseArray<Fragment>();
在您的
FragmentPagerAdapter
实例中,添加private SparseArray<Fragment> mFragments = new SparseArray<Fragment>();
Modify your getItem method as follows: 修改您的getItem方法,如下所示:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a Fragment
Fragment fragment = null;
switch (position) {
case 1:
case 2:
fragment = mFragments.get(position);
if (fragment == null) {
fragment = TimeTableFragment.newInstance(position);
mFragments.append(position, fragment)
}
break;
case 0:
//todo add the single day here
fragment = mFragments.get(position);
if (fragment == null) {
fragment = TimeTableFragment.newInstance(position);
mFragments.append(position, fragment)
}
break;
}
return fragment;
}
Now when you can get instance of fragment by position. 现在,您可以按位置获取片段实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.