我在viewpager中有3个标签,我正在使用从FragmentPagerAdapter扩展的ViewPagerAdapter。我想在收到新的json文件时刷新标签! json文件将在片段(标签)的onCreatView方法中解析,但正如我所说,我不知道如何刷新标签以从json文件中获取新数据并再次解析它! 我想将刷新代码放在主要活动的onResume方法中 PLZ帮助我,谢谢先进
private void designOfTabLayout() {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
viewPager = (ViewPager) findViewById(R.id.viewpager);
if(viewPager!=null){
viewPager.setOffscreenPageLimit(4);
setupViewPager(viewPager);
}
tabLayout = (TabLayout) findViewById(R.id.tabs);
if (tabLayout != null) {
tabLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
tabLayout.setupWithViewPager(viewPager);
this.setTabIcons();
this.changeTabsFont();
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
teamsFragment=new TeamsFragment();
dutiesFragment=new DutiesFragment();
projectsFragment=new ProjectsFragment();
adapter.addFragment(dutiesFragment, "Duties");
adapter.addFragment(projectsFragment, "Projects");
adapter.addFragment(teamsFragment, "Teams");
viewPager.setAdapter(adapter);
// For Opening Team Tab First :
viewPager.setCurrentItem(2);
}