爲了做到這一點,你應該創建一個FragmentPagerAdapter
,因爲他們所創建的保存到你的頁面的引用。事情是這樣的:
public class MyPagerAdapter extends FragmentPagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return MyListFragment.newInstance(TAB_NAME_A);
case 1:
return MyListFragment.newInstance(TAB_NAME_B);
case 2:
return MyListFragment.newInstance(TAB_NAME_C);
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return TOTAL_PAGES;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
您可以訪問特定頁面這樣的:
((MyListFragment) myPagerAdapter.getRegisteredFragment(0)).updateUI();
凡updateUI()是更新頁面上的名單,並呼籲notifyDataSetChanged()
一個自定義的方法。
來源
2014-07-23 01:42:35
JDJ