Update Fragment from ViewPager
Asked Answered
U

16

63

I have an Activity with a ViewPager. I also have a Dialog that will retrieve a list with some items that the user will choose. Now, how can I update the Fragment where the Items are supposed to display as new views attached to this Fragment?

Here is my ViewPager's adapter:

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new Fragment1();

        default:
            return null;
        }
    }
}

Now, here is the Fragment:

public class Fragment1 extends Fragment{

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public ViewGroup onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    ViewGroup view = (ViewGroup)inflater.inflate(R.layout.screen1, container, false);
    }
    return view;
}

}

This is the method that I want to use to add Items to my Fragment

private void addItem() {

    // Instantiate a new "row" view.

    final ViewGroup[] newViews = new ViewGroup[selectedItems.length];


    for(int i = 0; i < selectedItems.length; i++){
        newViews[i] = (ViewGroup) LayoutInflater.from(this).inflate(
                    R.layout.animated_layout_item, mContainerView, false);

        // Set the text in each new row.
        ((TextView) newViews[i].findViewById(android.R.id.text1)).setText(selectedItems[i]);

        // Because mContainerView has android:animateLayoutChanges set to true,
        // adding this view is automatically animated.

            mContainerView.addView(newViews[i], 0);


        // Set a click listener for the "X" button in the row that will remove the row.
        OnClickListener listener = new MyAddItemListener(newViews[i], mContainerView, this);
        newViews[i].findViewById(R.id.delete_button).setOnClickListener(listener);

    }


}
Upset answered 6/8, 2013 at 18:43 Comment(3)
Why do you have "ViewGroup view = view = " in Fragment1 onCreateView?Vesica
It's a typo. Let me correct it. Thanks for noticing it.Upset
github.com/pchauhan/RefreshFragementViewPagerDisbelief
F
139

Update Fragment from ViewPager

You need to implement getItemPosition(Object obj) method.

This method is called when you call

notifyDataSetChanged()

on your ViewPagerAdaper. Implicitly this method returns POSITION_UNCHANGED value that means something like this: "Fragment is where it should be so don't change anything."

So if you need to update Fragment you can do it with:

  • Always return POSITION_NONE from getItemPosition() method. It which means: "Fragment must be always recreated"
  • You can create some update() method that will update your Fragment(fragment will handle updates itself)

Example of second approach:

public interface Updateable {
   public void update();
}


public class MyFragment extends Fragment implements Updateable {

   ...

   public void update() {
     // do your stuff
   }
}

And in FragmentPagerAdapter you'll do something like this:

@Override
public int getItemPosition(Object object) {
   MyFragment f = (MyFragment ) object;
   if (f != null) {
      f.update();
   }
  return super.getItemPosition(object);
}

And if you'll choose first approach it can looks like:

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

Note: It's worth to think a about which approach you'll pick up.

Fleetwood answered 6/8, 2013 at 19:8 Comment(20)
Thanks for your answer. But, it is not updating it! Could you take a look at it in this Gist that I just created? I chose option 2.Upset
Also, when I try to call notifyDataSetChanged(), it tells me that is undefined for the type ViewPagerUpset
@LuisLavieri you should call it like yourPager.getAdapter().notifyDataSetChanged()Fleetwood
Thanks @Sajmon, but it is throwing a Null Pointer Exception when I call it. :( What do you think it might be?Upset
@LuisLavieri you need to call it after you instantiated adapter and viewpager and added adapter to pager.Fleetwood
Sure, I'm calling it in myDialogListener. But, after instantiated them. Like this: mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mAdapter);Upset
The ViewPager works fine at first, but the NPE is thrown after I click "OK" in my ListenerUpset
Beautiful. After hard work, it finally worked. Thanks buddy. The problem was my mContainerView; it wasn't instantiated properly.Upset
let us continue this discussion in chatUpset
+1 for @Override public int getItemPosition(Object object) { return POSITION_NONE; }Kyungkyushu
I am using the second method, but I my fragment is not updating... From debug, I can clearly see the the adapter for my fragment's listview is updated. The one issue remains is that the fragment in the viewpager is not updated Any idea?Libove
@Sajmon I have 2 fragments in FragmentPagerAdapter. But I want to recreate only One fragment onResume() of the MainActivity. Any idea on how to recreate one fragment out of the two?Flickertail
It's a really elegant solution for me to update fragment, I select "update" solution since that I want to reuse fragment without create a new fragment instance every time. Thanks very very much :)Lanner
@Sajmon I follow your steps but has a strange problem: getItemPosition() called more than one times after notifyDataSetChanged() called. This cause f.update() called many times too, but it should be called once! Would you please give me some tip, thanks.Lanner
This works best - not returning return super.getItemPosition(object) but returning POSITION_NONE Thank youWolfhound
this is biggest problem of viewpager adapterCameo
I have 5 fragment in FragmentPagerAdapter. I have tried both solution but none of worked for me as MyFragment called multiple times every time i swipe. Please help if anyone found solution.Winola
it's working great :) btw can you please explain, what does return POSITION_NONE do ? and why getItemPosition ?Latchkey
Small suggestion: For getItemPosition in the FragmentPagerAdapter, perhaps better to cast to Updateable instead of MyFragment, i.e.: MyFragment f = (MyFragment) object; -> Updateable f = (Updateable) object;?Gadmann
Whats the ViewPager2 equivalent?Conlin
B
33

I faced problem some times so that in this case always avoid FragmentPagerAdapter and use FragmentStatePagerAdapter.

It work for me. I hope it will work for you also.

Befuddle answered 27/11, 2014 at 6:8 Comment(1)
thank you :) I could create dynamic tab using this adapterKendall
L
15

