0

먼저 문제/질문을 재현 해 보겠습니다.SectionsPageAdapter with FragmentPageAdapter

내 활동은 SectionPageAdapter 및 FragmentPageAdapter를 사용하여 3 탭이있는 TabbedActivity입니다.

이 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; 
    } 

답변

0

얘들 아 내가 내 뇌를 갱신했을;

그냥 배열로 조각에서 인스턴스를 저장하고 나중에 사용할 수 있습니다). 나는 SectionPageAdapter에서 이것을 만들었고 instantiateItem() 메쏘드를 보았다.

public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

    private SparseArray<Fragment> registeredFragments = new SparseArray<>(); 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 


    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     Fragment fragment = (Fragment) super.instantiateItem(container, position); 
     Integer fragmentId = fragment.getArguments().getInt("fragment_id"); 
     registeredFragments.put(fragmentToPageMap.get(fragmentId), fragment); 

     return fragment; 
    } 

    public Fragment getRegisteredFragment(int position) { 
     return registeredFragments.get(position); 
    } 

    public boolean registeredFragmentExists(int position) { 
     if(registeredFragments.size() >= position) { 
      if(registeredFragments.get(position) != null) { 
       return true; 
      } 
     } 
     return false; 
    } 


    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return TravelInfoActivity.PlaceholderFragment.newInstance(position); 
    } 

    @Override 
    public int getCount() { 
     return maxTabs; 
    } 

} 

나중에는

mSectionsPagerAdapter.getRegisteredFragment(position); 
fragment.getActivity().findViewById(id); 
와 조각 rootView에 액세스 할 수 있습니다