0
首先讓我試着重現我的問題/問題。SectionsPageAdapter with FragmentPageAdapter
我的活動是帶有3個選項卡的TabbedActivity,使用SectionPageAdapter和FragmentPageAdapter完成。
在這3個選項卡上,用戶輸入數據。只需滑動,用戶可以更改選項卡。在用戶填寫完所有數據後,他必須點擊一個FloatingActionButton來保存數據。
現在有我的問題我不知道如果我可以訪問這些3填充數據的片段? 我是否可以加載所有3個碎片以填充數據,讀取數據並保存它,或者在更改頁面之前是否必須處理PageChangeListenEvent並保存數據?
代碼:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TravelInfoActivity.PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return maxTabs;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_FRAGMENT_ID = "fragment_id";
private static int REQUEST_CODE_PLACE = 1;
private View currentRootView;
private View currentClickedView;
@SuppressLint("UseSparseArrays")
private HashMap<Integer, View> hMap = new HashMap<>();
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static TravelInfoActivity.PlaceholderFragment newInstance(int sectionNumber) {
TravelInfoActivity.PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
int fragmentLayout;
switch (sectionNumber) {
case 1:
fragmentLayout = R.layout.fragment_accommodation;
break;
case 2:
fragmentLayout = R.layout.fragment_visited_places;
break;
case 3:
fragmentLayout = R.layout.fragment_additional_info;
break;
default:
fragmentLayout = R.layout.fragment_accommodation;
}
args.putInt(ARG_FRAGMENT_ID, fragmentLayout);
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(getArguments().getInt(ARG_FRAGMENT_ID), container, false);
currentRootView = rootView;
switch (getArguments().getInt(ARG_FRAGMENT_ID)) {
// switch handling here, not relevant
}
return rootView;
}