I implement a app demo following Sajmon's answer. Just for anyone else who search this question.

You can access source code on GitHub

enter image description here

Lanner answered 10/8, 2015 at 6:54 Comment(0)
G
14

In your PagerAdapter override getItemPosition

@Override
public int getItemPosition(Object object) {
// POSITION_NONE makes it possible to reload the PagerAdapter
return POSITION_NONE;
}

After that add viewPager.getAdapter().notifyDataSetChanged(); this code like below

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            viewPager.getAdapter().notifyDataSetChanged();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

should work.

Gladiate answered 9/11, 2016 at 7:7 Comment(1)
It works, but the UI starts getting slower when swipping between tabs. Thanks.Creeps
C
5

You can update the fragment in two different ways,

First way

like @Sajmon

You need to implement getItemPosition(Object obj) method.

This method is called when you call

notifyDataSetChanged()

You can find a example in Github and more information in this post.

enter image description here

Second way

My approach to update fragments within the viewpager is to use the setTag() method for any instantiated view in the instantiateItem() method. So when you want to change the data or invalidate the view that you need, you can call the findViewWithTag() method on the ViewPager to retrieve the previously instantiated view and modify/use it as you want without having to delete/create a new view each time you want to update some value.

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Object object = super.instantiateItem(container, position);
    if (object instanceof Fragment) {
        Fragment fragment = (Fragment) object;
        String tag = fragment.getTag();
        mFragmentTags.put(position, tag);
    }
    return object;
}

public Fragment getFragment(int position) {
    Fragment fragment = null;
    String tag = mFragmentTags.get(position);
    if (tag != null) {
        fragment = mFragmentManager.findFragmentByTag(tag);
    }
    return fragment;
}

You can find a example in Github or more information in this post:

enter image description here

Creolacreole answered 7/1, 2017 at 17:57 Comment(0)
H
4

Very simple to override the method in the fragment:

@Override

public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if(isVisibleToUser){
        actionView();
    }
    else{
        //no
    }      
}
Holland answered 24/11, 2016 at 7:31 Comment(1)
Currently this answer is deprecated, check the Simon answer, that is more correct.Centigram
G
3

Accepted answer is not understand, That's why added easy solution. after long struggle find Working solution.

Just notify to your adapter, it's working amazing,

Reference code

 void setAdapter(int position) {
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        // when notify then set manually current position.
        viewPager.setCurrentItem(position);
        pagerAdapter.notifyDataSetChanged();
    }

call this code when you set adapter and when you want to refresh UI.

In Your fragment add this code for refresh

((YourActivityName) getContext()).setAdapter(selectedTabPosition);

Use instanceof for cast context.

This code share only helping purpose who find solution.

Guitarfish answered 24/9, 2016 at 11:19 Comment(0)
L
2

A very simple yet effective solution to update your fragment views. Use setUserVisibleHint() method. It will be called upon when the fragment is about to be visible to user. There you can update our view (setText() or setVisibility() etc.)

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if(isVisibleToUser){
       //Write your code to be executed when it is visible to user
    }
}

It worked in my case. Hope it helps!

Lucianolucias answered 27/7, 2017 at 5:24 Comment(0)
H
2

If you use Kotlin, you can do the following:

1. On first, you should be create Interface and implemented him in your Fragment

interface RefreshData {
    fun refresh()
}

class YourFragment : Fragment(), RefreshData {
    ...
    override fun refresh() {
        //do what you want
    }
}

