更改viewpager的适配器时,MaterialTabs没有刷新

时间:2016-03-12 16:42:05

标签: java android android-viewpager android-tabs

我正在尝试重新加载viewpager的适配器,更改一些参数。 这是我的MainActivity的代码:

private void createTabs() {

    String titles[] = new String[] {getResources().getString(R.string.man),
                                    getResources().getString(R.string.woman)};

    int icons[] = new int[] {R.mipmap.man, R.mipmap.woman};

    Main_Fragments_Adapter adapter = new Main_Fragments_Adapter(getSupportFragmentManager(),
                                                                this,
                                                                titles,
                                                                icons,
                                                                new Coordinates(this.latitude, this.longitude),
                                                                searchRange,
                                                                cutTypes);

    this.vpPager.setAdapter(adapter);

    this.mtTabs.setViewPager(this.vpPager);
}

适配器的代码:

public class Main_Fragments_Adapter extends FragmentPagerAdapter implements MaterialTabs.CustomTabProvider {

private String[] TITLES;
private int[] ICONS;
private FragmentActivity activity;
private Coordinates coordinates;
private int searchRange;
private String cutTypes;

public Main_Fragments_Adapter(FragmentManager fm, FragmentActivity activity, String titles[], int icons[], Coordinates coordinates, int searchRange, String cutTypes) {
    super(fm);
    this.activity = activity;
    this.TITLES = titles;
    this.ICONS = icons;
    this.coordinates = coordinates;
    this.searchRange = searchRange;
    this.cutTypes = cutTypes;
}

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

@Override
public int getCount() {
    return TITLES.length;
}

@Override
public Fragment getItem(int position) {
    Bundle bundle = new Bundle();
    bundle.putDouble("latitude", coordinates.getLatitude());
    bundle.putDouble("longitude", coordinates.getLongitude());
    bundle.putInt("searchRange", searchRange);
    bundle.putString("cut_types", cutTypes);
    switch (position) {
        case 0:
        default:
            Main_Fragment_Man fragment_man = new Main_Fragment_Man();
            fragment_man.setArguments(bundle);
            return fragment_man;
        case 1:
            Main_Fragment_Woman fragment_woman = new Main_Fragment_Woman();
            fragment_woman.setArguments(bundle);
            return fragment_woman;
    }
}

@Override
public View getCustomTabView(ViewGroup parent, int position) {

    TextView textView = new TextView(this.activity);
    textView.setText(this.TITLES[position]);
    textView.setTextSize(24f);
    textView.setTextColor(this.activity.getResources().getColor(R.color.white));
    textView.setPadding(15, 0, 0, 0);

    ImageView imageView = new ImageView(this.activity);
    imageView.setImageDrawable(this.activity.getResources().getDrawable(ICONS[position]));

    LinearLayout linearLayout = new LinearLayout(this.activity);
    linearLayout.setGravity(Gravity.CENTER);
    linearLayout.addView(imageView);
    linearLayout.addView(textView);

    return linearLayout;
}

}

有自定义对话框,用于接收适配器的参数(searchRange和cutTypes)并调用createTabs()函数。但是,在那之后参数被改变,片段接收了先前的参数。我怎么解决呢?

0 个答案:

没有答案