第二次打开时 Viewpager 为空

Viewpager empty when opened second time

我正在创建一个应用程序来创建销售订单。它有 1 个片段来显示销售订单列表 (ListFragment),1 个片段来显示销售订单的表单 (FormFragment) 并且在内部,有一个包含 2 个片段的 viewpager:HeaderFragment 和 ItemsFragment。当我第一次访问它时,它工作正常,header 并且项目显示输入字段。但是当我第二次访问它时,header 和项目都是空的。

以下是我如何从 ListFragment 打开 FormFragment:

public class ListFragment extends Fragment {

    ListView listViewSO;
    Bundle bundle;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ((MainActivity)getActivity()).getSupportActionBar().setTitle("SO LIST");

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_list_so, container, false);

        bundle = getArguments();

        listViewSO = root.findViewById(R.id.list_view_so);

        listViewSO.setOnItemClickListener(SOListItemClick);

        return root;

    }

    private AdapterView.OnItemClickListener SOListItemClick = new AdapterView.OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            SalesOrder salesOrder = (ListFragment.this).adapter.getItem(i);

            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            bundle.putParcelable("salesorder", salesOrder);

            FormFragment FormFragment = new FormFragment();
            FormFragment.setArguments(bundle);

            fragmentTransaction.replace(R.id.fragment_container, FormFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    };
}

这是我的 FormFragment

public class FormFragment extends Fragment {
    Bundle bundle;
    SalesOrder salesorder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ((MainActivity)getActivity()).getSupportActionBar().setTitle("EDIT SO");

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_so_form, container, false);

       ((MainActivity)getActivity()).drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        ((MainActivity)getActivity()).toggle.setDrawerIndicatorEnabled(false);
        ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        bundle = getArguments();
        salesorder = bundle.getParcelable("salesorder");

        viewPager = (ViewPager) root.findViewById(R.id.view_pager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) root.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        return root;
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getActivity().getSupportFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        adapter.addFragment(new HeaderFragment(), "Header");
        adapter.addFragment(new ItemFragment(), "Items");
        adapter.addBundle(bundle);
        viewPager.setAdapter(adapter);
    }
}

这是我的适配器:

public class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public SectionsPageAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    public void addBundle(Bundle bundle) {
        for(int i = 0; i < mFragmentList.size(); i++) {
            mFragmentList.get(i).setArguments(bundle);
        }
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

最后,这是我的 HeaderFragment

public class VisitationSOHeaderFragment extends Fragment {

    Bundle bundle;
    SalesOrder salesorder;

    TextInputLayout textInputSoCode, textInputSoDate;
    ProgressBar loading;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_visitation_so_header, container, false);

        bundle = getArguments();
        salesorder = bundle.getParcelable("salesorder");

        loading = root.findViewById(R.id.loading);
        textInputSoCode = root.findViewById(R.id.text_input_so_code);
        textInputSoDate = root.findViewById(R.id.text_input_so_date);

        textInputSoCode.getEditText().setText(salesorder.getSoCode());
        textInputSoDate.getEditText().setText(salesorder.getSoDate());

        return root;
    }
}

这是怎么回事? viewpager设置有问题吗?任何帮助将不胜感激。

不要使用 getActivity().getSupportFragmentManager() 获取 FragmentPagerAdapter 的片段管理器。只需使用 getChildFragmentManager 方法即可。