我有一個活動,其中包括三個片段的ViewPager。在第二個片段中,我也有兩個不同片段的ViewPager。 這是我MainActivityPagerAdapter這MainActivity與ViewPager連接:重新加載ViewPager內部的片段,其中的另一個片段inViewPager
public class MainActivityPagerAdapter extends FragmentStatePagerAdapter implements LoginView.LoginStateChangeListener {
private static final String TAG = MainActivityPagerAdapter.class.getName();
private MapFragment mapFragment = new MapFragment();
private UserProfileFragment userProfileFragment = new UserProfileFragment();
private CompanyProfileFragment CompanyProfileFragment = new DiveCenterProfileFragment();
private NotificationsFragment notificationsFragment = new notificationsFragment();
public MainActivityPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return mapFragment;
case 1:
return notificationsFragment;
case 2:
switch (MyApplication.getInstance().getSharedPreferenceHelper().getActiveUserType()) {
case DIVECENTER:
return companyProfileFragment;
case DIVER:
case INSTRUCTOR:
return userProfileFragment;
case NONE:
return userProfileFragment;
}
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public int getItemPosition(Object object) {
Log.i(TAG, "getItemPosition");
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
在第二片段這是我的傳呼機適配器ViewPager:
public class NotificationsPagerAdapter extends FragmentStatePagerAdapter {
private PersonalNotificationsFragment personalNotificationsFragment = new PersonalNotificationsFragment();
private ActivityNotificationsFragment activityNotificationsFragment = new ActivityNotificationsFragment();
public NotificationsPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return personalNotificationsFragment;
case 1:
return activityNotificationsFragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
public void setPersonalNotificationsFragment(PersonalNotificationsFragment personalNotificationsFragment) {
this.personalNotificationsFragment = personalNotificationsFragment;
}
public void setActivityNotificationsFragment(ActivityNotificationsFragment activityNotificationsFragment) {
this.activityNotificationsFragment = activityNotificationsFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
因此,有在應用兩種用戶類型:
- 簡單用戶
- 公司
當用戶更改他的類型時,我需要刷新MainActivity中的所有片段,包括第二個片段中的片段。爲此,我爲MainActivityViewPager使用notifyDataSetChanged(),它可以正常工作,但第二個片段中的片段不會更新(onCreateView不會調用,而我只能看到空視圖)。 可能是什麼問題?
THX,這對我的工作! – Lashket