当我移动到另一个标签时,每次创建新标签。 我从服务器获取tabname和catid,
private void setTabTitle() {
Final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mviewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
viewPager.setOffscreenPageLimit(jSonTab.size());
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(mviewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mviewPager.setCurrentItem(tab.getPosition()); // Get current clicked Tab Position
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
});
}
public Parcelable saveState() {
return null;
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return MainFragment.newInstance(CatId.get(position), tabName.get(position));
}
@Override
public int getCount() {
return tabName.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabName.get(position);
}
}
当我在第一个标签中更改任何内容,然后转到另一个标签时,第一个标签会刷新并创建新实例, 每次调用服务器来获取特定选项卡的详细信息
答案 0 :(得分:0)
尝试设置viewPager.setOfscreenPagelimit(noOfPagesInViewPager);
类ViewPagerAdapter扩展FragmentStatePagerAdapter { Mainfragment mainfragment;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
mainfragment=new Mainfragment();
}
@Override
public Fragment getItem(int position) {
return mainfragment(CatId.get(position), tabName.get(position));
}
@Override
public int getCount() {
return tabName.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabName.get(position);
}
}