ViewPage Fragment disappear when reload again

前端 未结 5 1252
执笔经年
执笔经年 2021-02-06 05:20

The following is layout

test.xml





        
相关标签:
5条回答
  • 2021-02-06 05:32

    try using this:

    in the constructor of your adapter change this :

    public class MyPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> _fragments;
    
    public MyPagerAdapter(FragmentManager activity) {
        super(activity);
    
        this._fragments = new ArrayList<Fragment>();
    }
    
    public void add(Fragment fragment) {
        this._fragments.add(fragment);
    }
    
    @Override
    public Fragment getItem(int position) {
        return this._fragments.get(position);
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        return "Test";
    }
    
    @Override
    public int getCount() {
        return 1;
    }
    
    
    }
    

    and when you want to create the adapter send getChildFragmentManager() to its constructor

    public class HomeFragment extends Fragment {
    private MyPagerAdapter _adapter;
    
    public HomeFragment() {
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.common_view_pager_layout, container, false);
        ViewPager pager = (ViewPager) view.findViewById(R.id.MainViewerPage);
    
        this._adapter = new MyPagerAdapter(getChildFragmentManager());
        this._adapter.add(new DetailFragment());
    
    
        pager.setAdapter(this._adapter);
    
    
        return view;
    }
    }
    

    I test it and it works, any problem comment it.

    Good Luck!

    0 讨论(0)
  • 2021-02-06 05:34

    In your Fragment class, in the onCreate() method, you have to call setRetainInstance(true) like this:

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);     
        setRetainInstance(true);
    }
    

    This tells the FragmentManager to keep your fragments on reloads. In addition, check your manifest file doesn't have any

    android:screenOrientation
    

    config changes where you are basically saying you will handle reloads due to config changes yourself.

    0 讨论(0)
  • 2021-02-06 05:36

    In PagerFragment 's onResume() add this:

    @Override
    public void onResume() {
        super.onResume();
    
        for (Fragment fragment : getFragmentManager().getFragments()) {
            if (fragment instanceof Tab1Fragment || fragment instanceof Tab2Fragment) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.detach(fragment);
                ft.attach(fragment);
                ft.commit();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 05:37

    Ensure you pass the adapter instance the child fragment manager, not the fragment manager.

    Like this:

    this._adapter = new PagerAdapter(getChildFragmentManager());

    0 讨论(0)
  • 2021-02-06 05:50

    Try to add this lines in your code:

    public int getItemPosition(Object object) {
       return POSITION_NONE;
    }
    

    and Add the below lines

    pager.setOnPageChangeListener(new OnPageChangeListener() {
         @Override
         public void onPageSelected(int position) {
              ... anything you may need to do to handle pager state ...
              adapter.notifyDataSetChanged(); //this line will force all pages to be loaded fresh when changing between fragments
         }
    

    Under

    pager.setAdapter(adapter);
    

    Hope this may help you!

    0 讨论(0)
提交回复
热议问题