ViewPage Fragment disappear when reload again

前端 未结 5 1248
执笔经年
执笔经年 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 _fragments;
    
    public MyPagerAdapter(FragmentManager activity) {
        super(activity);
    
        this._fragments = new ArrayList();
    }
    
    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!

提交回复
热议问题