交换viewPager中的3个片段后,第一个片段未恢复编辑框值...我在viewpager中具有3个片段,并单击第3个片段选项卡并交换回第1个片段..编辑文本填充空白..plz提供一些解决方案..代码在这儿..
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_item_master, container, false);
viewPager = (ViewPager)v. findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)v. findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return v;
}
private void setupViewPager(ViewPager viewPager) {
// ViewPagerAdapter adapter = new ViewPagerAdapter(mActivity.getSupportFragmentManager());
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new ItemInfoFragment(), "Item Info");
adapter.addFragment(new ItemExtraInfoFragment(), "Extra Info");
adapter.addFragment(new OrderHistoryFragment(), "Order History");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
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);
}
最佳答案
仅当片段数量较少时才建议这样做。您可以使用yourViewPager.setOffscreenPageLimit(numberOfFragmentsInViewPager);
这样做是因为每次刷卡都不会创建新的片段,因此数据得以保留。如果您不设置此属性,则默认值为1。