2. Next step is add OnPageChangeListener to your ViewPager

viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
    override fun onPageScrollStateChanged(state: Int) { }

    override fun onPageSelected(position: Int) {
        viewPagerAdapter.notifyDataSetChanged()
        viewPager.currentItem = position
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { }
})

3. override getItemPosition in your Adapter

override fun getItemPosition(obj: Any): Int {
    if (obj is RefreshData) {
        obj.refresh()
    }
    return super.getItemPosition(obj)
}
Houseroom answered 18/3, 2018 at 11:30 Comment(0)
E
2

i had 4 Fragments in the ViewPager, but i wanted to update just one Fragment "FavoriteFragment" at the position 0 of the viewPager, every time the user click on this fragment. but none of the codes above helped me reload just the one Fragment i want. so i tried this and it works for me:

in my FavoriteFragment i overrided onResume()

@Override
    public void onResume() {
        super.onResume();
        updateUI(); // code to update the UI in the fragment
    }

in the Activity hosting the viewPager override addOnPageChangeListener() and add this code

mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                if(position == 0) { // 0 = the first fragment in the ViewPager, in this case, the fragment i want to refresh its UI
                    FavoritesFragment fragment = (FavoritesFragment) mViewPager.getAdapter().instantiateItem(mViewPager, position); 
                    fragment.onResume(); // here i call the onResume of the fragment, where i have the method updateUI() to update its UI
                    mViewPager.getAdapter().notifyDataSetChanged();
                }
            }
        });

and it works perfect for me, to refresh/reload just the one fragment i want.

Empathize answered 23/2, 2019 at 15:6 Comment(1)
Thank you for sharing solution :)Flaming
T
2

Alot of answers, but a quick and dirty solution that got the job done for me is having a method to init your pagerAdapter in your MainActivity:

public void initPagerAdapter(){
        pagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
}

In my fragment where I want to refresh, I just get the MainActivity and call that method:

MainActivity activity = (MainActivity) getContext();
activity.initPagerAdapter();
Tyndale answered 7/4, 2021 at 3:30 Comment(0)
G
1

Maybe your method for adding items into fragment should be public (placed in desired Fragment) and should have parameter the same type as selectedItems ..

That will make it visible from activity, which will have selectedItems array and voila..

p.s. better name it addItemsFromArray(typeOfSelectedItems[] pSelectedItems) cause name addItem() is quite undescriptive

Edit: stackoverflow just suggested similar topic :) Check here for detailed idea implementation.. :)

Gadroon answered 6/8, 2013 at 19:5 Comment(0)
C
0

1) Create a handler in the fragment that you want to update.

public static Handler sUpdateHandler;

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

        ...

        sUpdateHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                // call you update method here.
            }
        };
    }

2) In the Activity/Fragment/Dialog, wherever you want the update call to be fired, get the reference to that handler and send a message (telling your fragment to update)

// Check if the fragment is visible by checking if the handler is null or not.
Handler handler = TaskTabCompletedFragment.sUpdateHandler;
if (handler != null) {
    handler.obtainMessage().sendToTarget();
}
Catherincatherina answered 30/3, 2016 at 12:28 Comment(0)
A
0

Because none of the above answers did the trick for me, here is my solution:

I combined the POSITION_NONE with loading on setUserVisibleHint(boolean isVisibleToUser) instead of onStart()

As seen here: https://mcmap.net/q/55047/-viewpager-setoffscreenpagelimit-0-doesn-39-t-work-as-expected

In the Fragment:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // load data here
    }else{
       // fragment is no longer visible
    }
}

and in the FragmentStatePagerAdapter as seen in the top answer here from Simon Dorociak https://mcmap.net/q/301358/-update-fragment-from-viewpager:

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

Now the fragments reload the data into their views everytime they are shown to the user.

Amor answered 12/4, 2019 at 7:37 Comment(0)
U
0

This worked for me:

Activity:

for (Fragment fragment : getSupportFragmentManager().getFragments()) {
    if (fragment.isVisible() && fragment instanceof MyFragment) {
        ((MyFragment) fragment).refreshContent();
        break;
    }
}

Fragment:

public void refreshContent() {
    //refresh content
}
Unconscionable answered 6/10, 2021 at 20:14 Comment(0)
B
0

To refresh the fragment in ViewPager. You can reload the ViewPager declaration function (I tried and succeeded):

public static void loadViewPager() {
    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(context);
    viewPager.setAdapter(viewPagerAdapter);
}

Call in another activity or fragment:

(Activity or Fragment).loadViewPager();
Betweenwhiles answered 4/1 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.