如何手动刷新ViewPager中的片段

时间:2015-04-08 10:13:53

标签: android android-viewpager android-pageradapter

我正在使用viewPager和pagerAdapter开发我的PagerActivity。我内部的处理程序应该强制刷新,但是如何在寻呼机内刷新一个片段? 我已尝试使用notifyDataSetChangedsetOffscreenPageLimit(0),但这些方法无效。

public class CustomPagerAdapter extends SmartFragmentStatePagerAdapter {
private static final String TAG = "CustomPagerAdapter";
private static int NUM_ITEMS = 4;

private Context context;

public void setContext(Context context) {
    this.context = context;
}

public CustomPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public int getCount() {
    return NUM_ITEMS;
}

@Override
public Fragment getItem(int position) {
    Log.d(TAG, "getItem " + position);
    switch (position) {
        case 0:
            return UserInfoFragment.newInstance();
        case 1:
            return ChooseJourneyPatternFragment.newInstance();
        case 2:
            return VehicleJourneyFragment.newInstance();
        case 3:
            return SyncCarFragment.newInstance();
        default:
            return null;
    }
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return context.getResources().getString(R.string.pagerUserInfo);
        case 1:
            return context.getResources().getString(R.string.pagerPattern);
        case 2:
            return context.getResources().getString(R.string.pagerVehicleJourney);
        case 3:
            return context.getResources().getString(R.string.pagerSync);
        default:
            return null;
    }
}

1 个答案:

答案 0 :(得分:0)

我正在使用HashMap解决并使用fragmentManager检索有效片段,然后在活动的pageSelected侦听器中调用getFragment方法。这里有适配器的完整代码

public class CustomPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = "CustomPagerAdapter";
private static int NUM_ITEMS = 4;

private Context context;

private Map<Integer, String> fragmentTags;
private FragmentManager fragmentManager;

public void setContext(Context context) {
    this.context = context;
}

public CustomPagerAdapter(FragmentManager fm) {
    super(fm);
    fragmentTags = new HashMap<Integer, String>();
    fragmentManager = fm;
}



@Override
public int getCount() {
    return NUM_ITEMS;
}

@Override
public Fragment getItem(int position) {
    Log.d(TAG, "getItem " + position);
    switch (position) {
        case 0:
            return UserInfoFragment.newInstance();
        case 1:
            return SyncCarFragment.newInstance();
        case 2:
            return ChooseJourneyPatternFragment.newInstance();
        case 3:
            return VehicleJourneyFragment.newInstance(context);
        default:
            return null;
    }
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
             return context.getResources().getString(R.string.pagerUserInfo);
        case 1:
            return context.getResources().getString(R.string.pagerSync);
        case 2:
            return context.getResources().getString(R.string.pagerPattern);
        case 3:
            return context.getResources().getString(R.string.pagerVehicleJourney);
        default:
            return null;
    }
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Object obj =  super.instantiateItem(container, position);
    if (obj instanceof Fragment) {
        Fragment f = (Fragment) obj;
        String tag = f.getTag();
        fragmentTags.put(position, tag);
    }
    return obj;
}

public Fragment getFragment(int position) {
    String tag = fragmentTags.get(position);
    if (tag == null)
        return null;

    return fragmentManager.findFragmentByTag(tag);
}