How to recreate all fragments in ViewPager:

后端 未结 4 1737
既然无缘
既然无缘 2020-12-20 11:13

I have this problem, I have a FragmentActivity with a ViewPager and a ViewPagerAdapter.

In this ViewPager I have 3 Fragm

相关标签:
4条回答
  • 2020-12-20 11:53

    getActivity.recreate() will recreate activity hence all fragments

    0 讨论(0)
  • 2020-12-20 11:58

    As been suggested here by @Luksprog, changing my adapter class to this:

    public class ViewPagerAdapter extends FragmentPagerAdapter 
    {
    private List<Fragment> fragments;
    
    /**
     * @param fm
     * @param fragments
     */
    public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
     */
    
    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.view.PagerAdapter#getCount()
     */
    
    @Override
    public int getCount() {
        return this.fragments.size();
    }
    
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }  
    }
    

    Fixed my issue.

    what I did was to add this method:

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }  
    
    0 讨论(0)
  • 2020-12-20 12:15

    on @Emil-adz solution we can go little ahead because we got fragment listy in my case called _fragments we can return fragment position like this:

    @Override
    public int getItemPosition(Object object) {
        /** check if object is fragment class */
        if (object.getClass().isAssignableFrom(Fragment.class)) {
            /** cast object to fragment */
            Fragment customFragment = (Fragment)object;
            /** return fragment position from our list */
            return _fragments.indexOf(customFragment);
        } else {
            /** else re-create */
            return POSITION_NONE;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 12:19

    for those who still face the same problem which I faced the same problem when I have ViewPager with seven fragment. the default for these fragments to load the English content from the service but the problem here that I want to change the language from settings activity and after finish settings activity I want ViewPager in main activity to refresh the fragment to match the language selection from the user and load the Arabic content if user choose Arabic here what I did to work from the first trr :D

    1. you must use FragmentStatePagerAdapter .*

    1. mainActivity I overrided the onResume and did the following

       if (!(mPagerAdapter == null)) {
      
               mPagerAdapter.notifyDataSetChanged();
      
      
           }
      
    2. I ovveride the getItemPosition() in mPagerAdapter and make it return POSITION_NONE.

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

    works like charm

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