25

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

In this ViewPager I have 3 Fragments that each of them contains a TableLayout that gets populated programmaticly using a 2dArrayList.

on a click of a button in the FragmentActivity layout, I filter the data in the 2dArrayList and basically need to recreate all 3 Fragments with the new dataset (run the onCreateView method in all of them).

So I create an onClickListener for this button:

 private class SlicerSelectedOnClickListener implements OnClickListener {

    private ODSharedSlicer slicer;

    public SlicerSelectedOnClickListener(ODSharedSlicer slicer) {
        super();
        this.slicer = slicer;
    }

    public void onClick(View v) {
        String slicerValue = ((Button) v).getText().toString();
        Log.d(TAG,"Slicer value: "+ slicerValue +" chosen");
        application.currentReport.getODSharedSlicers().get(0).getSharedSlicerSelectedMemebers().add(slicerValue);

        ViewGroup parent = (ViewGroup) ((Button) v).getParent();

        for (int i = 0; i < parent.getChildCount(); i++) 
        {
            if ((parent.getChildAt(i) != v) && (parent.getChildAt(i) instanceof ToggleButton))
            {
                ((ToggleButton) parent.getChildAt(i)).setChecked(false);
            }
        }

        mPagerAdapter.notifyDataSetChanged();
    }
}

As you can see I have a mPagerAdapter.notifyDataSetChanged(); As suggested here:

How to recreate fragment with viewpager intentionally?

at the end of this method, So I expect the adapter to recreate all the Fragments, but the onCreateView from any of the fragments never runs.

Can any one suggest what am I doing wrong. any assistance would be appreciated.

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • 2
    Did you override the `getItemPosition()` method in the adapter to return `POSITION_NONE`? – user May 21 '13 at 13:57
  • 1
    No, I didn't. should I do that to use the notifyDataSetChanged() method in ViewPager? – Emil Adz May 21 '13 at 13:59
  • It wouldn't take you much to test it:) – user May 21 '13 at 14:05
  • @Luksprog, I tried it but for some reason POSITION_NONE can't resolve to a variable. in which class does this constant sits? I can't find it? – Emil Adz May 21 '13 at 14:10
  • http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#POSITION_NONE – user May 21 '13 at 14:14
  • @Luksprog, stupid me. Don't know how I missed it. checking right now. – Emil Adz May 21 '13 at 14:18
  • @Luksprog, I have added the getItemPosition() method with return POSITION_NONE. like describe here: http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view/7287121#7287121 but it didn't change a thing. – Emil Adz May 21 '13 at 14:29
  • can you try with mViewPager.setApdater(mPagerAdapter) – Blackbelt May 21 '13 at 14:47
  • I made a mistake in placing the getItemPosition as part of the FragmentActivity and not as part of the adapter. As soon as I changed that it works as expected. Thanks. – Emil Adz May 21 '13 at 15:56

4 Answers4

24

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

Zain
  • 37,492
  • 7
  • 60
  • 84
Ziad Gholmish
  • 861
  • 1
  • 9
  • 10
  • I m using `FragmentStatePagerAdapter` and from a Button OnclickListener() I have Called `mPagerAdapter.notifyDataSetChanged();` and also Overriding ..But no effect on ViewPager.its not recreating – Hemant Shori May 01 '15 at 08:46
  • did you override @Override public int getItemPosition(Object object) { return POSITION_NONE; } – Ziad Gholmish May 03 '15 at 06:34
  • yes i did. `notifyDataSetChanged();` is still not recreating the fragments again – Hemant Shori May 04 '15 at 04:19
  • hi hemant, if you still have the problem you can make the adapter global object and then when change call adapter.notifydatasetchanged(); the adapter should be global to to update adapter give this a try – Ziad Gholmish May 07 '15 at 07:04
  • i just want to change the backgrounds of the fragments by recreating it but i did it using a array of view and changing background one by one of each fragment . and my adapter is already global ..so – Hemant Shori May 07 '15 at 09:10
  • I am using exactly this solution but it is working only for Nougat and not for all previous versions. What's the problem ? – FRK Apr 11 '17 at 15:56
12

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;
}  
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
2

getActivity.recreate() will recreate activity hence all fragments

Afzal Khan
  • 336
  • 3
  • 16
0

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;
    }
}
ceph3us
  • 7,326
  • 3
  • 36
  • 43